【问题标题】:Timeout Get-WMIObject cmdlet超时获取 WMIObject cmdlet
【发布时间】:2014-10-16 01:32:06
【问题描述】:

我运行了一个执行许多 WMI 查询的脚本 - 但如果服务器没有应答,则 cmdlet 会挂起。 如果 X 秒过去了,有什么方法可以让这个(或任何其他 cmdlet)超时并退出?

编辑

感谢 mjolinor 的提示,解决方案是将其作为 -asjob 运行并在 while 循环中设置超时。但这已经从作业中运行(从 Start-Job 开始)。那么我怎么知道我在控制正确的工作呢?

这是我已经开始的工作中的代码:

Get-WmiObject Win32_Service -ComputerName $server -AsJob

$Complete = Get-date

While (Get-Job -State Running){
    If ($(New-TimeSpan $Complete $(Get-Date)).totalseconds -ge 5) {
        echo "five seconds has passed, removing"
        Get-Job  | Remove-Job -Force
    }
    echo "still running"
    Start-Sleep -Seconds 3
}

PS:我从 Start-Jobs 开始的工作已经处理好了..

【问题讨论】:

    标签: powershell wmi


    【解决方案1】:

    您可以尝试发布here 的get-wmiCustom 函数。如果 get-wmiObject 有超时参数不是很好吗?让我们为this thing点赞吧。

    【讨论】:

      【解决方案2】:

      我已修改 Daniel Muscetta 的 Get-WmiCustom 以也支持传递凭据。

      我知道这篇文章有点老了,希望这对其他人有所帮助。

      # Define modified custom get-wmiobject for timeout with credential from http://blogs.msdn.com/b/dmuscett/archive/2009/05/27/get_2d00_wmicustom.aspx
      Function Get-WmiCustom([string]$Class,[string]$ComputerName,[string]$Namespace = "root\cimv2",[int]$Timeout=15, [pscredential]$Credential) 
      { 
          $ConnectionOptions = new-object System.Management.ConnectionOptions
          $EnumerationOptions = new-object System.Management.EnumerationOptions
      
          if($Credential){
              $ConnectionOptions.Username = $Credential.UserName;
              $ConnectionOptions.SecurePassword = $Credential.Password;
          }
      
      
          $timeoutseconds = new-timespan -seconds $timeout 
          $EnumerationOptions.set_timeout($timeoutseconds)
      
          $assembledpath = "\\$Computername\$Namespace"
          #write-host $assembledpath -foregroundcolor yellow
      
          $Scope = new-object System.Management.ManagementScope $assembledpath, $ConnectionOptions 
          $Scope.Connect()
      
          $querystring = "SELECT * FROM " + $class 
          #write-host $querystring
      
          $query = new-object System.Management.ObjectQuery $querystring 
          $searcher = new-object System.Management.ManagementObjectSearcher 
          $searcher.set_options($EnumerationOptions) 
          $searcher.Query = $querystring 
          $searcher.Scope = $Scope
      
          trap { $_ } $result = $searcher.get()
      
          return $result 
      }
      

      【讨论】:

        【解决方案3】:

        很高兴我的 Get-WmiCustom 函数 http://blogs.msdn.com/b/dmuscett/archive/2009/05/27/get_2d00_wmicustom.aspx 很有用。

        【讨论】:

        • 非常有用。谢谢你这样做。
        【解决方案4】:

        使用 get-wmiobject 创建作业时,将该作业分配给一个变量,然后该变量可以通过管道传送到 get-job 以获取状态或接收作业以获取结果

        $ThisJob = start-job -scriptblock {param ($Target) Get-WmiObject -Class Win32_Service -ComputerName $Target -AsJob} -ArgumentList $server
        $Timer = [System.Diagnostics.Stopwatch]::StartNew()
        While ($ThisJob | Get-Job | where {$_.State -imatch "Running"}){
            If ($Timer.Elapsed.Seconds -ge 5) {
                echo "five seconds has passed, removing"
                $ThisJob | Get-Job | Remove-Job -Force
                } # end if
            echo "still running"
            Start-Sleep -Seconds 3
            } # end while
        
        $Results = $ThisJob | where {$_.State -inotmatch "failed"} | receive-job
        $Timer.Stop | out-null
        

        【讨论】:

          【解决方案5】:

          对于这个问题,我看到的仅有的两个解决方案是:

          1. 将查询作为后台作业运行并为其设置计时器,然后停止/删除运行时间过长的作业。

          2. 修复您的服务器。

          【讨论】:

          • 谢谢!我已经在工作中运行它(使用 start-job -scriptpath)。我还应该在开始工作中做 -asjob 吗?
          • 我认为嵌套作业没有任何帮助。要使计时器工作,您需要在每台服务器上运行一项作业。这将为每个创建作业增加几秒钟的开销,但您可以一次运行多个作业。您需要编写一个过程来保持 x 数量的作业同时运行,每个服务器一个,然后在它们完成或它们的 ET 太长时接收或取消作业,然后删除该作业并启动一个新作业从列表中的下一个服务器。
          • get-wmiobject 查询只是我在工作中做的许多事情之一,但是如果 get-wmiobject 没有及时回答,工作就会挂起。我会尝试做一个 get- wmiobject -asjob 并等待作业完成继续。也许不是最佳做法,但我认为没有其他办法。
          【解决方案6】:

          除了上面所说的,这不是一个防弹的解决方案,而是考虑先 ping 您的服务器 (Test-Connection),如果您没有响应的机器,它可以加快执行时间。

          【讨论】:

            猜你喜欢
            • 2021-10-30
            • 1970-01-01
            • 1970-01-01
            • 2011-09-04
            • 2017-08-20
            • 1970-01-01
            • 2014-07-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多