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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to start powershell with shebang from windows/cygwin/bash?
# 1  
Old 02-18-2013
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
Code:
#!powershell.exe -ExecutionPolicy unrestricted

The result is the result:
Code:
./set-sound-output.ps1
At line:1 char:33
+ "-ExecutionPolicy unrestricted" ./set-sound-output.ps1
+                                 ~~~~~~~~~~~~~~~~~~~~~~
Unexpected token './set-sound-output.ps1' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx 
   ception
    + FullyQualifiedErrorId : UnexpectedToken
 

Compilation finished at Mon Feb 18 09:52:59

The problem is that powershell needs this strange switch so it will execute (it is a mystery to me why this is not the default).

Can someone help me configure bash so it can use the shebang to start powershell?
Thanks
Siegfried
# 2  
Old 02-18-2013
#! lines must have absolute path /*
# 3  
Old 02-25-2013
OK, good to know! Thanks. But it still does not work. Any more ideas? I did this from bash:
Code:
chmod +x ./hello.ps1
./hello.ps1

and still received this response:
Code:
At line:1 char:33

+ "-ExecutionPolicy unrestricted" ./hello.ps1

+                                 ~~~~~~~~~~~

Unexpected token './hello.ps1' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx 

   ception

    + FullyQualifiedErrorId : UnexpectedToken

 

hello

Process compilation finished

Here is the source code:
Code:
#!c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe "-ExecutionPolicy unrestricted"
write-host "hello"

# 4  
Old 02-26-2013
In Cygwin, "c:\" is "/cygsrive/c/".
# 5  
Old 02-27-2013
Thanks again DGPickett! I'm still getting the same error, however.

Code:
#!/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy unrestricted
write-host "hello"

Here is the error:
Code:
-ExecutionPolicy : The term '-ExecutionPolicy' is not recognized as the name 

of a cmdlet, function, script file, or operable program. Check the spelling of 

the name, or if a path was included, verify that the path is correct and try 

again.

At line:1 char:1

+ -ExecutionPolicy unrestricted ./hello.ps1

+ ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-ExecutionPolicy:String) [], Co 

   mmandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Process compilation finished

# 6  
Old 02-27-2013
#! only accepts 1 argument. You can make a bash shell script that calls it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

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... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

2. UNIX for Beginners Questions & Answers

Using powershell on win10 to invoke bash

Hi Guys, I am trying to run flyway command on ubuntu. on windows command line is simple : C:\Users\admin\Downloads\flyway-commandline-5.0.7-windows-x64\flyway-5.0.7> flyway migrate how can i do it via bash? i tied to use mnt command but starting flyway is not possible. Please... (22 Replies)
Discussion started by: jaryszek
22 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. 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

5. Shell Programming and Scripting

Question about Shebang line of Bash Script

Hello All, I was writing a Bash shell script that will be executed on both an AIX server (/usr/bin/ksh) and a SLES server (/bin/bash). The AIX server has Bash installed at "/usr/bin/bash", which is in a different dir then the SLES server. So basically I am writing the script on the SLES... (4 Replies)
Discussion started by: mrm5102
4 Replies

6. Windows & DOS: Issues & Discussions

How can I start a cygwin application from windows 7 command prompt?

I have compiled an application called tmesh.exe, that was compiled for the cygwin, win7 platform. I wonder, if I do that outside X window, Is there a chance that I can run it on the command prompt of windows 7? (tmesh:4048): Gtk-WARNING **: cannot open display: THANKS Jack (3 Replies)
Discussion started by: lucky7456969
3 Replies

7. Shell Programming and Scripting

How to run Cygwin bash from windows scheduled task?

Hmmm.... I love these forums because I always get great prompt responses and I want to ask a question about running bash on windows. Is that allowed? Now I know I can install cygwin cron and run bash that way. Can I run bash from windows schedule task? How? thanks siegfried (1 Reply)
Discussion started by: siegfried
1 Replies

8. Shell Programming and Scripting

Error in shell script when #!/bin/bash is used as shebang

#!/bin/ksh echo -en "\033|||'-')) echo -e "\033 The above script works fine when the interpreter is ksh, but outputs the following error when #!/bin/bash is used as shebang: test.sh: line 5: syntax error near unexpected token `(' test.sh: line 5: `case "$ACTIVATION_KEY" in +(|||'-'))' (2 Replies)
Discussion started by: proactiveaditya
2 Replies

9. Windows & DOS: Issues & Discussions

Cygwin OpenSSH Windows

Hi all, I am just enjoying my first experience with UNIX type stuff but I am completely stumped with syntax etc. I have installed OpenSSH on my windows machine which is a working nicley and I can gain access using putty. I am however trying to set up public key authentication and turn off passwords... (2 Replies)
Discussion started by: bilbonvidia
2 Replies

10. BSD

How to use Cygwin to access X windows

Hi, I use WinMe, want to access, remotely, X-windows of my FreeBSD6.2 machine using the Cigwin from my windows machine as a normal user. However, I can login (through ssh and Telnet) to my FeeBSD machine as root and/or 'su' from some users grouped to the 'wheel' group. I'm still new to... (1 Reply)
Discussion started by: Praveen_218
1 Replies
Login or Register to Ask a Question