2014-11-05 156 views
0

我想從bat文件啓動RunAs腳本的PowerShell腳本。這工作。從bat文件啓動powershell時出現語法錯誤

@echo 
SET "InstallerFolder=\\dc01\e\script" 

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\Script.ps1""' -Verb RunAs}"; 

但是,如果我補充一下:

-RedirectStandardOutput ""%InstallerFolder%\node.txt"" 

它打破。

所以行看起來是這樣的:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-RedirectStandardOutput ""%InstallerFolder%\node.txt"" -NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\TSM Client Install Script.ps1""' -Verb RunAs}"; 

以及其中如此之快,我不能看到它的PowerShell的錯誤resuslts。 可能的語法?

非常感謝! 謝謝。

+0

將'-NoExit'參數添加到Powershell,所以它不會退出,您可以閱讀錯誤消息。 – vonPryz 2014-11-05 09:40:27

+0

去哪裏?當我這樣做時出現錯誤: 找不到與參數名稱'noexit'匹配的參數。 – user3019059 2014-11-05 09:47:50

+0

由於您沒有告訴您嘗試添加開關的位置,請閱讀Powershell.exe的[help](http://technet.microsoft.com/zh-cn/library/hh847736.aspx),然後查找正確的語法。 – vonPryz 2014-11-05 11:31:41

回答

0

出現錯誤,因爲powershell.exe沒有-RedirectStandardOuptut參數。 (見Technet)。

另外你的語法是關閉的(但因爲我沒有看到任何理由啓動PowerShell再次啓動PowerShell,我不會麻煩解釋語法錯誤)。

如果您想使用cmd中的RunAs,請直接使用它。欲瞭解更多信息,請參閱Technet(再次)。

如果您想追加,您也可以使用>>>重定向批處理文件中的輸出。

+0

我不想從.bat文件輸出。沒有PowerShell沒有一個名爲-RedirectStandardOuptut的參數,但是啓動進程確實有一個叫做那個的參數。我需要啓動prowershell兩次,所以我可以使用RunAs。我不認爲你明白我在做什麼。 – user3019059 2014-11-07 08:42:27

+0

@ user3019059要麼你或你沒有得到我的答案。你想重定向你開始的PowerShell實例的輸出?正如我的回答指出,你可以使用蝙蝠的runas並從那裏重定向。如果你出於某種原因仍然需要使用你的方法,那就讓你的語法正確吧!在你的示例代碼中,你正在傳遞-redirect ...作爲powershell.exe的參數,顯然不起作用 – Paul 2014-11-07 17:20:26

+0

如果將它添加到參數列表中,我會得到語法錯誤。我不知道爲什麼,我也看不到錯誤。 – user3019059 2014-11-07 17:25:57