2

I wrote a powershell script that can be run from powershell like so:

PS C:> S:\MyScript.ps1 -a "select thing from table where name like 'name'"

I've stored it on a shared drive (S:), and I cannot run my powershell script in this form from either powershell or cmd.exe:

PS C:> powershell S:\MyScript.ps1 -a "select thing from table where name like 'name'"
C:\> powershell S:\MyScript.ps1 -a "select thing from table where name like 'name'"

I've also tried to run it bypassing the executionpolicy:

C:\> powershell -executionpolicy bypass -command S:\MyScript.ps1 -a "select thing from table where name like 'name'"

I've also tried to run it with the full UNC path (in all the forms above):

C:\> powershell \\MyServer\path\to\MyScript.ps1 -a "select thing from table where name like 'name'"

The only form that shows output is if I use a parameter that doesn't require a string to be passed:

C:\> powershell S:\MyScript.ps1 -z

thing
-----
thing1
thing2
thing3

Does anyone know why this is? Is there some way I can pass a string to the script from cmd.exe?

2 Answers 2

4

Also note, that powershell.exe has a parameter -File which might be better for a file than -command. I'm not sure about the script parameters as they should be grouped in a string like Nick said. Just try Nicks solution and if that doesn't work, try the -File parameter instead of -command.

Sign up to request clarification or add additional context in comments.

Comments

2

Try running it like this:

powershell -executionpolicy bypass -command {S:\MyScript.ps1 -a "select thing from table where name like 'name'"}

All cmd knows is that you sent it the command S:\MyScript.ps1 it doesn't know what to do with -a "select thing from table where name like 'name'" by enclosing in { } it should take the whole thing as the command to run. If it still causes a problem then try this:

powershell -executionpolicy bypass -command " &{S:\MyScript.ps1 -a "select thing from table where name like 'name'"}"

2 Comments

Also note, that powershell.exe has a parameter -File which might be better for a file than -command. I'm not sure about the script parameters as they should be grouped in a string like Nick said. Just try Nicks solution and if that doesn't work, try the -File parameter instead of -command.
I've copied my comment as answer. Did a simple replace of -command through -File work or did you have to escape your parameters in some way?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.