VMware ESX network cloning

Ever wanted to clome your network settings across multiple ESX hosts ?

 

Execute this first :

Add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

 

That adds in the relevant Powershell tools save you having to use PowerCLi

 

Then copy and paste this into a .ps1 file

 

$srvconnection = Connect-VIServer (Read-Host “Please enter the name of your VCenter SERVER”)
$sourceHost = Get-VMHost -Name (Read-Host “Enter the name of your existing server as seen in the VI Client Source:”)
$targetHost = Get-VMHost -Name (Read-Host “Enter the name of the server to configure as seen in the VI Target Client:”)

#Collecting source information
$sourceHostObj = Get-VMHost -Name $sourceHost -Server $srvconnection
write-host “Exporting vSwithes configurations from $sourceHost”
$sourcevSwitches = $sourceHostObj | Get-VirtualSwitch
write-host “Exporting Port Group configurations from $targetHost”
$sourcevPGs = $sourceHostObj | Get-VirtualPortGroup

#Collecting target host information
$targethostObj = Get-VMHost -Name $targetHost -Server $srvconnection
write-host “Exporting vSwithes configurations of target $targetHost”
$targetvSwitches = $targetHostObj | Get-VirtualSwitch
write-host “Exporting Port Group configurations of target $targetHost”
$targetvPGs = $targetHostObj | Get-VirtualPortGroup

# determine the difference
$differencevSwitches = Compare-Object $sourcevSwitches $targetvSwitches
$differencevPGs = Compare-Object $sourcevPGs $targetvPGs

# Only process the difference in vSwitches
$differencevSwitches | %{
$newvSwitch = $_.InputObject
Write-Host “Creating Virtual Switch $($newvSwitch.Name) on $targetHost”
if($newvSwitch.Nic) {
$outputvSwitch = $targethostObj | New-VirtualSwitch -Name $newvSwitch.Name -NumPorts $newvSwitch.NumPorts -Mtu $newvSwitch.Mtu -Nic $newvSwitch.Nic
} else {
$outputvSwitch = $targethostObj | New-VirtualSwitch -Name $newvSwitch.Name -NumPorts $newvSwitch.NumPorts -Mtu $newvSwitch.Mtu
}
}

# Only Process difference in Port Groups
$differencevPGs | %{
$newvPG = $_.InputObject
Write-Host “Creating Port group “”$($newvPG.Name)”” on vSwitch “”$($newvPG.VirtualSwitchName)”” on target host $targetHost”
$outputvPG = $targethostObj | Get-VirtualSwitch -Name $newvPG.VirtualSwitchName | New-VirtualPortGroup -Name $newvPG.Name-VLanId $newvPG.VLanID
}

 

 

Only thing to note is it creates the VMotion ports incorrectly , delete them and add them as a VMKernel port.