1. How To Check Server Status - PowerShell
i | P a g e
Table of Contents
Overview ...........................................................................................................................................1
Applies To......................................................................................................................................1
Pre-Requisites ................................................................................................................................1
PowerShell Script - Check Server’s Status.............................................................................................1
Code Snippet..................................................................................................................................1
PowerShell Output..........................................................................................................................2
2. How To Check Server Status - PowerShell
1 | P a g e
Overview
In thispost / snippetwe will demonstrate, checkserverstatusutilizingpingcommandresultoutputand
writing the output to a “CSV” file.
This script will read the server name(s) from a file and loop through.
Applies To
Tested on Windows 10, Windows 2008 R2, Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”,youcan get current execution policy by running the command; “Get-ExecutionPolicy”.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
PowerShell Script - Check Server’s Status
Thisscriptwill testserverslistedin“ServersList.txt”file,whereinitwill pingandif the serverisaliveitwill
write output to CSV file “Host Name” and “Up / Down”.
Code Snippet
#
# Declare Variables
#
$OutputFileName="Server_Status.csv"
$ServersList="c:tempServersList.txt"
#
# Readhostname(s) fromfile
#
$ServerNames =Get-Content$ServersList
#
# Write ColumnHeadingdelimitedbyTab
#
echo"Host Name`tNode Status" >$OutputFileName
3. How To Check Server Status - PowerShell
2 | P a g e
#
# LoopEach Serveranddo pingtestonce
#
foreach($ServerName in$ServerNames) {
$StartCount = 0
$TotalCount= (Get-Content$ServersList|Measure-Object|Select-Object-ExpandProperty Count)
#
# TestNode reachability
#
if ( Test-Connection -ComputerName $ServerName-Count1-ErrorAction SilentlyContinue) {
#
# Write Node Up, delimitedbytab
#
echo"$ServerName`tUp">>$OutputFileName
}
else {
#
# Write Node Down,delimitedbytab
#
echo"$ServerName`tDown">>$OutputFileName
}
$StartCount++
#
# ProgressBar
#
Write-Progress -Activity "CollateHostStatus"-Status"PingingHosts..." -PercentComplete
($StartCount/ $TotalCount*100)
}
#
# LaunchPingStatus OutputFile (CSV File)
#
Invoke-Item$OutputFileName
PowerShell Output
In this example we checked for 3 servers only.