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

 

2 Comments

    joseph

    This is great for many reasons. One, I love it because I have been learning powershell for 2 years and this has lots of lovely comments; I love comments in code. Two, based on this reading this for a couple of hours, I finally learned my directory structure for my scripts (e.g. powershell, bash, etc). “Most everyone” on the internet that posts scripts on the internet uses concrete directory names, like you do, i.e. “C:\PowerCLI\”. These type of concrete directory names are not viable. So, the question is do I modify my powershell path variable or do I have to manually cd into a directory to employ the script. Based on your script, I know will have a directory: “…\add_vmkernel” and “…add_vm_kernel\example”. All your stuff will go in “…\_ex”, (n.b. _ex will be for example – I THINK, I only use underscore as first letter because it the directory name shows up first in unix and windows) except for the .csv file because that is a security risk for me to download a .csv file. Then I will extract out the concrete names in “…\add_vmkernel\abstract\addvmkernel_v#.ps1” where # will be the version number and abstract is …so then I can prepare to move it to some directory, unknown to me at this time, where I can do a full script for add_vmkernel_process so that I can add_vmkernel and set a 10 minute rollback timer so that I can do an automatic rollback. I do not know how to do anything like a 10 minute rollback timer now, so I will just have to make a manual remove_vmkernel for the add_vmkernel_process. This looks like a lot of fun. I will have to go over to the scripting guy and see what “he has to say about this” (IOW, I have to go read all his archives). In my add_vmkernel_process.ps1, I will have all concrete ip addresses and read things in from a local flat file in that directory – wherever that directory is…..So your “quickly put together a script” will probably take me 120 hours to do. I am sure this is very confusing to anyone that reads this, but now I have a plan so I can “add_vmkernel” to my 1 homelab esxi…. I just need to create, among other things, remove_vmkernel.ps1

    joseph

    And your “start-transcript” is not valid for my usage.
    I will have to rethink my entire “start-transcript” policy because I have a MASTER “start-transcript” in my “…\Windows Powershell\profile.ps1”. You decimated, in one fell swoop, my entire “start-transcript” policy. It will take me about 8 hours to redesign a new “strart-transcript” policy.

Leave a Reply

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

*
*