PowerShell: Testing Connectivity of Multiple Workstations on Your Network

February 24, 2016 | Views: 5179

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

If you’re tasked with pinging a huge list of workstations, look no further. Gone are the days where we must keyboard cram in the command prompt to test connectivity.

First off, let’s get you started with PowerShell. PowerShell is a free program offered by Microsoft and it’s basically a command prompt on steroids.

It’s easy to find and easy to download. Get your hands on Version 3 at least – optimally PowerShell ISE v4.

The program is pretty self-explanatory. You can use all the same commands you use in the command prompt, as well as a whole plethora of new commands. PowerShell doesn’t just stop there. You can add new functions that are useful with your everyday applications. For instance, I was able to add functions in PowerShell that helped me manage SCCM.
All you have to do is look it up online.

So once you’ve downloaded it, you’re going to want to…

file -> new -> paste code (below) -> save as (make sure you save as .ps1 file)

Function Popup-Box($Message,$Title,$Button,$Icon)

{

Add-Type -AssemblyName Microsoft.VisualBasic

Return [Microsoft.VisualBasic.Interaction]::MsgBox(“$Message”,”$Button,MsgBoxSetForeground,$Icon”,”$Title”)

}

Function Open-File($Directory,$Filter,$Title)

{

Add-Type -AssemblyName System.Windows.Forms

#Commonly used file types that i allow to be abriviated when passing it to the function

If ($Filter.ToLower() -eq “all”)

{

$Filter = “All Files(*.*)|*.*”

}

If ($Filter.ToLower() -eq “txt”)

{

$Filter = “Text Files(*.txt)|*.txt”

}

If ($Filter.ToLower() -EQ “csv”)

{

$Filter = “Comma Separated Value(*.csv)|*.csv”

}

If ($Filter.ToLower() -EQ “txtcsv”)

{

$Filter = “Text Files(*.txt)|*.txt|Comma Separated Value(*.csv)|*.csv”

}

$Title = “$Title -DO NOT PRESS HELP OR THE SCRIPT WILL BECOME UNRESPONSIVE”

$SelectAFile = New-Object System.Windows.Forms.OpenFileDialog

$SelectAFile.InitialDirectory = $Directory

$SelectAFile.Filter = $Filter

$SelectAFile.Title = $Title

$SelectAFile.ShowHelp = $True

$SelectAFileChoice = $SelectAFile.ShowDialog()

Switch($SelectAFileChoice)

{

“Ok”

{

Return $SelectAFile.FileName

}

“Cancel”

{

$CancelCheck = Popup-Box -Message “Are You Sure You Want To Cancel?” -Title “You Pressed Cancel!” -Button “YesNo” -Icon “Information”

Switch($CancelCheck)

{

“yes”

{

Exit

}

“no”

{

Open-File -Directory $Directory -Filter $Filter -Title $Title.Replace(“-DO NOT PRESS HELP OR THE SCRIPT WILL BECOME UNRESPONSIVE”, “”)

}

}

}

}

}

Function Save-File($SaveFileTitle,$SaveFileFilter,$SaveFileInitialDirectory,$SaveFileName)

{

If ($SaveFileFilter -eq “all”)

{

$SaveFileFilter = “All Files(*.*)|*.*”

}

If ($SaveFileFilter -eq “txt”)

{

$SaveFileFilter = “Text Files(*.txt)|*.txt”

}

If ($SaveFileFilter -EQ “csv”)

{

$SaveFileFilter = “Comma Separated Value(*.csv)|*.csv”

}

If ($SaveFileFilter -EQ “txtcsv”)

{

$SaveFileFilter = “Text Files(*.txt)|*.txt|Comma Separated Value(*.csv)|*.csv”

}

$SaveFileTitle = “$SaveFileTitle -DO NOT PRESS HELP OR THE SCRIPT WILL BECOME UNRESPONSIVE”

$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog

$SaveFileDialog.CheckPathExists = $True

$SaveFileDialog.RestoreDirectory = $True

$SaveFileDialog.ShowHelp = $True

$SaveFileDialog.ValidateNames = $True

$SaveFileDialog.OverwritePrompt = $True

$SaveFileDialog.Title = $SaveFileTitle

$SaveFileDialog.Filter = $SaveFileFilter

$SaveFileDialog.InitialDirectory = $SaveFileInitialDirectory

$SaveFileDialog.FileName = $SaveFileName

$SaveFileDialogChoice = $SaveFileDialog.ShowDialog()

Switch($SaveFileDialogChoice)

{

“Ok”

{

Return $SaveFileDialog.Filename

}

“Cancel”

{

$CancelCheck = Popup-Box -Message “Are You Sure You Want To Cancel?” -Title “You Pressed Cancel!” -Button “YesNo” -Icon “Information”

Switch($CancelCheck)

{

“yes”

{

Exit

}

“no”

{

Save-File -SaveFileInitialDirectory $SaveFileInitialDirectory -SaveFileFilter $SaveFileFilter -SaveFileTitle $SaveFileTitle.Replace(“-DO NOT PRESS HELP OR THE SCRIPT WILL BECOME UNRESPONSIVE”, “”)

}

}

}

}

}

