1

I have a script in powershell to hit each server in a list and check if it is online or not. As it was time consuming, I wrote a thread logic to call that script, My script works fine. But I want to split my good/bad servers into two different text files, I cannot hard code it in my script as obviously resource might be used by another thread. If I write it in thread logic my output (both good and bad) will be in the same file. How can I nicely format only desired output?

### Start-MultiThread.ps1 ###
$Servers =Get-Content -Path "C:\Scripts\Servers.txt"

#Start all jobs
ForEach($Server in $Servers){
    Start-Job -FilePath "C:\Scripts\ChkOnline.PS1" -ArgumentList $Server
}

#Wait for all jobs
Get-Job | Wait-Job

#Get all job results
Get-Job | Receive-Job |Out-File -FilePath "C:\Scripts\Output.txt"


##ChkOnline.PS1###

Param($Server = "ServerNameHolder")
$PingStatus= Test-Connection $Server -Quiet

              If ($PingStatus -eq 1)
                        {
                          Return $Server " is online!!"
                        }
            Else
                        {
                          Return $Server " is offline!"
                        }
4
  • 4
    Can you post your script? Commented Nov 11, 2013 at 20:36
  • Thanks for looking... I have posted Commented Nov 12, 2013 at 19:36
  • What's your reason for doing this with jobs/multithreading? Commented Nov 12, 2013 at 19:42
  • You can speed this up massively by using "Test-Connection" with the "-Count 1" parameter and value. Commented Nov 12, 2013 at 20:35

2 Answers 2

1

You can do this without starting multiple jobs. Starting multiple jobs incurs memory & processing time overhead and for a task like this, it's more overhead than it's worth.

$Servers = Get-Content -Path "C:\Scripts\Servers.txt"
$results = Test-Connection -ComputerName $servers -Count 1 -ErrorAction silentlycontinue;
$AvailableServers = $results|select -expandproperty address
$OfflineServers = Compare-Object -ReferenceObject $Servers -DifferenceObject $AvailableServers -PassThru;
$AvailableServers | out-file c:\scripts\onlineservers.txt;
$OfflineServers | out-file c:\scripts\offlineservers.txt;
Sign up to request clarification or add additional context in comments.

4 Comments

I would still use the technique I posted to filter out off the offline servers. Then, if needed, spawn a job for each server to get your other health data. That job would probably return a custom PSObject that hols all of your health check data (and probably collections of other objects representing it). But "server health check" is a solved problem already - a lot of PowerShell pros/bloggers/MVPs have posted such things, and you should look to those first. You may find that they've not found a need to use jobs, and may have already written a script you can use with minimal changes.
But before you go writing a script that spawns 100 extra jobs, consider carefully whether you need this to be multithreaded in the first place. It's a good option to have in your back pocket, but don't use it unless you actually need it.
Thanks for the inputs.The reason why I chose to multithread is I hope to add few more health checks in future, to my script like services health and DB health checks. In that case either I have to dump all data to a single file or I should design my script to give me only failed server/services/DB statuses?
I really wish you hadn't deleted your comments/questions that I was responding to, as I was responding directly to them. And now you've re-asked the questions, but slightly changing them, changing the context.
0

Using the -asjob parameter (multi-process) makes it very fast. The ones that are up will have a non-null ResponseTime property. Unfortunately, test-connection displays the ResponseTime property as 'Time(ms)' by default. The targetname can be an array.

$servers = 'microsoft.com','yahoo.com'
$a = test-connection $servers -AsJob | Receive-job -Wait -AutoRemoveJob

$a | where responsetime # up

Source        Destination     IPV4Address      IPV6Address  Bytes    Time(ms)
------        -----------     -----------      -----------  -----    --------
DESKTOP-JQ... yahoo.com       98.138.219.231                32       65


$a | where { ! $_.responsetime } # down

Source        Destination     IPV4Address      IPV6Address  Bytes    Time(ms)
------        -----------     -----------      -----------  -----    --------
DESKTOP-JQ... microsoft.com   40.112.72.205                 32

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.