【问题标题】:How to run a script in administrator mode with commands to execute如何在管理员模式下使用要执行的命令运行脚本
【发布时间】:2020-08-07 13:30:25
【问题描述】:

我想从一个 powershell 脚本中以管理员模式启动另一个 powershell 实例,如果可能的话,向它指出一些要在同一命令行中执行的代码。

这是具体案例(我在 windows 下使用chocolatey 进行包管理):

Start-Process powershell -Verb runAs -ArgumentList "choco outdated"

此代码有效,但不幸的是,powershell 窗口在执行代码后立即关闭。所以“不可能”看到过时的包。

你有解决方案吗:

  • 在命令后冻结脚本?
  • 或添加更多代码行以放入Start-Sleep -Seconds 10
  • 或其他解决方案?

提前致谢!

【问题讨论】:

  • Read-Host 将中断并等待用户输入
  • 感谢您的回答!确实我试过这个:Start-Process powershell -Verb runAs -ArgumentList "choco outdated","Read-Host" 和这个:Start-Process powershell -Verb runAs -ArgumentList "choco outdated Read-Host" 不幸的是它不起作用,在显示过时的包后窗口关闭!
  • 你可以试试这个:Start-Process powershell -Verb runAs -ArgumentList "-NoExit choco outdated"
  • 它也很完美!谢谢!

标签: windows powershell administrator chocolatey


【解决方案1】:

这是让它等待的一种方法。

Start-Process powershell -Verb runAs -ArgumentList "choco outdated`n`rpause"

【讨论】:

  • 哦,完美!这正是我需要的,非常感谢!
【解决方案2】:

您可以使用“NoExit”参数: About PowerShell.exe

PowerShell[.exe]
    [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo]
    [-NoExit]
    [-Sta]
    [-Mta]
    [-NoProfile]
    [-NonInteractive]
    [-InputFormat {Text | XML}]
    [-OutputFormat {Text | XML}]
    [-WindowStyle <style>]
    [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File - | <filePath> <args>]
    [-ExecutionPolicy <ExecutionPolicy>]
    [-Command - | { <script-block> [-args <arg-array>] }
                | { <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

确保将参数以正确的顺序放入“-ArgumentList”参数中。

Start-Process powershell -Verb runAs -ArgumentList "-noexit", "-noprofile", "-command choco outdated"

【讨论】:

  • 你认为可以在最后添加几个命令吗?像这样:Start-Process powershell -Verb runAs -ArgumentList "-noexit", "-noprofile", "-command choco outdated", "-command choco upgrade"
猜你喜欢
  • 2021-06-14
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 2011-11-29
  • 2014-10-22
  • 1970-01-01
相关资源
最近更新 更多