使用powershell进行gpupdate

使用powershell进行gpupdate,powershell,wait,updating,group-policy,Powershell,Wait,Updating,Group Policy,我想执行gpupdate作为System.IO.FileSystemWatcher实例的-Action,但从PowerShell调用gpupdate时,有时会完全挂起。我尝试了以下方法来处理inresponsive gpupdate,但它并没有像预期的那样工作-当gpupdate出错时,整个脚本仍然挂起 $command = "/target:computer" $gp = Start-Process -FilePath gpupdate ` -ArgumentList $comm

我想执行
gpupdate
作为
System.IO.FileSystemWatcher
实例的
-Action
,但从PowerShell调用gpupdate时,有时会完全挂起。我尝试了以下方法来处理inresponsive 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进程
    -命令在gpupdate进程响应时没有真正关闭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错误处理的内容值得一读吗

也许您可以添加/wait参数。请看这里:谢谢。我会试试这个,因为它可能会解决我的问题。(尽管对我来说它仍然很神秘,为什么上面的代码不能按预期工作…)我尝试了它,但由于一些奇怪的原因它没有按预期工作。我也更新了我的问题。也许你对此有更多的想法?提前谢谢!