Overview
In this post / snippet we will demonstrate, check server status utilizing ping command result output and writing the output to a “CSV” file.This script will read the server name(s) in the file and check each server status and loop through and also this script can be utilized to check one server, wherein server names list is not needed.
Applies To
Pre-Requisites
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. |
- To check multiple servers, ServersList.txt has to be created with list of servers that needs to be checked.
- ServersList.txt file should contain, only one server name each line.
- For Single Server, "ServersList.txt" is not necessary.
PowerShell Script - Check Server’s Status
This script will test a server or servers listed in “ServersList.txt” file, wherein it will ping and if the server is alive it will write to output CSV file “Host Name” and “Up / Down”.Code Snippet
# # Declare Variables # $OutputFileName="c:\temp\Server_Status.csv" # # Delete Output file # if (Test-Path $OutputFileName ){ Clear-Host Write-Host "Output File already Exists, deleting output file now - $OutputFileName.`n" -ForegroundColor Black -BackgroundColor Green Remove-Item $OutputFileName [console]::Beep(500,700) } # # Validate to check Single or Muliple servers # $Check_Type=Read-Host "Do you want to Check status of (S)ingle or (M)ultiple Servers?" if ($Check_Type -eq "S" -and $Check_Type -ne "") { $ServerName=Read-host "Enter Computer Name" if (Test-Connection -ComputerName $ServerName -Count 1 -ErrorAction SilentlyContinue) { # Write Column Heading delimited by Tab # echo "Host Name`tNode Status" > $OutputFileName # # Write Node Up, delimited by tab echo "$ServerName`tUp" >> $OutputFileName } else { # Write Column Heading delimited by Tab # echo "Host Name`tNode Status" > $OutputFileName # # Write Node Down, delimited by tab echo "$ServerName`tDown" >> $OutputFileName } # # Launch Ping Status Output File # Write-Host "Task completed, launching file" Invoke-Item $OutputFileName } elseif ($Check_Type -eq "m" -and $Check_Type -ne "") { $ServersList="c:\temp\ServersList.txt" # Get Servers List # $ServerNames = Get-Content $ServersList # # # Write Column Heading delimited by Tab # echo "Host Name`tNode Status" > $OutputFileName # # Loop Each Server and do ping test once # foreach ($ServerName in $ServerNames) { $StartCount = 0 $TotalCount = (Get-Content $ServersList | Measure-Object | Select-Object -ExpandProperty Count) # # Test Node reachability (ping) # if (Test-Connection -ComputerName $ServerName -Count 1 -ErrorAction SilentlyContinue) { # Write Node Up, delimited by tab echo "$ServerName`tUp" >> $OutputFileName } else { # Write Node Down, delimited by tab echo "$ServerName`tDown" >> $OutputFileName } $StartCount++ # # Progress Bar # Write-Progress -Activity "Collate Host Status" -Status "Pinging Hosts..." -PercentComplete ($StartCount / $TotalCount *100) } # # Launch Ping Status Output File # Write-Host "Task completed, launching file" Invoke-Item $OutputFileName } else { Write-Host "Invalid Option`n" -BackgroundColor Red -ForegroundColor Green [console]::Beep(600,700) }
PowerShell Output
In this example we have defined 3 servers and hence the server check loop will run 3 times only.Single Server - Output
To test single server, user has to key-in “Host IP / Hostname”.Multiple Servers - Output
To test multiple servers, server's to be tested are read from “ServersList.txt” file.Slideshare Information
Step by step guide with screenshots is uploaded.
No comments:
Post a Comment