Author Posts

March 2, 2016 at 7:19 am

How would I set a timeout for this function please?

function Discovery {
					if (Get-Service -ComputerName $ -name "*DiscoveryClientAgent*")
                    {Write-host "Query Discovery"
                        $Discovery = Get-WMIObject -class Win32_Service -computerName $ | Where-Object { $ -like "*DiscoveryClientAgent*" } | Select-Object StartMode,State
                        if ($Discovery.StartMode -eq "Auto"){
                        $Global:DiscoveryInstalled = $Discovery.State 
                        {$Global:DiscoveryInstalled = $Discovery.StartMode}

March 2, 2016 at 7:23 am

You mean, if Get-Service or Get-WmiObject take a long time to return? You can't, really – the client-side of those protocols are more or less hardcoded. You can technically tweak the WMI one, but it's STRONGLY discouraged as it can break some other things.

March 2, 2016 at 7:30 am

Thanks for your response
yes I use a number of functions like this to determine tools status on customer servers. My script works pretty well but occasionally freezes on some server or other. It doesn't take a long time it just halts

March 2, 2016 at 7:36 am

That's just a joy of WMI and DCOM, unfortunately. They're old technologies and largely deprecated. If your computers have WMF3 or higher, you should switch to CIM.

March 2, 2016 at 7:52 am

No need to use Get-Service or Where-Object, using –Filter could be faster. If you know the service's exact name, change
{Name LIKE '%DiscoveryClientAgent%'} to {Name = 'DiscoveryClientAgentExactName'}

If (Test-Connection -ComputerName $ -Count 1 -Quiet -TimeToLive 5)
{Get-WmiObject -Class win32_service -Filter {Name LIKE '%DiscoveryClientAgent%'}}

March 2, 2016 at 8:45 am

Doesn't that just timeout if the connection doesn't work? my problem is that it starts running on the computer and then halts when running one of the functions

March 2, 2016 at 9:11 am

That "halt" is likely a hang in the remote WMI service, and it's not something you can easily deal with from your end, unfortunately. As I said, moving to CIM – which is newer, and actively being developed and supported – can help resolve that significantly. WMI, for better or worse, kinda is what it is at this point. Microsoft isn't investing in that protocol stack, or the underlying service, any more. What you're running into is neither unusual nor especially solvable.

Sometimes (rarely, and mainly on Win2003/XP computers), the problem can be because of a corrupted WMI repository, which can require a complete repository rebuild. That's not something you do lightly, though, since it carries some risks of its own. But if its a particular machine that's hanging consistently, it can be worth exploring.

When I *have* to use WMI, I tend to feed all my computer names to Get-WmiObject at once, and use the -AsJob parameter. That'll spawn a job, and my script can Wait-Job for them all to stop running. That way, a hung job doesn't hang the entire script, or keep other computers from completing. It means a change in how you thin about processing results and whatnot, but it can help parallelize things a little bit, making the hangup less of a serious impact.

March 4, 2016 at 6:20 am

thanks for the reply but im out of my depth on this one. I inherited the script and have very little experience