2013-01-18 128 views
2

我寫的,可以從PowerShell中,像這樣運行的PowerShell腳本:我已經將其存儲在共享驅動器(S :)上無法從cmd.exe運行powershell腳本?

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

,我不能以這種形式無論從PowerShell中運行我的PowerShell腳本或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'" 

我也試着運行它繞過executionpolicy:

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

我也試着用完整的UNC路徑運行(在人以上L的形式):

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傳遞給腳本?

回答

4

還要注意,powershell.exe有一個參數-File這可能是比-command文件更好。我不確定腳本參數,因爲它們應該像尼克說的那樣按照字符串分組。試試尼克斯解決方案,如果不行,請嘗試-File參數而不是-command

2

嘗試運行它像這樣:

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

所有的命令都知道的是,你在{ }封閉,應該把整個事情作爲發送它的命令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'"}" 
+3

另請注意,powershell.exe有一個參數'-File',它可能比'-command'更適合文件。我不確定腳本參數,因爲它們應該像尼克說的那樣按照字符串分組。試試Nicks解決方案,如果這樣做不起作用,請嘗試'-File'參數而不是'-command'。 – wullxz

+0

-文件似乎工作,謝謝! – EGr

+0

@wullxz把這個作爲一個答案我會投票。 –