Timeout Get-WMIObject cmdlet

后端 未结 6 1290
星月不相逢
星月不相逢 2020-12-06 15:00

I run a script which performs many WMI-querys - but the cmdlet hangs if the server doesn\'t answer.. Is there any way I can make this (or any other cmndlet for that matter)

相关标签:
6条回答
  • 2020-12-06 15:10

    Glad my Get-WmiCustom function here http://blogs.msdn.com/b/dmuscett/archive/2009/05/27/get_2d00_wmicustom.aspx is useful.

    0 讨论(0)
  • 2020-12-06 15:16

    The only two solutions I've seen for this problem are:

    1. Run the queries as background jobs and put a timer on them, then stop/remove the jobs that run too long.

    2. Fix your servers.

    0 讨论(0)
  • 2020-12-06 15:17

    I've modified Daniel Muscetta's Get-WmiCustom to also support passing credentials.

    I know this post is a little old, hopefully this helps someone else.

    # 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 
    }
    
    0 讨论(0)
  • 2020-12-06 15:19

    In addition to what has been said, not a bullet proof solution but consider pinging your servers first (Test-Connection), it can speed up execution time in case you have no responding machines.

    0 讨论(0)
  • 2020-12-06 15:24

    when creating the job using get-wmiobject assign that job to a variable, then that variable can be piped into get-job for status or receive-job for results

    $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
    
    0 讨论(0)
  • 2020-12-06 15:29

    You could try the get-wmiCustom function, posted here. Wouldn't it be nice if get-wmiObject had a timeout parameter? Let's upvote this thing.

    0 讨论(0)
提交回复
热议问题