Running Powershell Script from Linux through Cygwin


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Running Powershell Script from Linux through Cygwin
# 1  
Old 07-26-2018
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
.
HTML Code:
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! Smilie
# 2  
Old 07-26-2018
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?
# 3  
Old 07-26-2018
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
Code:
ssh odshekhar@Windowsmachine  'powershell -executionpolicy bypass -File  "C:\scripts\hello-world.bat"'


What output/errors do you get?



Thanks, in advance,
Robin
# 4  
Old 07-26-2018
Quote:
Originally Posted by rbatte1
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
Code:
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

Code:
echo "Hello World!"
import-module ActiveDirectory
get-adcomputer -Filter * |select Name|select -first 10

2. 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-

Code:
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

3. Now I generate an ssh-key on Linux for user shekhar and copy it on Windows machine to make a password less connection.

Code:
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.

4. 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

Code:
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.

Last edited by RudiC; 07-26-2018 at 04:37 PM..
# 5  
Old 07-30-2018
Hi All,

I will appreciate any help. Thanks.
# 6  
Old 07-30-2018
"Unable to contact the server".

I suggest looking into Windows Firewall.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

powershell script to unix shell script conversion.

Here is a powershell script to use restful API to create ticket in our ticketing tool. Can anyone please convert it to a shell script sothat, I can run it in Unix servers, below is the code: $body = @{ Customer= ''test' Summary= 'test summary' Impact= '4-Minor/Localized' ... (2 Replies)
Discussion started by: pandeybhavesh18
2 Replies

2. Shell Programming and Scripting

Execute powershell script with UNIX

Hi I have a powershell script which is checking whether a windows service is running (as shown below) function FuncCheckService { $ServiceName = 'pgsql-9.2' $arrService = Get-Service -Name $ServiceName if ($arrService.Status -eq 'Running') { Write-Output... (8 Replies)
Discussion started by: simpsa27
8 Replies

3. Shell Programming and Scripting

Powershell script to monitor windows process

Hello Friend, I am not expert in power shell scripting. I nee custom powershell script which check if given process is running on windows machine or not. will use it in nagios to monitor that process. (0 Replies)
Discussion started by: ghpradeep
0 Replies

4. Programming

Running python script in cygwin

I have python installed here on windows: C:\Python27 and can run a script from the command line but would rather use cygwin. $ cd "C:\Users\cmccabe\Desktop\annovar" cmccabe@DTV-L2231M5J /cygdrive/c/Users/cmccabe/Desktop/annovar $ python run_batch_job.py Traceback (most recent call last):... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Bash script - cygwin (powershell?) pull from GitHub API Parse JSON

All, Have a weird issue where i need to generate a report from GitHub monthly detailing user accounts and the last time they logged in. I'm using a windows box to do this (work issued) and would like to know if anyone has any experience scripting for GitAPI using windows / cygwin / powershell?... (9 Replies)
Discussion started by: ChocoTaco
9 Replies

6. Shell Programming and Scripting

Running q-shell commands( on IBM-i Series) from cygwin terminal (on windows)

I have cygwin installed on windows server and when I do echo $SHELL the output is /bin/bash I have created a ssh tunnel from this windows server through cygwin to ibm -i series which is running Q-shell. I am trying to invoke a utility wsadmin (used for scripting) on ibm-i from the... (12 Replies)
Discussion started by: gaurav99
12 Replies

7. Shell Programming and Scripting

How to start powershell with shebang from windows/cygwin/bash?

I would like to the the windws8/cygwin/bash shebang feature to start a powershell script. I do a "chmod +x set-sound.ps1" and then at a bash prompt I do ./set-sound.ps1 The first line of ./set-sound.ps1 #!powershell.exe -ExecutionPolicy unrestricted The result is the result: ... (5 Replies)
Discussion started by: siegfried
5 Replies

8. Windows & DOS: Issues & Discussions

Powershell Script Help

Need some help getting this script to work. can someone help. I am trying to create a script that moves log files to another location and only keeps 60 days worth. please see script I started but having issue getting to work.. $TODAY=GET-DATE GET-CHILDITEM C:\test\*.LOG | Where {... (9 Replies)
Discussion started by: freedavis
9 Replies

9. Shell Programming and Scripting

Running shell script in Cygwin terminal

I am new to shell scripting. I tried to run a simple shell script using Cygwin terminal in Win XP env. The script I have written is as follows - #!/bin/bash a=5 ] && echo "true" || echo "false" But when I execute the script, getting some confusing error. The error I am getting are - ... (3 Replies)
Discussion started by: linux_learner
3 Replies

10. UNIX for Dummies Questions & Answers

Running a.out on Cygwin

I wrote and compiled a program on a unix machine using g++. Now I copied it over to the machine with the Unix emulator Cygwin. I tried to run it by entering "a.out" in the command prompt but was given: "bash: a.out: command not found" (without the quotes) Also worth mentioning is that i... (5 Replies)
Discussion started by: hito
5 Replies
Login or Register to Ask a Question