Guest User

Untitled

a guest
Apr 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. $VMHosts = Get-Cluster "Test01" | Get-VMHost
  2. [INT]$Time = "86400" #24 Hours
  3. $Report = @()
  4.  
  5. #Start running through all hosts in the vCenter
  6. foreach($VMhost in $VMHosts){
  7. #$Resp is used to get responed from try/catch command for getting vlan stats status
  8. $Resp = ""
  9. #Test if vmnic0 is enabled for vlan stats – if true it presumed that all vmnic’s on the host is enabled
  10. try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
  11.  
  12. #If $Resp is NOT NULL go into if and enable vlan stats for all nics on this host
  13. if($resp){
  14. $vmnics = ""
  15. $vmnics = $VMhost | Get-VMHostNetworkAdapter -Physical | %{($esxcli.network.nic.vlan.stats.set($TRUE,$_.Name) | Select Vlanid)}
  16.  
  17. #Check again to see if vlan stats are now enabled
  18. $Resp = ""
  19. try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
  20. }#If $Resp is not NULL – vlan stats hasn’t been enabled! Error, Abort, Abort, ABORT!
  21. elseif($resp){
  22. #Write to prompt a useful error message
  23. Write-host $VMhost.Name "Says: ARRRRGGG PANIC"
  24. }}
  25.  
  26. #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!
  27. for ($a=$Time; $a -gt 1; $a–) {
  28. $PerCent = [Math]::Round($a/$Time*100,2)
  29. Write-Progress -Activity "Collecting VLAN Stats…" -SecondsRemaining $a -CurrentOperation "$PerCent% complete" -Status "Please wait."
  30. Start-Sleep 1
  31. }
  32.  
  33. foreach($VMhost in $VMHosts){
  34. #$Resp is used to get responed from try/catch command for getting vlan stats status
  35. $Resp = ""
  36. #Test if vmnic0 is enabled for vlan stats – if true it presumed that all vmnic’s on the host is enabled
  37. try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
  38.  
  39. #If vlan stats are enabled – $Resp will be NULL and we go into the if statement.
  40. if(([string]::IsNullOrEmpty($Resp))){
  41.  
  42. $Details = "" | Select Host,VlanDiff
  43. $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
  44.  
  45. $vmnics = ""
  46. $vmnics = ($VMhost | Get-VMHostNetworkAdapter -Physical | %{($esxcli.network.nic.vlan.stats.get($_.Name) | Select Vlanid)}) | Group-Object Vlanid | Select Name
  47.  
  48. $Details.Host = $VMHost.name
  49. $Details.VLanDiff = Compare-Object -ReferenceObject ($vmnics | Sort Name).Name -DifferenceObject $dvsVlan -IncludeEqual | where {$_.SideIndicator -match "=>"} | Select InputObject
  50.  
  51. $Report += $Details
  52. }
  53. }
  54.  
  55. #Disable vlan stats again
  56. #Start running through all hosts in the vCenter
  57. foreach($VMhost in $VMHosts){
  58. #$Resp is used to get responed from try/catch command for getting vlan stats status
  59. $Resp = ""
  60. #Test if vmnic0 is disabled for vlan stats – if true it presumed that all vmnic’s on the host is disabled
  61. try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
  62.  
  63. #If $Resp is NULL go into if and enable vlan stats for all nics on this host
  64. if([string]::IsNullOrEmpty($Resp)){
  65. $vmnics = ""
  66. Write-Host $VMhost.Name":"
  67. $VMhost | Get-VMHostNetworkAdapter -Physical | %{($esxcli.network.nic.vlan.stats.set($FALSE,$_.Name))}
  68.  
  69. #Check again to see if vlan stats are now disabled
  70. $Resp = ""
  71. try { ($esxcli = $VMHost | Get-EsxCli).network.nic.vlan.stats.get("vmnic0")| Out-Null } catch { $Resp = $_.Exception }
  72.  
  73. }#If $Resp is NULL – vlan stats hasn’t been disabled! Error, Abort, Abort, ABORT!
  74. elseif(([string]::IsNullOrEmpty($Resp))){
  75. #Write to prompt a useful error message
  76. Write-host $VMhost.Name "Says: ARRRRGGG PANIC"
  77. }
  78. }
  79.  
  80. $Report | select Host -ExpandProperty vlandiff | Sort Host,InputObject
Add Comment
Please, Sign In to add comment