以管理员身份运行PS但收到错误 - 该操作需要提升

时间:2015-03-11 15:31:30

标签: powershell credentials invalidoperationexception

由于各种新的安全策略,我们需要能够以指定的uid运行各种任务。为了协助这一点,开发了一个通用的菜单系统。脚本中的一个功能是:

$credential = user_credential
$cmd = "C:\windows\system32\mmc.exe" 
$args = "C:\Windows\System32\compmgmt.msc"
Start-Process -FilePath $cmd -ArgumentList $args  -Credential $credential

收到错误:

Start-Process : This command cannot be run due to the error: The requested operation requires elevation.
t C:\webeng\webeng.ps1:131 char:5
     Start-Process -FilePath $cmd -ArgumentList $args  -Credential $credential
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
   + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

该错误似乎表明PS脚本需要提升的私有,但我已经以管理员身份运行。

我可能会在这里失踪什么?

2 个答案:

答案 0 :(得分:1)

# variables
$mmc = "$($env:SystemDrive)\Windows\System32\mmc.exe"
$msc = "$($env:SystemDrive)\Windows\System32\compmgmt.msc"

# credentials
$username   = "DOMAIN\USERNAME"
$securePass = ConvertTo-SecureString "PASSWORD" -AsPlainText –Force
$cred       = New-Object System.Management.Automation.PSCredential $username, $securePass

# call MSC
Start-Process powershell.exe -Credential $cred -ArgumentList "Start-Process -FilePath $mmc -ArgumentList $msc -Verb runAs"

答案 1 :(得分:0)

当您以管理员身份使用PowerShell会话登录PowerShell时,您的问题将消失。这将使您在活动会话中缺少提升的权限。 如果您在服务器上运行脚本时遇到问题并且需要额外的努力,则此解决方案有效。 https://blogs.msdn.microsoft.com/virtual_pc_guy/2010/09/23/a-self-elevating-powershell-script/

GLHF!