vrijdag, april 17, 2009

ESX - VLAN compare / checker script for PowerShell

First and foremost: all credit goes to Hugo Peeters over at http://www.peetersonline.nl. I just took his script and modified what was useful from a VLAN perspective. The code below will create a HTML page with VLAN inconsistencies. This can be a useful script in a large environment to compare all VLANs/portgroups presented on a per cluster basis. I know we use it ;)

Copy/paste this in PowerGUI script editor and run! Make sure c:\temp exists

BTW: in vSphere we will have host profiles and distributed vSwitches to tackle the “not presented everywhere” problem ;)

---------------------------------------------------------------

$outputFile = 'C:\temp\compareVLAN.html'
$VCServer = "<vCenter FQDN>"
$VCUsername = '<username>'
$VCPassword = "<password>"

Function Create-HTMLTable
{
    param([array]$Array)
    $arrHTML = $Array | ConvertTo-Html
    $arrHTML[-1] = $arrHTML[-1].ToString().Replace('</body></html>',"")
    Return $arrHTML[5..1000]
}

$output = @()
$output += '<html><head></head><body>'
$output += '<style>table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:20%;}th{text-align:left;}td{background-color:#fff;width:20%;border-style:solid;border-width:1px;}body{font-family:verdana;font-size:8pt;}h1{font-size:12pt;}h2{font-size:10pt;}</style>'
$output += '<h1>VMWARE CONFIGURATION INCONSISTENCIES - VLAN</h1>'
$output += '<p>The following VLANs are not available to all ESX servers in the cluster:</p>'

$VC = Connect-VIServer $VCServer -User $VCusername -Password $VCPassword

ForEach ($Cluster in (Get-Cluster | Sort Name))
{
        $output += "<h2>" + $Cluster + "</h2>"
        $VMHosts = $Cluster | Get-VMHost | Sort Name
        $vSwitches = Get-VirtualSwitch -VMHost $VMHosts
        $AllPGs = Get-VirtualPortGroup -VirtualSwitch $vSwitches
        $myPGCol = @()

        $PGdiffs = $VMHosts | ForEach {Compare-Object $AllPGs (Get-VirtualPortGroup -VMHost $_) -SyncWindow 1000} | ForEach {$_.InputObject} | Sort Name | Select Name -Unique
        ForEach ($PGdiff in $PGdiffs)
        {
            If ($PGdiff.Name -ne $null)
            {
                   $myPGObj = "" | Select VirtualPortGroup
                $myPGObj.VirtualPortGroup = $PGdiff.Name
                   ForEach ($VMHost in $VMHosts)
                   {
                   $myPGObj | Add-Member -MemberType NoteProperty -Name $VMHost.Name -Value (!!(Get-VirtualPortGroup -Name $myPGObj.VirtualPortGroup -VMHost $VMHost -ErrorAction SilentlyContinue))
                }
                $myPGCol += $myPGObj
            }
        }
        If ($myPGCol.Count -eq 0)
        {
            $output += '<p>'
            $output += "All VLANs OK."
            $output += '</p>'
        }
        Else
        {
            $output += Create-HTMLTable $myPGCol
        }
        Clear-Variable $VMHosts -ErrorAction SilentlyContinue
        Clear-Variable $myPGObj -ErrorAction SilentlyContinue
        Clear-Variable $myPGCol -ErrorAction SilentlyContinue
        Clear-Variable $PGdiffs -ErrorAction SilentlyContinue
        Clear-Variable $PGdiff -ErrorAction SilentlyContinue
}

$output += '</body></html>'   
$output | Out-File $outputFile -Force
ii $outputfile

Disconnect-VIServer -Confirm:$False

---------------------------------------------------------------

Geen opmerkingen: