Creating an isolated network between two virtual machines and facing issue while assigning static IP

This post was originally published on this site

Issue- I was following the KB https://kb.vmware.com/s/article/2043160 and facing issues while assigning static IP to a VM with two network adapters

 

Script that I was using –

 

$vmNameTemplate = “VM-{0:D3}”

 

$cluster = Get-Cluster -Name “Test Cluster”

$template = Get-Template “New_Template”

$vmList = @()

 

New-VirtualSwitch -VMHost ‘esxi3.gsslabs.org’ -Name vSwitch8

New-VirtualPortGroup -Name “Private” -VirtualSwitch vSwitch8 -VLanId 0

 

$vmhost = Get-Cluster ‘Test Cluster’ | Get-VMHost -Name ‘esxi3.gsslabs.org’

 

$myDatastoreCluster = ‘Datastore’

 

for ($i = 1; $i –le 2; $i++) {

    $vmName = $vmNameTemplate –f $i

    $vmList += New-VM –Name $vmName -Datastore $myDatastoreCluster -VMHost $vmhost –Template $template

    |Start-VM   

}

 

#Create a custom spec 

$staticIpList = Import-CSV C:deploy.csv

      $sCust = @{

         Name = “Win2008r”

         OSType = ‘Windows’

         Type = ‘Persistent’

         FullName = “Test”

         OrgName = “VMware1”

         NamingScheme = ‘VM’

         AutoLogonCount = 1

         ChangeSID = $true

         Confirm = $false

         Workgroup = “VMware”

    }

 

New-OSCustomizationSpec @sCust

$nicMappings = Get-OSCustomizationNicMapping –OSCustomizationSpec Win2008r

Remove-OSCustomizationNicMapping –OSCustomizationNicMapping $nicMappings

$specClone = New-OSCustomizationSpec –Spec Win2008r –Type NonPersistent

 

for ($i = 0; $i –lt $vmList.Count; $i++) {

$vm=Get-VM -Name $vmList[$i]

$ip = $staticIpList[$i].ip

New-Networkadapter -vm $vmList[$i] -NetworkName “Private” -Type “E1000” -startconnected

$publicNIC = $vmList[$i] | Get-NetworkAdapter| where {$_.NetworkName -eq “VM Network”}

$privateNIC = $vmList[$i] | Get-NetworkAdapter| where {$_.NetworkName -eq “Private”}

 

 

New-OSCustomizationNicMapping -OSCustomizationSpec $specClone –IpMode UseDhcp –NetworkAdapterMac $publicNIC.MacAddress

New-OSCustomizationNicMapping -OSCustomizationSpec $specClone –IpMode UseStaticIP –IpAddress $ip –SubnetMask “255.255.255.0” –DefaultGateway “192.168.0.1” -Dns “192.168.0.10” -NetworkAdapterMac $privateNIC.MacAddress

 

$nicMappings = Get-OSCustomizationNicMapping –OSCustomizationSpec $specClone | where {$_.Position –eq 1}

$nicMappings | Set-OSCustomizationNicMapping –IpMode UseDhcp –NetworkAdapterMac $publicNIC.MacAddress

 

$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec $specClone | where {$_.Position –eq 2}

$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $ip –SubnetMask “255.255.255.0” –DefaultGateway “192.168.0.1” -Dns “192.168.0.10” -NetworkAdapterMac $privateNIC.MacAddress

  

$vmCust = Get-OSCustomizationSpec -Name $specClone

#New-NetFirewallRule -DisplayName “Allow inbound ICMPv4” -Direction Inbound -Protocol ICMPv4 -IcmpType 8 -Action Allow

Set-VM –VM $vmList[$i] –OSCustomizationSpec $vmCust –Confirm:$false

   }

 

Any assistance that I can get would be much appreciated.

Thank you

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.