Timeout for function

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Davey Boy Davey Boy 9 months ago.

  • Author
    Posts
  • #35933
    Profile photo of Davey Boy
    Davey Boy
    Participant
    How would I set a timeout for this function please?
    
    function Discovery {
    					if (Get-Service -ComputerName $objItem.name -name "*DiscoveryClientAgent*")
                        {Write-host "Query Discovery"
                            $Discovery = Get-WMIObject -class Win32_Service -computerName $objItem.name | Where-Object { $_.name -like "*DiscoveryClientAgent*" } | Select-Object StartMode,State
                            if ($Discovery.StartMode -eq "Auto"){
                            $Global:DiscoveryInstalled = $Discovery.State 
                            }
                            ELSE
                            {$Global:DiscoveryInstalled = $Discovery.StartMode}
                        }
    
  • #35934
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #35935
    Profile photo of Davey Boy
    Davey Boy
    Participant

    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

  • #35936
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #35937
    Profile photo of random commandline
    random commandline
    Participant

    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 $objItem.name -Count 1 -Quiet -TimeToLive 5)
    {Get-WmiObject -Class win32_service -Filter {Name LIKE '%DiscoveryClientAgent%'}}
    
  • #35940
    Profile photo of Davey Boy
    Davey Boy
    Participant

    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

  • #35942
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #36054
    Profile photo of Davey Boy
    Davey Boy
    Participant

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

You must be logged in to reply to this topic.