Showing posts with label Test-NetConnection. Show all posts
Showing posts with label Test-NetConnection. Show all posts

Friday, October 27, 2017

Powershell script to ping multiple servers at the same time.

Powershell script to ping multiple servers at the same time.
Warning: This script is resource intensive as it starts a powershell instance for each of the jobs. If you have multiple servers then it will take all up all the memory until it finishes all the jobs.

$scriptDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$serverlist = GC "$scriptdir\list.txt"
$outputpath =  "$scriptdir\pingcheck.txt"



foreach($server in $serverlist)
{

  Start-job    -scriptblock {
                                                                         param([string]$computer,$outputpath)
                                                                         $out = Test-NetConnection -ComputerName $computer
                                                                         if($out.pingsucceeded -eq "True")
                                                                         {
                                                                         $output = "$computer" + "`t" + $out.remoteaddress + "`t" + " is pinging"
                                                                       
                                                                         }
                                                                         Else
                                                                         {
                                                                         $output = "$computer" + "`t" + $out.remoteaddress + "`t" + " is not pinging"
                                                                         Out-File -InputObject $output -FilePath $outputpath -Append
                                                                         }
                                                                         Sleep -Seconds 10
 return $output

                                                                   } -ArgumentList @($server,$outputpath)

 }

$A = Get-Job | Wait-Job | Receive-Job
Write-host "Writing A" -ForegroundColor Yellow
$A
Out-File -InputObject $A -FilePath $outputpath -Append 
Get-job | Remove-Job

Tuesday, April 25, 2017

How to test if a port is open using powershell


Use this Powershell command to to test if a port is open on a server and if you are able to connect to it remotely.

$tcp = New-Object System.Net.Sockets.TcpClient
$tcp.connect('servername or ip address', portnumber)

Or you can use this single line of code
(New-Object System.Net.Sockets.TcpClient).Connect('servername or ip address', portnumber)

There is also an in built powershell cmdlet that lets you test a port connection which only works with powershell 4.0

Test-NetConnection -Port portnumber -Computername 'Servername or ip address'

The first commands are much faster compared to the second one but does not give you any output.
The powershell cmdlet does give you a good output that you can use in another script but is much slower.