Last active
August 29, 2015 14:26
-
-
Save istairbn/28cb3cbb8ce2e3564f02 to your computer and use it in GitHub Desktop.
A Powershell function that pings given host names and returns their status - basic machine monitoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function PingIt{ | |
<# | |
.SYNOPSIS | |
Pings a list of computers, returns if they are offline or online and provides MS return time and status code | |
.FUNCTIONALITY | |
Simply used as a status indicator of machines as opposed to applications | |
.DESCRIPTION | |
Puts out the status of the hosts requested | |
.PARAMETER Computers | |
The hosts you want monitored | |
.PARAMETER Wait | |
Length between pings | |
.PARAMETER Logscape | |
Boolean - Using with Logscape (Outputs strings instead of objects) | |
.EXAMPLE | |
PingIt -Computers HOST1,HOST2 | |
.OUTPUTS | |
String | |
.NOTES | |
Author: Ben Newton - Excelian | |
.LINK | |
http://github.com/istairbn | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)] | |
[string[]] | |
$Computers = @(), | |
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)] | |
[int] | |
$Wait = 60, | |
[Parameter(Mandatory=$False)] | |
[bool] | |
$Logscape = $False | |
) | |
$Now = Get-Date -Format "yyyy/mm/dd hh:MM:ss" | |
while(1){ | |
ForEach($Computer in $Computers){ | |
Try{ | |
If($IT = Test-Connection -ErrorAction SilentlyContinue -Count 1 -ComputerName $Computer -ErrorVariable Failure){ | |
If($Logscape){ | |
$String = $Now + " Server:" + $IT.Address +" MsResponse:" + $IT.ResponseTime +" State:Online Status:" + $IT.StatusCode +" IPV4:" + $IT.IPV4Address | |
Write-Output $String | |
} | |
Else{ | |
$Obj = New-Object -TypeName PSObject -Property @{"TimeStamp"=$Now;"Server"=$IT.Address;"MsResponse"=$IT.ResponseTime;"State"="Online";"StatusCode"=$IT.StatusCode;"IPV4Address"=$IT.IPV4Address;"IPV6Address"=$IT.IPV6Address } | |
Write-Output $Obj | |
} | |
} | |
else{ | |
If($Logscape){ | |
$String = $Now + " Server:" + $Computer + " Status: Offline" | |
Write-Output $String | |
} | |
Else{ | |
$Obj = New-Object -TypeName PSObject -Property @{"TimeStamp"=$Now;"Server"=$IT.Address;"State"="Offline"} | |
Write-Output $Obj | |
} | |
} | |
} | |
catch [System.Exception]{ | |
Write-Error $Failure | |
} | |
} | |
sleep $Wait | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to include a $Logscape parameter. If True, it will output a string (suitable for captured console output). If false (default) it will output a standard powershell object including the timestamp