Solved

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

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

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now