Skip to content

Instantly share code, notes, and snippets.

@MichaelRyom
Created April 14, 2018 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelRyom/c7aaa47b31c65d4993a7f046d0c20d83 to your computer and use it in GitHub Desktop.
Save MichaelRyom/c7aaa47b31c65d4993a7f046d0c20d83 to your computer and use it in GitHub Desktop.
$VMHosts = Get-Cluster "Test01" | Get-VMHost
[INT]$Time = "86400" #24 Hours
$Report = @()
#Start running through all hosts in the vCenter
foreach($VMhost in $VMHosts){
#$Resp is used to get responed from try/catch command for getting vlan stats status
$Resp = ""
#Test if vmnic0 is enabled for vlan stats – if true it presumed that all vmnic’s on the host is enabled
try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
#If $Resp is NOT NULL go into if and enable vlan stats for all nics on this host
if($resp){
$vmnics = ""
$vmnics = $VMhost | Get-VMHostNetworkAdapter -Physical | %{($esxcli.network.nic.vlan.stats.set($TRUE,$_.Name) | Select Vlanid)}
#Check again to see if vlan stats are now enabled
$Resp = ""
try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
}#If $Resp is not NULL – vlan stats hasn’t been enabled! Error, Abort, Abort, ABORT!
elseif($resp){
#Write to prompt a useful error message
Write-host $VMhost.Name "Says: ARRRRGGG PANIC"
}}
#Count down and sleep for 24 hours (86400 seconds – Specified in $Time variable) – The amount of time specified is that amount of time that data is collected for – Please do not use small # as the result WILL be INACCURATE!
for ($a=$Time; $a -gt 1; $a–) {
$PerCent = [Math]::Round($a/$Time*100,2)
Write-Progress -Activity "Collecting VLAN Stats…" -SecondsRemaining $a -CurrentOperation "$PerCent% complete" -Status "Please wait."
Start-Sleep 1
}
foreach($VMhost in $VMHosts){
#$Resp is used to get responed from try/catch command for getting vlan stats status
$Resp = ""
#Test if vmnic0 is enabled for vlan stats – if true it presumed that all vmnic’s on the host is enabled
try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
#If vlan stats are enabled – $Resp will be NULL and we go into the if statement.
if(([string]::IsNullOrEmpty($Resp))){
$Details = "" | Select Host,VlanDiff
$dvsVlan = (($VMHost | Get-VirtualPortGroup)| Select Name, @{N="VLANId";E={[INT]$_.Extensiondata.Config.DefaultPortCOnfig.Vlan.VlanId}} | Where {$_.vlanid -ne $NULL} | Sort VlanID | Group-Object Vlanid).name
$vmnics = ""
$vmnics = ($VMhost | Get-VMHostNetworkAdapter -Physical | %{($esxcli.network.nic.vlan.stats.get($_.Name) | Select Vlanid)}) | Group-Object Vlanid | Select Name
$Details.Host = $VMHost.name
$Details.VLanDiff = Compare-Object -ReferenceObject ($vmnics | Sort Name).Name -DifferenceObject $dvsVlan -IncludeEqual | where {$_.SideIndicator -match "=>"} | Select InputObject
$Report += $Details
}
}
#Disable vlan stats again
#Start running through all hosts in the vCenter
foreach($VMhost in $VMHosts){
#$Resp is used to get responed from try/catch command for getting vlan stats status
$Resp = ""
#Test if vmnic0 is disabled for vlan stats – if true it presumed that all vmnic’s on the host is disabled
try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
#If $Resp is NULL go into if and enable vlan stats for all nics on this host
if([string]::IsNullOrEmpty($Resp)){
$vmnics = ""
Write-Host $VMhost.Name":"
$VMhost | Get-VMHostNetworkAdapter -Physical | %{($esxcli.network.nic.vlan.stats.set($FALSE,$_.Name))}
#Check again to see if vlan stats are now disabled
$Resp = ""
try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
}#If $Resp is NULL – vlan stats hasn’t been disabled! Error, Abort, Abort, ABORT!
elseif(([string]::IsNullOrEmpty($Resp))){
#Write to prompt a useful error message
Write-host $VMhost.Name "Says: ARRRRGGG PANIC"
}
}
$Report | select Host -ExpandProperty vlandiff | Sort Host,InputObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment