Invoke-VmkPing.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#requires -Version 1.0
#requires -PSSnapin VMware.VimAutomation.Core

<#PSScriptInfo
 
        .VERSION 0.1
 
        .GUID 58a4280b-fcf2-43bc-9dc9-b1da178da404
 
        .AUTHOR Chris Masters
 
        .COMPANYNAME Chris Masters
 
        .COPYRIGHT (c) 2018 Chris Masters. All rights reserved.
 
        .TAGS network vmware vsphere ping vmkping virtual
 
        .LICENSEURI
 
        .PROJECTURI https://www.powershellgallery.com/profiles/masters274/
 
        .ICONURI
 
        .EXTERNALMODULEDEPENDENCIES VMware.VimAutomation.Core
 
        .REQUIREDSCRIPTS
 
        .EXTERNALSCRIPTDEPENDENCIES
 
        .RELEASENOTES
        12/22/2018:
                0.1 - Day one release.
 
        .PRIVATEDATA
         
#>
 

<#
        .SYNOPSIS
        VMK Ping from PowerCli
 
        .DESCRIPTION
        This script allows you to test the connectivity of your virtual nics in vSphere. You no longer need to
        enable SSH to perform these tests. This script works great in an automated deployment strategy.
 
        .EXAMPLE
        Invoke-VmkPing -VMHost myHost.domain.local -Credential root -IPAddress 192.168.200.10
        This will perform a VMK ping to 192.168.200.10 from host myHost.domain.local, results will look similar to
        the following.
 
        Duplicated : 0
        HostAddr : 192.168.200.10
        PacketLost : 0
        Recieved : 3
        RoundtripAvgMS : 192
        RoundtripMaxMS : 221
        RoundtripMinMS : 168
        Transmitted : 3
 
        .EXAMPLE
        Invoke-VmkPing -VMHost myHost.domain.local -Credential root -IPAddress 192.168.200.10 -DFBit -Size 8972
        This will perform a VMK ping to 192.168.200.10 from host myHost.domain.local with the DF (don't fragment)
        bit set, and test that jumbo frames are configured properly, end to end. Don't forget about packet headers.
        We set the size to 8972, to test that our jumbo configuration of 9000 is working propery. Don't forget the
        -DFBit setting, otherwise it will always work no matter the size if connectivity is true.
 
        .NOTES
        Requires that you have VMware.VimAutomation.Core PSSnapin loaded.
 
        .LINK
        https://github.com/masters274/
        https://www.powershellgallery.com/profiles/masters274/
 
        .INPUTS
        Accepts a string value for API key and a string or array of strings for the ServiceTag parameter
 
        .OUTPUTS
        Provides PSObject with network stats, based on the results
         
        Duplicated : 0
        HostAddr : 192.168.200.10
        PacketLost : 0
        Recieved : 3
        RoundtripAvgMS : 192
        RoundtripMaxMS : 221
        RoundtripMinMS : 168
        Transmitted : 3
#>



Param
(
    [Parameter(Mandatory=$true, HelpMessage = 'VMHost you want to ping from')]
    [String] $VMHost, 
        
    [Parameter(Mandatory=$true, HelpMessage='Credentials for administering VMHost')]
    [System.Management.Automation.Credential()]
    [PSCredential] $Credential,
        
    [int] $Count = 3,
        
    [Switch] $DFBit, # set this when testing jumbo frames, or > 1500 packet size
        
    [Parameter(Mandatory = $true, HelpMessage = 'IP you want to ping for testing')]
    [IPAddress] $IPAddress,
        
    [ValidatePattern('^vmk*')]
    [String] $Interface = $null, # $null will pick the nic based on routing table, or interface subnet
        
    [int] $Size = 1500, # set to 8972 to test jumbo frames
        
    [Long] $TTL = $null
)
    
Begin
{
    
}
    
Process
{
    # Variables
    $strStopAction = 'Stop'
    
    # Connect to the VMHost
    Try
    {
        Connect-VIServer -Server $VMHost -Credential $Credential -WarningAction SilentlyContinue -ErrorAction $strStopAction | Out-Null
        $cmdESXcli = Get-EsxCli -VMHost $VMHost -ErrorAction $strStopAction
    }
    Catch
    {
        Write-Error -Message ('Failed to connect to VMHost {0}' -f $VMHost)
        return
    }
        
    #ping(long count, boolean debug, boolean df, string host, string interface, string interval, boolean ipv4, boolean ipv6, string netstack, string nexthop, long size, long ttl, string wait
    [Bool] $isIPv4 = $false
    [Bool] $isIPv6 = $false
     
    If ($IPAddress.AddressFamily -eq 'InterNetworkV6')
    {
        $isIPv6 = $true
    }
    Else
    {
        $isIPv4 = $true
    }
        
    $ret = $cmdESXcli.network.diag.ping(
        $Count,
        $false, # debugging
        $(If (!$DFBit) {$null} Else {$DFBit}), # Don't fragment bit
        $IPAddress,
        $null,
        $null, # String Interval
        $isIPv4,
        $isIPv6,
        $null, # [string] netstack
        $null, # [string] nexthop
        $Size, # Set to 8972 to test jumbo frames, also need DF bit set
        $(If (!$TTL) {$null} Else {$TTL}),
        $null # [String] wait
    )
        
    If ($ret.summary.PacketLost -gt 0)
    {
        Write-Warning -Message ('IP {0} not reachable, or missing packets!' -f $IPAddress)
    }
    
    $ret.summary
}
    
End
{
    Disconnect-VIServer -Server $VMHost -Force -Confirm:$false | Out-Null
}