【问题标题】:Run a powershell script from cmd with elevated privilages and passing parameters使用提升的权限和传递参数从 cmd 运行 powershell 脚本
【发布时间】:2021-04-17 02:44:41
【问题描述】:

我正在尝试以提升的权限从 cmd 运行 powershell 脚本,但我需要将参数传递给 powershell 脚本。这是我正在使用的批处理文件的代码:

echo off
cls
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -Verb runas -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dp0\MigrationTool.ps1 -ConfigFile ConfigFile.xml""'}"

当我使用上面的代码时,脚本什么都不做。

如果我在没有 -ConfigFile 参数的情况下运行脚本,则脚本会运行,然后它会提示我输入配置文件。虽然我得到提示,然后我只是输入配置文件,但脚本仍然不起作用。

我在互联网上做了一些挖掘,但找不到任何解决方案。

谢谢。

【问题讨论】:

  • 为什么要运行cmd运行PowerShell运行PowerShell?可能有一个不太复杂的解决方案来解决您的问题。您可以查看runas 工具。
  • 这个问题可能是由双重""引起的。 cmd 和 powershell 转义不同。

标签: powershell batch-file


【解决方案1】:

我通常是这样做的:

@echo off

REM Calling script without parameters
REM powershell.exe -ExecutionPolicy Bypass -File E:\scripts\some-ps-script.ps1

REM Calling script with option 'Parameter'
powershell.exe -ExecutionPolicy Bypass -File "E:\scripts\some-ps-script.ps1" -Parameter

这应该适用于您的情况:

powershell.exe -ExecutionPolicy Bypass -File "E:\scripts\some-ps-script.ps1 -ConfigFile E:\scripts\ConfigFile.xml"

【讨论】:

  • 它没有被提升 = 作为管理员 ;)
  • 当然,你是对的。我完全忽略了“具有提升权限的 cmd”。
【解决方案2】:

感谢您的帮助,但我找到了解决问题的方法。 请参阅下面的其他人推荐的内容:

回声 cls

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "%~dp0MigrationTool.ps1" -ConfigFile "%~dp0ConfigFile.xml "'"

感谢您的所有帮助。

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 1970-01-01
    • 2021-01-09
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多