Solved

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

Posted on 2015-01-15
4
86 Views
Last Modified: 2015-01-15
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
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40551416
Add a test-connection to your script so thatif the computer is not responding it goes to the next.
0
 

Author Comment

by:toshi_
ID: 40551423
hello becraig.
thank you for your answer.

Could you drop a line on how to do that ?

Thank you in advance.
toshi
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40551442
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
 

Author Comment

by:toshi_
ID: 40552944
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article will help you understand what HashTables are and how to use them in PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question