/
Test-Ping.ps1
51 lines (51 loc) · 1.58 KB
/
Test-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
#Pings all VMs on the specified ESXi host, comparing against previous results if they exist
param
(
[alias("v")]
[string]$vmHost = $(read-host -prompt "Enter the ESXi hostname"),
[alias("r")]
[string]$results = "E:\temp\Ping-test.txt",
[alias("o")]
[switch]$overwrite,
[alias("a")]
[switch]$append
)
if ($overwrite -and $append){write-error "Cannot Append and Overwrite; specify up to one option."; exit 20}
$allVMs = get-vmhost $vmHost | get-vm | where {$_.PowerState -eq "PoweredOn"}
$outResults = @()
$saveData = $false
if (!($overwrite) -and (test-path $results)){$outResults = get-content $results}
if ($overwrite -or $append -or !(test-path $results)){$saveData = $true}
$newDownSystems = @()
foreach ($thisVM in $allVMs)
{
if(!($thisVM.guest.hostname) -or !(Test-Connection $thisVM.guest.hostname -count 1 -quiet))
{
#If the results file doesn't exist or if it's set to save to the file, record the ping results. Otherwise, compare against the results
if ($saveData)
{
write-host "Ping failure: $($thisVM.name)"
$outResults += "Ping failure: $($thisVM.name)"
}
else
{
if (!(select-string $results -pattern $thisVM.name))
{
write-host "$('!'*10) Ping failure: $($thisVM.name)" -foregroundcolor red
$newDownSystems += $thisVM.Name
}
else
{
write-host "(expected) Ping failure: $($thisVM.name)"
}
}
}
}
if ($outResults){$outResults | select -unique | sort | set-content $results}
if ($newDownSystems)
{
write-host ""
write-host "Unexpectedly Down Systems"
write-host "$('='*25)"
$newDownSystems
}