Adding VMKernel ports to multiple hosts using PowerCLI

So, yesterday I was asked to quickly put together a script to add VMkernel ports to Multiple ESXi hosts. I have a script to add VM Port groups to multiple host, and this is easy. But the issue with the VMkernel ports is that they require a unique IP address.

So I put together the following CSV and Script

CSV File which includes the below information. Obviously the fields will be changed to suit your network


The PowerCLI script:

##----------------------------------------------------------------------##
##---------Script Defaults----------------------------------------------##
##----------------------------------------------------------------------##
Clear-Host
#$ErrorActionPreference = "silentlycontinue"
#$WarningPreference = "silentlycontinue"
#Find PowerCli initalization script
$PowerCli = (Get-ChildItem "c:\Program *" -Filter "*PowerCLIEnvironment.ps1*" -Recurse -ErrorAction SilentlyContinue).FullName
#Run Powercli initalization script
& $PowerCli
Start-Sleep 5
Start-Transcript -path C:\PowerCLI\results.txt

##----------------------------------------------------------------------##
##---------CONNECTIVITY-------------------------------------------------##
##----------------------------------------------------------------------##

$CredentialESXi = Get-Credential "root" -message "Enter the root creds" #Comment out this line if you don't use root creds or if your hosts do not share common credentials

##----------------------------------------------------------------------##
##---------THE MAGIC----------------------------------------------------##
##----------------------------------------------------------------------##
$S = 0

$Esxi_Hosts = Import-CSV C:\PowerCLI\add_vmkernel.csv 

Foreach ($_ in $Esxi_Hosts) 
    {
        Connect-VIServer $_.Esxi_Host -Credential $CredentialESXi | Out-Null #Comment out this line if you don't use root creds or if your hosts do not share common credentials
        #Connect-VIServer $_.Esxi_Host -Credential $_.CredentialESXi | Out-Null #Uncomment this line if you want to use credentials stored in the CSV.
        #Connect-VIServer $_.Esxi_Host | Out-Null #Uncomment this line if you prefer to enter credentials manually
        Write-Host "Adding" $_.VLAN_Label "to" $_.Esxi_Host -foregroundcolor green
        New-VMHostNetworkAdapter -PortGroup $_.VLAN_Label -VirtualSwitch $_.vSwitch -IP $_.NFS_IP -SubnetMask $_.SubnetMask -MTU $_.MTU  | out-null
        Get-virtualportgroup -name $_.VLAN_Label | Set-VirtualPortGroup -Name $_.VLAN_Label -VLANID $_.VLAN_ID | out-null
        Disconnect-VIServer * -Confirm:$false

        $S++
        Write-Progress -Activity "Configuring ESXI Hosts" -status "Configured: $S of $($Esxi_Hosts.Count)" -PercentComplete (($S / $Esxi_Hosts.Count) * 100)

    }


Write-Host "!!!Host Configurations Complete!!!" -ForegroundColor Green

Stop-Transcript

Download the CSV (add_vmkernel.csv) and copy the script from above and place in C:\PowerCLI

Browse to PowerCLI from a powershell window and launch .\add_vmkernel.ps1

You will get prompted for root credentials (or not – depending on what you have commented / uncommented in the script above)

The process will continue as below

Once complete, if you browse to the hosts, you will see the port group has been added

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*