-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.ps1
67 lines (56 loc) · 1.44 KB
/
ping.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<#
Example usage: .\ping.ps1 -JSON '["google.com","bing.com"]'
INPUT:
JSON Array of FQDNs of servers to ping
Example: ["google.com","bing.com"]
OUTPUT:
JSON Array of objects containing the server fqdn (server) and whether or not the server was ping-able (up)
Example:
[
{
"server": "google.com",
"up": "False"
},
{
"server": "bing.com",
"up": "False"
}
]
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$JSON
)
try {
# Check we're receiving valid format JSON
$objects = $JSON | ConvertFrom-Json -ErrorAction Stop
# Check to make sure we only receive a string or list/array of strings
$objects | ForEach-Object {
if ($_.GetType().Name -ne "String") {
throw "Expected Object type String. Got Type $($_.GetType().Name)"
}
}
}
catch {
"Error parsing JSON" | Write-Debug
$Error[0] | Write-Error
exit
}
$output = [System.Collections.ArrayList]@()
$objects | ForEach-Object {
try {
$pingResult = Test-NetConnection -ComputerName $_ -ErrorAction Stop -WarningAction Stop
}
catch {
"Test-NetConnection failed. Trying Test-Connection as backup" | Write-Debug
$pingResult = Test-Connection -ComputerName $_ -ErrorAction Stop -Quiet
}
$null = $output.Add(
[PSCustomObject]@{
server = $_
up = $pingResult.ToString()
}
)
}
$output | ConvertTo-Json