Wiki

Clone wiki

powershell / Home

PowerShell ... the "wrapper"!

Using Cygwin makes you wish for a "Why can't we all just get along" computer world. Why can't a Cygwin shell session launch a PowerShell script and get a result? Forget the Cygwin vs. PowerShell arguments. Ignore all the "Why would you want to do that" answers.

The powershell wrapper script started out as a simple 3 line bash script that would allow me to add a she-bang to a powershell .ps1 script file so it would execute like any other script in cygwin/bash. This would call powershell.exe like a good little interpreter to execute the script.

It worked Great! But I still wasn't happy. PowerShell needs options like "-SetExecutionPolicy" and others to run scripts. So I added options to the wrapper script. This was going like gang-busters!

So what can we add next? Well, why can't I call powershell -File /path/to/a/powershell/script.ps1? Or even check if the file arg is actually a file and automagically add the "-File" arguement. You can call a perl script this way. So now you can just powershell script.ps1 directly in Cygwin and get an answer.

Why not just run a command from the wrapper, ala powershell -Command write-host Hello. That's done too!

Examples:

http://stackoverflow.com/questions/14044671/running-powershell-via-cygwin?rq=1 using my script:

$ ./powershell -b -c 'Get-EventLog System | Where-Object { $_.EventID -match "6009" }'

   Index Time          EntryType   Source                 InstanceID Message   
   ----- ----          ---------   ------                 ---------- -------   
  454862 Oct 21 20:10  Information EventLog               2147489657 Microso...
  453331 Oct 13 21:41  Information EventLog               2147489657 Microso...
...

http://stackoverflow.com/questions/10316758/monitor-windows-services-with-cygwin

$ ./powershell -b -c gsv

Status   Name               DisplayName                           
------   ----               -----------                           
Running  AdobeARMservice    Adobe Acrobat Update Service          
Stopped  AdobeFlashPlaye... Adobe Flash Player Update Service     
Running  AeLookupSvc        Application Experience
...

hmmm, what else can we add? What if I just want to run powershell as an interpreter directly in my session and then work interactively at the PS> promtp?, Just say powershell with no args and get me a nice old PS> prompt waiting for input? No go! I stepped too far on this one. These two refuse to work together here.

Pretty hard to get powershell to be interactive in a rxvt or mintty terminal session in Cygwin_x86 and I doubt x64 is any better.

So here we are. A full featured wrapper to call powershell from Cygwin and the only thing I can't do is go interactive with powershell.exe and get a working prompt.

Tired of Google searching on this one. No answers on StachExchange. Hate to give up on it though...

Updated