Sunday, January 19, 2014

PowerCLI script to change the security setting of you vSwitch vSphere 4.x/5.x (Bulk Changes)

You need to open the PowerCLI and then connect to your vCenter Server system with administrator privilege account and then run the script below (you can change the security True or False as per your requirement)


foreach ($VMHost in Get-VMHost){
            Write-Host "`n"$VMHost.Name
            foreach($vSwitch in $VMHost | Get-VirtualSwitch -Standard){
                       Write-Host " "$vSwitch.Name
                         foreach ($esxid in $VMhost |% {get-view $_.Id}){
                         Write-host " "$esxid.Name
                         #get the current vSwitch configuration
                         $esxidconfig=$esxid.configmanager
                         $esxns=$esxidconfig.networksystem
                         $esxnsview=get-view $esxns
                         $esxvSwitch=$esxnsview.NetworkConfig.Vswitch | where {$_.Name -eq $vSwitch}
                          #Add changes to the vSwitch specification
                          $specChange= $esxvSwitch.Spec
                          $specChange.policy.security.allowPromiscuous=$True
                          $specChange.policy.security.forgedTransmits=$True
                          $specChange.policy.security.macChanges=$True
                          #update the specification for the vSwitch
                          $esxnsview.UpdateVirtualSwitch($esxvSwitch.name,$specChange)
                          echo "Added security updates to $vSwitch"
                          }
              }
}

No comments: