Hi all!
In my previous topic vSphere 6.5 powercli OSCustomizationSpec 2nd IP LucD help me in solving the issue that I had.
The script works like a charm but only for the first run, I will explain:
The scripts reads the csv file and creates 9 VM’s, the VM’s are powered on, it assigns the 2 IP’s to the individual nics on each of the VM’s along with the subnet mask and gateway and changes their hostnames.
Here comes the part when running the script again with changes to the csv file:
Since I want to add 4 more VM’s, I open the csv file remove the old information and enter the new data and save it. When I run the script again It creates the 4 VM’s.
Unfortunately, some VM’s are powered on and some are not. The assigned IP's on the ones that were powered on are not static but dynamic, and the hostname didn’t change. When powering on the other ones it is the same issue, dynamic IP’s and no change in the hostname.
I have reset everything, I have recreated the 9 VM’s, and then added the other 4 and it would create them, power on all 4 but, the IP’s are dynamic and the hostname does not change.
I have done some research but nothing similar is found anywhere.
csv headers:
Name,Cluster,Template,IP,IP2,Folder
Powercli code that is working on the first run:
#Declares and sets a variable to ignore warning errors that are not fatal
$errorActionPreference = "SilentlyContinue"
#Connects to the vCenter Server
Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxxxx
#Create the linux VM,s from template
$vms = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
#Creates the customizing for nic 1
$OSSpec = New-OSCustomizationSpec –Name LinuxCus2NIC –Domain local –DnsServer “xxx.xxx.xxx.xxx” –OSType Linux –Type NonPersistent
#Creates the customizing for nic 2
$OSSpec2 = New-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC –IpMode UseStaticIP –IpAddress “10.0.0.1” –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
#Starts the creation of each of the vm's
foreach ($vm in $vms)
{
#increments by 1
$NewVMCount++
#Displays a message
Write-Host "Creating: " -ForegroundColor yellow -NoNewline; Write-Host "$($vm.Name) " -ForegroundColor Cyan -NoNewline; Write-Host "in " -ForegroundColor Yellow -NoNewline; Write-Host "$($vm.cluster) " -ForegroundColor Cyan
#Asigns the header from the csv files
New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template -RunAsync
#Sets the custimizing for nic 1
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $vm.IP –SubnetMask “255.255.255.0” –DefaultGateway “xxx.xxx.xxx.xxx”
#Sets the custimizing for nic 2
$nicMapping2 = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
$nicMapping2 | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStaticIP –IpAddress $vm.IP2 –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
#Displays a message
Write-Host "Configuring settings on " -ForegroundColor yellow -NoNewline; write-host $($vm.name) -ForegroundColor Cyan
#Customize the Guest VM
Set-VM $vm.name -OSCustomizationSpec LinuxCus2NIC -Confirm:$False
#Starts the vm's
Start-VM $vm.Name
#Displays a message
Write-Host "Running scripts on " -ForegroundColor yellow -NoNewline; write-host $($vm.name) -ForegroundColor Cyan -NoNewline; write-host "..." -ForegroundColor yellow -NoNewline;
#Moves the created vm to the correct vm folder
$folder = Get-Folder -Name $vm.Folder -Type VM
Move-VM -VM $vm.Name -InventoryLocation $folder -Confirm:$false
}
#Disconnects from the vCenter Server
Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False
#Verifies if the variable has a 1
if($NewVMCount -eq 1)
{
#Displays this mesage
Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm " -ForegroundColor Cyan -NoNewline; Write-Host "has been created." -ForegroundColor yellow
}
else
{
#Displays this message if the variable is empty
Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm's " -ForegroundColor Cyan -NoNewline; Write-Host "have been created." -ForegroundColor yellow
}
#Creates spaces
Write-Host
Write-Host "Completed " -ForegroundColor green
#Removes the customization created
Get-OSCustomizationSpec -Name 'LinuxCus2NIC' -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false
#Clears the variables
Clear-Variable -Name "vms"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "OSSpec2"
Clear-Variable -Name "vm"
Clear-Variable -Name "NewVMCount"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "nicMapping2"
Clear-Variable -Name "folder"
Any assistance is appreciated.
Regards,
Hi LucD,
It seems that my code needed some tweaking.
The issue was that when creating the VM and applying the OS customization, I needed to add time for the VM to complete being created.
With the adding of the 30 secs between the completion of the VM and the customization being applied they are now receiving their IP's, name change, etc.…. Of Course the 30 sec are in my case, this would most likely need to be adjusted for your environment.
Here is the code with some additional changes for anyone who may use it:
CSV Header:
Name,Cluster,Template,IP,IP2,Folder
#Connects to the vCenter Server
Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxx
#
$errorActionPreference = "SilentlyContinue"
$vmsVerify = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
$Erronum = 0
$NewVMCount = 0
#
#Here it verifies if the VM name exists in the Vsphere inventory.
foreach ($vmverify in $vmsVerify)
{
$Exists = get-vm -name $vmverify.Name
if(-not $Exists)
{
Write-Host $vmverify.Name -NoNewline; Write-Host -ForegroundColor green " √"
}
else
{
$Erronum++
Write-Host -ForegroundColor yellow $vmverify.Name -NoNewline; Write-Host -ForegroundColor red " X"
}
}
#
#If the variable is equal or greater than one
if($Erronum -ge 1)
{
Write-Host
Write-Host -ForegroundColor yellow "Virtual Machine name already exist! Please verify your data."
Clear-Variable -Name "errorActionPreference"
Clear-Variable -Name "Erronum"
Clear-Variable -Name "Exists"
Clear-Variable -Name "vms"
Clear-Variable -Name "vmsVerify"
Clear-Variable -Name "vmverify"
Clear-Variable -Name "NewVMCount"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "nicMapping2"
Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False
Break
}
else
{
Write-Host
Write-Host
$errorActionPreference = "SilentlyContinue"
#Connects to the vCenter Server
Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxx
#Create the linux VM,s from template
$vms = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
#Creates the customizing for nic 1
$OSSpec = New-OSCustomizationSpec –Name LinuxCus2NIC –Domain local –DnsServer “xxx.xxx.xxx.xxx” –OSType Linux –Type NonPersistent
#Creates the customizing for nic 2
$OSSpec2 = New-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC –IpMode UseStaticIP –IpAddress “10.0.0.1” –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
#Starts the creation of the vm's
foreach ($vm in $vms)
{
$NewVMCount++
#Displays a message
Write-Host "Creating: " -ForegroundColor yellow -NoNewline; Write-Host "$($vm.Name) " -ForegroundColor Cyan -NoNewline; Write-Host "in " -ForegroundColor Yellow -NoNewline; Write-Host "$($vm.cluster) " -ForegroundColor Cyan
#Asigns the header from the csv files
New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template
#Sets the custimizing for nic 1
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $vm.IP –SubnetMask “255.255.255.0” –DefaultGateway “xxx.xxx.xxx.xxx”
#Sets the custimizing for nic 2
$nicMapping2 = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
$nicMapping2 | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStaticIP –IpAddress $vm.IP2 –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
Write-Host "Configuring settings on " -ForegroundColor yellow -NoNewline; write-host $($vm.name) -ForegroundColor Cyan -NoNewline
#
Start-Sleep -Seconds 30
#Customize the Guest VM
Set-VM $vm.name -OSCustomizationSpec LinuxCus2NIC -Confirm:$False
#Starts the vm's
Start-VM $vm.Name | Wait-Tools
$folder = Get-Folder -Name $vm.Folder -Type VM
Move-VM -VM $vm.Name -InventoryLocation $folder -Confirm:$false
}
}
if($NewVMCount -eq 1)
{
Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm " -ForegroundColor Cyan -NoNewline; Write-Host "has been created." -ForegroundColor yellow
}
else
{
Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm's " -ForegroundColor Cyan -NoNewline; Write-Host "have been created." -ForegroundColor yellow
}
Write-Host
Write-Host "Completed " -ForegroundColor green
Clear-Variable -Name "errorActionPreference"
Clear-Variable -Name "Erronum"
Clear-Variable -Name "Exists"
Clear-Variable -Name "vms"
Clear-Variable -Name "vmsVerify"
Clear-Variable -Name "vmverify"
Clear-Variable -Name "NewVMCount"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "nicMapping2"
Clear-Variable -Name "vmsMove"
Clear-Variable -Name "vmMove"
Clear-Variable -Name "folder"
Get-OSCustomizationSpec -Name 'LinuxCus2NIC' -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false
#Disconnects from the vCenter Server
Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False
Regards,
Getting a dynamic instead of a fixed IP address can have many reasons.
Did you check inside the Guest OS if there is anything in the logs?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HI LucD,
I did not verify the logs since all the VMs were created using the same template. I am able to create all 13 VMs at once with no problem. Its only happens when I want to add more vms and I rerun the script.
I was thinking that maybe its PowerShell acting up or maybe its my script. I was looking at my code, and thinking could it be that there is not enough time between the creation of the Vms and the start of the OS Customization.
Is it possible to wait for the VM to complete being created and then the start of the OS customization set-vm?
Regards,
You could wait for the events CustomizationSucceeded or CustomizationFailed.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
It seems that my code needed some tweaking.
The issue was that when creating the VM and applying the OS customization, I needed to add time for the VM to complete being created.
With the adding of the 30 secs between the completion of the VM and the customization being applied they are now receiving their IP's, name change, etc.…. Of Course the 30 sec are in my case, this would most likely need to be adjusted for your environment.
Here is the code with some additional changes for anyone who may use it:
CSV Header:
Name,Cluster,Template,IP,IP2,Folder
#Connects to the vCenter Server
Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxx
#
$errorActionPreference = "SilentlyContinue"
$vmsVerify = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
$Erronum = 0
$NewVMCount = 0
#
#Here it verifies if the VM name exists in the Vsphere inventory.
foreach ($vmverify in $vmsVerify)
{
$Exists = get-vm -name $vmverify.Name
if(-not $Exists)
{
Write-Host $vmverify.Name -NoNewline; Write-Host -ForegroundColor green " √"
}
else
{
$Erronum++
Write-Host -ForegroundColor yellow $vmverify.Name -NoNewline; Write-Host -ForegroundColor red " X"
}
}
#
#If the variable is equal or greater than one
if($Erronum -ge 1)
{
Write-Host
Write-Host -ForegroundColor yellow "Virtual Machine name already exist! Please verify your data."
Clear-Variable -Name "errorActionPreference"
Clear-Variable -Name "Erronum"
Clear-Variable -Name "Exists"
Clear-Variable -Name "vms"
Clear-Variable -Name "vmsVerify"
Clear-Variable -Name "vmverify"
Clear-Variable -Name "NewVMCount"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "nicMapping2"
Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False
Break
}
else
{
Write-Host
Write-Host
$errorActionPreference = "SilentlyContinue"
#Connects to the vCenter Server
Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxx
#Create the linux VM,s from template
$vms = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
#Creates the customizing for nic 1
$OSSpec = New-OSCustomizationSpec –Name LinuxCus2NIC –Domain local –DnsServer “xxx.xxx.xxx.xxx” –OSType Linux –Type NonPersistent
#Creates the customizing for nic 2
$OSSpec2 = New-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC –IpMode UseStaticIP –IpAddress “10.0.0.1” –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
#Starts the creation of the vm's
foreach ($vm in $vms)
{
$NewVMCount++
#Displays a message
Write-Host "Creating: " -ForegroundColor yellow -NoNewline; Write-Host "$($vm.Name) " -ForegroundColor Cyan -NoNewline; Write-Host "in " -ForegroundColor Yellow -NoNewline; Write-Host "$($vm.cluster) " -ForegroundColor Cyan
#Asigns the header from the csv files
New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template
#Sets the custimizing for nic 1
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $vm.IP –SubnetMask “255.255.255.0” –DefaultGateway “xxx.xxx.xxx.xxx”
#Sets the custimizing for nic 2
$nicMapping2 = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
$nicMapping2 | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStaticIP –IpAddress $vm.IP2 –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
Write-Host "Configuring settings on " -ForegroundColor yellow -NoNewline; write-host $($vm.name) -ForegroundColor Cyan -NoNewline
#
Start-Sleep -Seconds 30
#Customize the Guest VM
Set-VM $vm.name -OSCustomizationSpec LinuxCus2NIC -Confirm:$False
#Starts the vm's
Start-VM $vm.Name | Wait-Tools
$folder = Get-Folder -Name $vm.Folder -Type VM
Move-VM -VM $vm.Name -InventoryLocation $folder -Confirm:$false
}
}
if($NewVMCount -eq 1)
{
Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm " -ForegroundColor Cyan -NoNewline; Write-Host "has been created." -ForegroundColor yellow
}
else
{
Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm's " -ForegroundColor Cyan -NoNewline; Write-Host "have been created." -ForegroundColor yellow
}
Write-Host
Write-Host "Completed " -ForegroundColor green
Clear-Variable -Name "errorActionPreference"
Clear-Variable -Name "Erronum"
Clear-Variable -Name "Exists"
Clear-Variable -Name "vms"
Clear-Variable -Name "vmsVerify"
Clear-Variable -Name "vmverify"
Clear-Variable -Name "NewVMCount"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "nicMapping2"
Clear-Variable -Name "vmsMove"
Clear-Variable -Name "vmMove"
Clear-Variable -Name "folder"
Get-OSCustomizationSpec -Name 'LinuxCus2NIC' -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false
#Disconnects from the vCenter Server
Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False
Regards,