使用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
- 为什么上面的代码片段没有达到我期望的效果(即,
-命令在gpupdate进程响应时没有真正关闭gpupdate进程)wait进程
/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错误处理的内容值得一读吗