我想将gpupdate
作为System.IO.FileSystemWatcher
实例的-Action
执行,但 gpupdate 在从 PowerShell 调用时偶尔会完全挂起。我尝试了以下方法来处理无响应的 gpupdate,但它不能按预期工作-当 gpupdate 出错时,整个脚本仍然挂起。
$command = "/target:computer"
$gp = Start-Process -FilePath gpupdate `
-ArgumentList $command -NoNewWindow -PassThru
wait-process -inputobject $gp -timeout 10
if( $gp.HasExited -ne $true )
{
stop-process -inputobject $gp -force
$errcode = 99
}
else
{
$errcode = $gp.exitcode
}
我的问题是
有没有人遇到过类似的问题,从 PowerShell(v2)调用gpupdate
;可能是什么原因?
为什么上面的代码片段没有做我期望它做的(即为什么wait-process
-命令没有真正关闭 gpupdate 进程,当后者是无响应的)?
我已经尝试添加/wait
参数,但它没有改变任何东西。
$gp = Invoke-WmiMethod -ComputerName "localhost"`
-EnableAllPrivileges`
-Path win32_process -Name "create"`
-ArgumentList "gpupdate /target:Computer /wait:5"
与$gp.ReturnValue
作为$errcode
,但问题仍然存在。在几次运行后,我仍然没有得到脚本的响应,并且订阅者停止触发。在尝试了start-job
和wait-job
之后,这也没有解决它,我的机智结束了。
有没有更好的选择来执行此任务?
是否值得阅读 powershell 错误处理?
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(17条)