【问题标题】:Run powershell script as administrator and with a different user account以管理员身份并使用不同的用户帐户运行 powershell 脚本
【发布时间】:2018-09-15 11:49:51
【问题描述】:

我需要使用 powershell 以管理员身份和当前登录用户以外的用户身份启动 powershell 脚本。这是为了在多台服务器机器上运行 GPO 的一些更新。目的是让脚本在启动时提示输入凭据。

这是我迄今为止尝试过的:

 Start-Process 'powershell.exe' -Credential 'adminAccount' -ArgumentList '"file path\updateGPO.ps1" -Verb runAs'

这会提示输入密码,并将使用指定帐户运行脚本,但不会以管理员身份运行,管理员操作会出现“拒绝访问”错误。新的 powershell 窗口在标题中缺少管理员文本。

我还尝试了以下方法:

 $script = 'file path\updateGPO.ps1'
 Start-Process 'powershell.exe' -Credential 'adminAccount' -ArgumentList '-command &{start-process $script} -Verb runAs'

这会启动一个 powershell 窗口,不写入任何文本,然后立即关闭。

提前感谢您提供任何见解。

【问题讨论】:

  • -VerbStart-Process (Get-Command -Name Start-Process -Syntax) 上使用 -Credential 时不是有效参数
  • 如果您的脚本需要提升,请使用自提升机制。 Here's a github snippet example

标签: powershell administrator gpo


【解决方案1】:

TheIncorrigible1 的评论让我走上了正轨。

 Start-Process 'powershell.exe' -Credential 'adminAccount' -ArgumentList '"file path\updateGPO.ps1"'

在 updateGPO.ps1 脚本的顶部,我包含了自提升机制。

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2023-03-22
    • 2016-11-22
    • 2022-01-08
    相关资源
    最近更新 更多