在尝试使用提升的权限运行进程时,我遇到了非常奇怪的错误。我写了一个PS函数,它获取命令和用户凭据,它应该在这些凭据下执行命令。它就像Start-Process cmdlet,但我编写了该函数,因为我需要捕获已执行命令的输出。关键在于,当我将Verb
的属性ProcessStartInfo
设置为应该启动UAC的“runas”时。
我的功能在这里:
Function Grant-ElevatedPrivileges {
[cmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String]$command
,
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$credential
)
Write-Verbose -msg "EXECUTING COMMAND WITH ELEVATED PRIVILEGES: $command"
# Write-Host "$command"
$result = @{'result' = $false; 'output' = $false; 'error' = $false};
$psi = New-object System.Diagnostics.ProcessStartInfo
#$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
$psi.FileName = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
$psi.Arguments = @"
-ExecutionPolicy Bypass -noprofile $command
"@
$psi.UserName = $credential.GetNetworkCredential().UserName
if ($credential.GetNetworkCredential().Domain -ne "") { $psi.Domain = $credential.GetNetworkCredential().Domain }
$psi.Password = $credential.Password
$psi.Verb = "runas"
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $psi
try {
$result['result'] = $process.Start()
$process.WaitForExit()
$result['output'] = $process.StandardOutput.ReadToEnd()
$result['error'] = $process.StandardError.ReadToEnd()
} catch [System.ComponentModel.Win32Exception] {
if (($_.Exeption.NativeErrorCode) -eq 1326) {
Write-Verbose "USUARIO O CONTRASEÑA INCORRECTA"
$result['error'] = "BADUSER"
} else {
$result['error'] = $_.Exception.Message
}
$result['result'] = $false
} catch {
$result['result'] = $false
$result['error'] = Write-Error $_.Exception.Message
}
if ($result['error'] -ne '') {
Write-Verbose $result['error']
$result['result'] = $false
}
$result
}
我通过调用函数检查我提到的行为:
Grant-ElevatedPrivileges -command "([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')" -credential $creds -Verbose
如果命令在管理员权限下执行,则应返回True ...
关于我缺少什么的任何想法?
感谢您的建议或想法;)