I’ve created a windows VM template and a customization profile which get VM name from deploy and for static IP during VM creation.
I’m trying to create a powercli script that reads Name and I’m from a CSV file and use them in a new-VM command line with Template and VM customization, however I’m not finding the correct way of doing this and get errors.
Can anyone spare some example of a script that would pass variables for Name and Static IP to the command line to create a VM with template and customization ?
@LucD I just found that if I set only one DNS in the line
$NicSpecsProperties.Dns = $IPDNSServer1
It works
So the issue is if I try to put two DNS with
$NicSpecsProperties.Dns = $IPDNSServer1, $IPDNSServer2
$NicSpecsProperties = @{OSCustomizationNicMapping= Get-OSCustomizationNicMapping -OSCustomizationSpec $spec }
if ($IsDHCP){
$NicSpecsProperties.IpMode = “UseDHCP”
}else{
#Assigning IP values
$NicSpecsProperties.IpMode = “UseStaticIP”
$NicSpecsProperties.IpAddress = $v.IP
$NicSpecsProperties.SubNetMask = $IPSubnetMask
$NicSpecsProperties.DefaultGateway = $IPGateway
$NicSpecsProperties.Dns = $IPDNSServer1,$IPDNSServer2
This part of code, taken by other examples, works by getting the Network parameters from the customization profile and then set the IP read from a CSV files.
I confirm that it's working fine now.
Can you show us the code you are using and the errors you are getting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here the core of the script I'm trying to use
foreach ($v in $VMs) {
$vmhost = Get-VMHost -Server $vcenter0 -location EITESX | Get-Random -Count 1
$Datastore = Get-Datastore -Location DSCLUSTER_AL9000 | Get-Random -Count 1
$NicSpecsProperties = @{OSCustomizationNicMapping= Get-OSCustomizationNicMapping -OSCustomizationSpec $spec }
if ($IsDHCP){
$NicSpecsProperties.IpMode = “UseDHCP”}else{
#Assigning IP values
$NicSpecsProperties.IpMode = “UseStaticIP”
$NicSpecsProperties.IpAddress = $v.IP
$NicSpecsProperties.SubNetMask = $IPSubnetMask
$NicSpecsProperties.DefaultGateway = $IPGateway
$NicSpecsProperties.dns = $IPDNSServer1,$IPDNSServer2
}
Set-OSCustomizationNicMapping @NicSpecsProperties | Out-Null
New-VM -Name $v.NAME -Template $Template -OSCustomizationSpec $Spec -VMHost $vmhost -Datastore $Datastore
Start-VM -Name $v.NAME
}
Here is the error message
Set-OSCustomizationNicMapping : 2/19/2024 8:41:08 AM Set-OSCustomizationNicMapping The parameter 'Dns' is invalid.
At C:\Scripts\CreaVM.ps1:49 char:1
+ Set-OSCustomizationNicMapping @NicSpecsProperties | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-OSCustomizationNicMapping], ViError
+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateIpAddress_InvalidIP,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping
New-VM : 2/19/2024 8:41:19 AM New-VM The operation for the entity "WindowsServer2019Template" failed with the following message: "A specified parameter was not
correct: nicsettings:adapter:ip"
At C:\Scripts\CreaVM.ps1:51 char:1
+ New-VM -Name $v.NOME -Template $Template -OSCustomizationSpec $Spec - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], InvalidArgument
+ FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
Start-VM : A parameter cannot be found that matches parameter name 'Name'.
At C:\Scripts\CreaVM.ps1:52 char:11
+ Start-VM -Name $v.NOME
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-VM], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.StartVM
The VM gets created but without the customization
@LucD I just found that if I set only one DNS in the line
$NicSpecsProperties.Dns = $IPDNSServer1
It works
So the issue is if I try to put two DNS with
$NicSpecsProperties.Dns = $IPDNSServer1, $IPDNSServer2
What is this supposed to do?
$NicSpecsProperties = @{OSCustomizationNicMapping= Get-OSCustomizationNicMapping -OSCustomizationSpec $spec }
And how can this even work?
if ($IsDHCP){
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$NicSpecsProperties = @{OSCustomizationNicMapping= Get-OSCustomizationNicMapping -OSCustomizationSpec $spec }
if ($IsDHCP){
$NicSpecsProperties.IpMode = “UseDHCP”
}else{
#Assigning IP values
$NicSpecsProperties.IpMode = “UseStaticIP”
$NicSpecsProperties.IpAddress = $v.IP
$NicSpecsProperties.SubNetMask = $IPSubnetMask
$NicSpecsProperties.DefaultGateway = $IPGateway
$NicSpecsProperties.Dns = $IPDNSServer1,$IPDNSServer2
This part of code, taken by other examples, works by getting the Network parameters from the customization profile and then set the IP read from a CSV files.
I confirm that it's working fine now.
I set 02 DNS without issue. You should review again.
$SpecClone | Get-OSCustomizationNicMapping | set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $subnet -DefaultGateway $gateway -Dns $DNS1,$DNS2
@linhtm27 Yes I found what the problem was, it had an extra space in the dns2 variable
thanks