【问题标题】:Syntax error when starting powershell from bat file从 bat 文件启动 powershell 时出现语法错误
【发布时间】:2014-11-05 09:14:24
【问题描述】:

我想用 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中添加-NoExit参数,这样它就不会退出,你可以阅读错误信息。
  • 去哪里?执行此操作时出现错误:找不到与参数名称“noexit”匹配的参数。
  • 由于您没有告诉您尝试添加开关的位置,请阅读 Powershell.exe 的 help 并查找正确的语法。
  • 我尝试了所有可以想到的地方。在第一个 powershell 之后,在第二个之后,在参数列表中。没有骰子。帮助文档对此没有提供任何帮助。
  • 源代码如果我删除了redirectstandardoutput,-noexit 工作正常。但这并不重要..

标签: powershell command-line


【解决方案1】:

您收到错误消息,因为 powershell.exe 没有 -RedirectStandardOuptut 参数。 (见Technet)。

你的语法也很离谱(但因为我看不出有任何理由启动 powershell 来再次启动 powershell,所以我不会费心解释语法错误)。

如果您想从 cmd 使用 RunAs,请直接使用它。如需更多信息,请参阅Technet(再次)。

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

【讨论】:

  • 我不想要 .bat 文件的输出。没有 powershell 没有名为 -RedirectStandardOuptut 的参数,但 start-process 确实有一个名为该参数的参数。我需要启动 prowershell 两次,这样我才能使用 RunAs。我认为你不明白我想要做什么。
  • @user3019059 要么你没有得到我的答案。您想重定向您启动的 powershell 实例的输出吗?正如我的回答所说,您可以只使用蝙蝠中的 runas 并从那里重定向。如果您由于某种原因仍然必须使用您的方法,请正确使用您的语法!在您的示例代码中,您正在传递 -redirect... 作为 powershell.exe 的参数,这显然不起作用
  • 如果我将它添加到参数列表中,我会收到语法错误。而且我不知道为什么,也看不到错误。
  • @user3019059 您必须将其添加为启动进程的参数,argumentlist 参数包含您传递给进程的参数
  • PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList ' -NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\TSM Client Install Script.ps1""' -RedirectStandardOutput ""%InstallerFolder%\node.txt"" -Verb RunAs}";哪个在这里?这也不起作用。
猜你喜欢
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多