Running Powershell Script from Linux through Cygwin

Hello Experts,

I am creating a run time powershell script on Linux machine and copying that powershell script to Windows machine. To connect to windows through "ssh", I am using Cygwin tool. To make the connection password less I copied my public in authorized_keys in windows Administrator account and now I am able to login without password like below and able to run some powershell script like echoing "Hello world" and running some Windows command from Linux machine
.

ssh Administrator@windowsServer

But, when I call any script that needs elavated access(tha's what I observed, I may be wrong also" it gives me error.

odshekhar@LinuxMachine:/tmp$  ssh odshekhar@Windowsmachine  'powershell -executionpolicy bypass -File  "C:\scripts\abc.ps1"'

If I delete my public key from windows machine and perform the same operation from Linux machine(that is if i don't use password less connection), It asks me my password for Windows machine and it works.

So, in short, it does not work when I try to use passowrd less connection and works if I dont use passowrd less connection.

Please note it is not a permission issue or any issue to establish a connection to make a connection because I am able to run simple script making password-less connection and able to run many commands too.

Thanks in advance! :slight_smile:

What be the error / error message? Can you run in debug / xtrace mode?

Isn't this a windows related problem? Shouldn't you post in the DOS / windows forum, then?

Can you get a very simple proof of concept script to work? A quick "Hello-world" through the same mechanism would hopefully show if it is the process of elevating privilege or the commands actually running that is the problem. So a

ssh odshekhar@Windowsmachine  'powershell -executionpolicy bypass -File  "C:\scripts\hello-world.bat"'

What output/errors do you get?

Thanks, in advance,
Robin

okay. So below is the explanation and error.

  1. The script which I have put on my Windows machine: abc.ps1
echo "Hello World!"
import-module ActiveDirectory
get-adcomputer -Filter * |select Name|select -first 10

  1. When I run it from Linux machine using Cygwin WITHOUT enabling password-less connection, It asks for password and execute like a charm. See below-
shekhar@LinuxBox:~$ ssh shekhar@WindowsBox.doamin.com 'powershell "C:/scripts/abc.ps1"'
shekhar@WindoxBox.domain.com's password:

Hello World!

Name
----
Server1
Server2
Server3
Server4
  1. Now I generate an ssh-key on Linux for user shekhar and copy it on Windows machine to make a password less connection.
shekhar@LinuxBox:~$ ssh-copy-id shekhar@WindowsBox.doamin.com
shekhar@WindowsBox's password:
Now try logging into the machine, with "ssh 'shekhar@WindowsBox.domain.com'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

  1. Now I run the same script again. Now it doesn't asks a password makes a connection with Windows box and display Hello world(first line of code) but fails on the second line
shekhar@LinuxBox:~$ ssh shekhar@WindoxBox.domain.com  'powershell "C:/scripts/abc.ps1"'
Hello World!
WARNING: Error initializing default drive: 'Unable to contact the server. This
may be because this server does not exist, it is currently down, or it does not
 have the Active Directory Web Services running.'.
Get-ADComputer : Unable to contact the server. This may be because this server
does not exist, it is currently down, or it does not have the Active Directory
Web Services running.
At C:\scripts\abc.ps1:3 char:15
+ get-adcomputer <<<<  -Filter * |select Name|select -first 10
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ADComputer], ADSer
   verDownException
    + FullyQualifiedErrorId : Unable to contact the server. This may be becaus
   e this server does not exist, it is currently down, or it does not have th
  e Active Directory Web Services running.,Microsoft.ActiveDirectory.Managem
 ent.Commands.GetADComputer

I am using Cygwin to establish ssh connection from LinuxBox to Windiws Box.

Hope this explains the issue.

Hi All,

I will appreciate any help. Thanks.

"Unable to contact the server".

I suggest looking into Windows Firewall.