Skip to content

Instantly share code, notes, and snippets.

@Mierdin
Last active December 27, 2015 18:49
Show Gist options
  • Save Mierdin/7372710 to your computer and use it in GitHub Desktop.
Save Mierdin/7372710 to your computer and use it in GitHub Desktop.
Creates a VMKernel port on each host connected to a vCenter instance. This will eventually get rolled into the master toolkit, so look there for updates
#---------------------------------------------------------------------
# Name: CreateVMK.ps1
# Author: Matthew Oswalt
# Created: 11/8/2013
# Description: Creates a VMKernel port on each host connected to a vCenter instance. This will eventually get rolled into the master toolkit, so look there for updates
#---------------------------------------------------------------------
#Import PowerCLI Module
Add-PSSnapin VMware.VimAutomation.Core
#Connect to vCenter
Connect-VIServer -Server 10.102.43.12 -User root -Password vmware
#Must use below format for stating subnet of new VMK ports (everything but the numbers in the last octet - that's generated automatically)
$newSubnet = "10.102.32."
$subnetMask = "255.255.255.0"
$strPG = "1KV_CTRL"
#Using the $_ method here, equivalent to the "this" nomenclature in java and .net
Get-VMHost | foreach {
#pull address of management VMK to use as a baseline for new address (use the same last octet)
$thisVMK = $_ | Get-VMHostNetworkAdapter -name vmk0
#Retrieve last octet, without decimal
$lastOctet = $thisVMK.IP.substring($thisVMK.IP.LastIndexOf(".") + 1, $thisVMK.IP.Length - $thisVMK.IP.LastIndexOf(".") - 1)
$thisIPaddr = [string]::Concat($newSubnet, $lastOctet.ToString())
New-VMHostNetworkAdapter -VMHost $_ -VirtualSwitch vSwitch0 -PortGroup $strPG -IP $thisIPaddr -SubnetMask $subnetMask
$PG = Get-VirtualPortgroup -Name $strPG -VMHost $_
Set-VirtualPortGroup -VirtualPortGroup $PG -VlanId 232
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment