-
Notifications
You must be signed in to change notification settings - Fork 6
/
VMPing.ps1
59 lines (41 loc) · 1.87 KB
/
VMPing.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
Function VMPing {
[CmdletBinding(DefaultParameterSetName="1")]
param(
[parameter(position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyname=$True,Mandatory=$True)]
$VM,
[parameter(parametersetname=1)][Alias('n')][int]$Count = 4,
[parameter(parametersetname=2)][Alias('t')][switch]$Continuous,
[ValidateRange(50,10000)][int]$Delayms = 700,
[Alias('w')][int]$Timeout = 800,
[switch]$enableIPv6
)
TRY {
IF (!($VM -as [VMware.VimAutomation.ViCore.types.V1.Inventory.VirtualMachine]) -as [bool]) {
$VMnotFound = $VM
$VM = Get-VM $VM -ErrorAction SilentlyContinue
IF (!$VM) {throw "$VMnotFound not found"}
}
IF ($VM.powerstate -eq "poweredon") {
$IP = $VM.ExtensionData.guest.net.ipaddress
IF (!$enableIPv6) {$IP = $IP | where {($_ -as [ipaddress]).AddressFamily -eq "InterNetwork"}} #exclude IPv6 addresses
$ping = New-Object system.Net.NetworkInformation.Ping
$r = 0
while ($r -lt $count) {
$Table = @()
$IP | ForEach-Object {
$Result = $ping.Send($_,$Timeout)
[pscustomobject]@{
VM = $VM.name
IP = $_
Time = IF ($result.Status -eq "Success") {$result.RoundtripTime} ELSE {$result.Status}
TTL = $result.Options.Ttl
}
}
Sleep -Milliseconds $Delayms
IF (!$Continuous) {$r++}
}
} ELSE {Write-Warning "$($VM.name) is powered off"}
} CATCH {
Write-Warning $_.Exception -ErrorAction Stop
}
}