Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从bat文件启动powershell时出现语法错误_Powershell_Command Line - Fatal编程技术网

从bat文件启动powershell时出现语法错误

从bat文件启动powershell时出现语法错误,powershell,command-line,Powershell,Command Line,我想使用bat文件中的RunAs启动powershell脚本。这很有效 @echo SET "InstallerFolder=\\dc01\e\script" PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\Scrip

我想使用bat文件中的RunAs启动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错误中重新出现,这个错误发生得太快了,我看不见。 可能是语法

非常感谢您的帮助!
谢谢。

您会收到一个错误,因为
powershell.exe
没有
-redirectStandardOutput
参数。(见附件)

另外,您的语法也不太正确(但因为我看不出有任何理由启动powershell以再次启动powershell,所以我不会费心解释语法错误)

如果您想使用cmd中的RunAs,请直接使用它。有关更多信息,请参阅(再次)


此外,如果要追加,您还可以使用
>
将批处理文件中的输出重定向。

-NoExit
参数添加到Powershell,这样它就不会退出,并且您可以读取错误消息。到哪里?我这样做时会出错:找不到与参数名称“noexit”匹配的参数。由于您没有说明尝试在何处添加开关,请阅读Powershell.exe并查找正确的语法。我尝试了所有可以想到的地方。在第一个powershell之后,在第二个powershell之后,在argumentlist内部。没有骰子。帮助文档对此没有提供任何帮助。如果我删除重定向标准输出,-noexit工作正常。但是这并不重要,我不想要.bat文件的输出。没有powershell没有名为-RedirectStandardOutput的参数,但启动进程有一个名为该参数的参数。我需要启动prowershell两次,以便使用RunAs。我想你不明白我想做什么。@user3019059要么这样,要么你得不到我的答案。是否要重定向启动的powershell实例的输出?正如我的回答所说,你可以从你的bat中使用runas并从那里重定向。如果出于某种原因,您仍然需要使用您的方法,只需正确使用语法即可!在您的示例代码中,您正在传递-redirect。。。作为powershell.exe的参数,如果将其添加到argumentlist,则会出现语法错误。我不知道为什么,也看不到错误。@user3019059您必须将其作为启动进程的参数添加,argumentlist参数包含传递给processPowerShell-NoProfile-ExecutionPolicy Bypass-Command“&{start process PowerShell-argumentlist'-NoProfile-ExecutionPolicy Bypass-File”的参数“%InstallerFolder%\TSM客户端安装脚本.ps1”“-重定向标准输出”“%InstallerFolder%\node.txt”“-动词RunAs}”,在这里?这也不起作用。