2014-11-05 157 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