Solved

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

Posted on 2015-01-15
4
85 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 …
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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