Remove-Job *

$JobList = @()

$SuccessfulPings = @()

$UnsuccessfulPings = @()

$ServerListPath = Open-File -Directory “C:” -Filter “TXT” -Title “Please Select The Server List File”

$Servers = Get-Content $ServerListPath

ForEach ($Server In $Servers)

{

$JobList += Start-Job -Scriptblock {

If (Test-Connection $Args[0] -Count 1 -Quiet)

{

$SuccessfulPings = “$($Args[0]) Yes”

Return $SuccessfulPings

}

Else

{

$UnsuccessfulPings = “$($Args[0]) No”

Return $UnsuccessfulPings

}

} -Argumentlist $Server

}

$JobTracker = “Not Finished”

Do {

$JobList | ForEach {

If ($_.State -EQ “Completed”)

{

If ($(Receive-Job $_ -Keep) -Match “Yes”)

{

$SuccessfulPings += $(Receive-Job $_).Trimend(” Yes”)

Remove-Job $_

}

ElseIf ($(Receive-Job $_ -Keep) -Match “No”)

{

$UnsuccessfulPings += $(Receive-Job $_).Trimend(” No”)

Remove-Job $_

}

}

}

$JobTracker = $(Job)

}Until ($JobTracker -EQ $Null)

If ($UnsuccessfulPings)

{

$UnsuccessfulPath = Save-File SaveFileTitle “Failed Ping Export File” -SaveFileFilter “TXT” -SaveFileInitialDirectory “C:” -SaveFileName “Failed Ping Export”

If ($UnsuccessfulPath)

{

Set-Content -Path $UnsuccessfulPath -Value $UnsuccessfulPings

}

}

If ($SuccessfulPings)

{

$SuccessfulPath = Save-File SaveFileTitle “Successful Ping Export File” -SaveFileFilter “TXT” -SaveFileInitialDirectory “C:” -SaveFileName “Successful Ping Export”

If ($SuccessfulPath)

{

Set-Content $SuccessfulPath -Value $SuccessfulPings

}

}

 


You made it! Don’t copy this text, only the bracket above this.

Once you’ve done that, make a text file in the same location, put one workstation name or IP per line in the document… Nothing Else.

 

Like this:
workstationdfiushdfoh1293087

workstationfdksfjsdlfk2938029

… or …

192.168.1.1
192.168.1.2

 

That’s it!

Right-click the PowerShell script you made and “Run With PowerShell” and then select the .txt file with the workstations names/IPs in it. Let it do it’s thing (it’ll take a bit if there are a bunch of workstations/IPs in your list). Eventually, it will prompt you to let it output “Failed Ping List” and “Successful Ping List” – just hit save on both of those. Open those files and it’ll show you which ones are connected to the network and which ones aren’t!

GLHF

– Sheridan

 

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
4 Comments
  1. How can this be modified for NSLOOKUPs rather than PINGs?

  2. No problem with the code block…Awesome effort, thanks for sharing!

  3. Sorry for the long block of code guys!

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 

Cybrary|0P3N

Is Linux Worth Learning in 2020?
Views: 331 / December 14, 2019
How do I Get MTA Certified?
Views: 923 / December 12, 2019
How much does your PAM software really cost?
Views: 1376 / December 10, 2019
How Do I Get into Android Development?
Views: 1754 / December 8, 2019

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel