[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

in powershell how can i stop a query after 4 secondes ?

Hello,

I have a function which collects some informations on machines over the network. the function receives a list of hostnames as parameters then get into a loop and check over the network.

When the machine is up and running the answers comes in less than 1 second.
When the machine is down the process  last at least 20 seconds.

I'd like to stop the process after 4 seconds, how can i change the following code ?
function Get_Tshr_WhatTestingMachineHasTester( [string[]]$arrayOfMachinesNames= $(Get_tshr_MachinesNames_OutOfDataFile_InArray) ) 
{
	foreach($aHostName in $arrayOfMachinesNames) 
    { 
        
        $sw = [Diagnostics.Stopwatch]::StartNew()
         try {
         
            $Users = Get-WmiObject  win32_computersystem -ComputerName $aHostName -ErrorAction Stop | select Username,Caption,Manufacturer

	         If (($Users | %{$_.Username}) -eq $null){
	          New-Object PSObject -Property @{
                Server = $aHostName
		        Users = "None"
		        Manufacturer = $Users.Manufacturer
                RTotal = TotalRAM $aHostName
                RFree = FreeRAM $aHostName
		        }
	         }Else{
	          New-Object PSObject -Property @{
                Server = $aHostName
		        Users = ($Users | %{$_.Username}) -join ","
		        Manufacturer = $Users.Manufacturer
                RTotal = TotalRAM $aHostName
                RFree = FreeRAM $aHostName
		        }
	         }
         }
         catch
         {
	          New-Object PSObject -Property @{
                Server = $aHostName
                Users = $_.exception.message
		        Manufacturer = ".."
                RTotal = ".."
                RFree = ".."
	        }
         }
         $sw.Stop()
         Write-Host "time-elapsed : " -NoNewline
         Write-Host $sw.Elapsed  -ForegroundColor Cyan

	}
}

Open in new window


thank you in advance four your help.
toshi
0
toshi_
Asked:
toshi_
  • 2
1 Solution
 
becraigCommented:
Add a test-connection to your script so thatif the computer is not responding it goes to the next.
0
 
toshi_Author Commented:
hello becraig.
thank you for your answer.

Could you drop a line on how to do that ?

Thank you in advance.
toshi
0
 
QlemoC++ DeveloperCommented:
Just test whether the remote machine is reachable, and only perform actions if it is:
if (Test-Connection aHostName -Count 1 -Quiet)
{
   # get-wmi etc.
}

Open in new window

0
 
toshi_Author Commented:
Thanks a lot Qlemo !

i could not figure out how to get a short answer and not all the error.
I was missing -Quiet parameter.


thanks a lot.

Toshi
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now