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
It looks as if you have more than 1 OScustomizationSpec in $vmCust.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
And is there a question in here?
What is the issue?
Any error messages?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for your response LucD
I was deploying VMs on the same host to create isolated vms, each vm with 2 nics of which 1 nic has dhcp and another nic should have static ip. I was unable to assign static ip to second nic
Error Message:
Set-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.OSCustomization.OSCustomizationSpec'
required by parameter 'OSCustomizationSpec'. Specified method is not supported.
At C:\Users\naishwarya\OneDrive - VMware, Inc\SR\SR\SR_PrivateNetwork\Isolated-VMs.ps1:94 char:50
+ Set-VM –VM $vmList[$i] –OSCustomizationSpec $vmCust –Confirm:$fa ...
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VM], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM
It looks as if you have more than 1 OScustomizationSpec in $vmCust.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for your response LucD
Basically I need to assign a static IP to a guest VM. It's not giving any error while executing the code but IP is not assigned to newly created VM. Please let me know how to assign a static IP to a VM.
$template = Get-Template "New_Template"
$vmhost = Get-Cluster 'Test Cluster' | Get-VMHost -Name 'esxi3.gsslabs.org'
$myDatastore = 'Datastore'
Get-OSCustomizationSpec VM_custom | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress "192.168.0.6" -SubnetMask 255.255.255.0 –DefaultGateway "192.168.0.1" -Dns "192.168.0.10"
New-VM –Name "TestVM" -Datastore $myDatastore -VMHost $vmhost –Template $template -OSCustomizationSpec VM_Custom |Start-VM
So you 'System.Object[]' error is resolved I assume?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I just took a piece of code and trying to assign static IP. "VM_Custom" is the customization spec..But IP is not getting assigned to a newly created TestVM
$template = Get-Template "New_Template"
$vmhost = Get-Cluster 'Test Cluster' | Get-VMHost -Name 'esxi3.gsslabs.org'
$myDatastore = 'Datastore'
Get-OSCustomizationSpec VM_Custom | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress "192.168.0.6" -SubnetMask 255.255.255.0 –DefaultGateway "192.168.0.1" -Dns "192.168.0.10"
New-VM –Name "TestVM" -Datastore $myDatastore -VMHost $vmhost –Template $template -OSCustomizationSpec VM_Custom |Start-VM
One way for debugging OSCustomizationSpec I often use is to use the Web Client to check what is actually in there.
Are your static IP settings in there?
Also, the OSCustomization creates logs, see KB1026317.
Did you check the logs for what might have happened?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, It's reflecting in web client. Sure, I will do the log analysis.
It worked!! I was able to assign static IPs to the vms.
LucD Thank you!