Pages

1/02/2014

Get-WmiCustom (aka: Get-WMIObject with timeout!) - musc@> $daniele.work.ToString() - Site Home - MSDN Blogs

Get-WmiCustom (aka: Get-WMIObject with timeout!) - musc@> $daniele.work.ToString() - Site Home - MSDN Blogs: Unfortunately, the Get-WmiObject cmdlet does not let you specify a timeout. Therefore I cooked my own function which has a compatible behaviour to that of Get-WmiObject, but with an added “-timeout” parameter which can be set.

Function Get-WmiCustom([string]$computername,[string]$namespace,[string]$class,[int]$timeout=15) 
{ 
$ConnectionOptions = new-object System.Management.ConnectionOptions 
$EnumerationOptions = new-object System.Management.EnumerationOptions 

$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 
}

No comments: