Solved

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

Posted on 2015-01-15
4
83 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_
  • 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 69

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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