使用凭据启动的进程等待时访问被拒绝

3
我有一个脚本在用户上下文中运行,该用户没有安装应用程序的权限。我需要安装一个exe文件,所以我需要以域管理员的身份进行安装。
安装完成后,我想在脚本中执行其他操作,但必须等待安装完成后才能继续脚本。
当我使用Wait-Process等待安装结束时,会出现访问被拒绝的错误。
有没有一种方法可以等待在另一个用户上下文中启动的进程结束?
这是我的代码:
$P = Start-Process $Pfad\vcredist_x64.exe -Argumentlist "/install","/passive","/norestart" `
                         -WorkingDirectory $Pfad -Credential $cred -PassThru | Wait-Process

以下是错误信息的翻译(从德语翻译):

访问被拒绝

Wait-Process:此命令终止了“vcredist_x64(6284)”操作,原因是出现了以下错误:访问被拒绝。在第6行 字符:84 + ... -WorkingDirectory $Pfad -Credential $cred -PassThru | Wait-Process + ~~~~~~~~~~~~ + CategoryInfo : CloseError: (System.Diagnost... (vcredist_x64):Process) [Wait-Process], ProcessCommandException + FullyQualifiedErrorId : ProcessNotTerminated,Microsoft.PowerShell.Commands.WaitProcessCommand

超时问题

Wait-Process:此命令终止了进程,因为“vcredist_x64(6284)”进程未在指定的超时时间内完成。在第6行 字符:84 + ... -WorkingDirectory $Pfad -Credential $cred -PassThru | Wait-Process + ~~~~~~~~~~~~ + CategoryInfo : CloseError: (System.Diagnost... (vcredist_x64):Process) [Wait-Process], TimeoutException + FullyQualifiedErrorId : ProcessNotTerminated,Microsoft.PowerShell.Commands.WaitProcessCommand


你尝试过使用Start-Process <exe路径> -NoNewWindow -Wait或者WaitForExit()方法吗? - Clint
@Clint 是的,使用-wait也会出现访问被拒绝的情况,WaitForExit()似乎根本不起作用。 - SimonS
@Clint 好的,现在 WaitForExit 怎么样了,非常感谢! - SimonS
2个回答

2
将我之前的评论发布为答案
$proc = Start-Process "Notepad.exe" -PassThru
$proc.WaitForExit()
$proc1 = Start-Process "Calc.exe" -PassThru
$proc1.WaitForExit()

0

我前几天也做了类似的事情。我的情况有点不同。我正在启动某个进程,希望在该进程结束后发生某些事情,同时我还在做其他事情。为此,我使用了一个事件。以下是使用记事本的示例:

$do_other_stuff = { 
    Write-Host 'Do Other Stuff'
    Get-EventSubscriber | Unregister-Event
}

$p = Start-Process notepad.exe -PassThru
...do other stuff...

$job = Register-ObjectEvent -InputObject $p `
    -EventName Exited `
    -SourceIdentifier notepad `
    -Action $do_other_stuff

如果我想等待事件触发,我会使用Wait-Event。在$do_other_stuff脚本块中放置任何你想要完成的安装程序运行后的任务。
对于你的问题,以下方法适用于我:
$p = Start-Process notepad.exe -PassThru -Wait
...do stuff...

As did...

$p = Start-Process notepad.exe -PassThru
$p.WaitForExit()
...do stuff...

这些情况在提升和非提升上下文中运行脚本时都有效。通过-Credential参数将凭据传递给Start-Process时,它可以正常工作。我无法尝试调用Start-Process的帐户是低特权帐户的情况;我有一个会议要参加,抱歉。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接