无法从cmd.exe运行powershell脚本?

时间:2013-01-18 21:33:00

标签: string powershell powershell-v2.0 cmd

我编写了一个PowerShell脚本,可以像PowerShell一样从PowerShell运行:

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

我已将其存储在共享驱动器(S :)上,并且我无法从powershell或cmd.exe运行此表单中的powershell脚本:

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'"

我也尝试绕过executionpolicy运行它:

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

我也尝试用完整的UNC路径运行它(以上面的所有形式):

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

显示输出的唯一形式是,如果我使用不需要传递字符串的参数:

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

thing
-----
thing1
thing2
thing3

有谁知道这是为什么?有什么方法可以将字符串从cmd.exe传递给脚本吗?

2 个答案:

答案 0 :(得分:4)

另请注意,powershell.exe的参数-File可能比-command更适合文件。我不确定脚本参数,因为它们应该像Nick所说的那样分组。只需尝试使用Nicks解决方案,如果不起作用,请尝试-File参数而不是-command

答案 1 :(得分:2)

尝试像这样运行:

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

所有cmd都知道你发送了S:\MyScript.ps1命令-a "select thing from table where name like 'name'",它不知道如何处理{ }通过封闭在powershell -executionpolicy bypass -command " &{S:\MyScript.ps1 -a "select thing from table where name like 'name'"}" 它应该把整个事情作为命令来跑。如果它仍然导致问题,那么试试这个:

{{1}}
相关问题