Hi All,
I am having a script to deploy a VM from an CSV file . I need to change the vm ( CPU & Memory) configuration after vm deployment. where the name of the vm should be taken from csv.So any help is much appreciated.
Attaching the script used . ( downloaded and works fine)
$vms = Import-CSV D:\vm-deploy\vm-deploy.csv
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization -Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync
}
## This cmd need to execute after the vm deployment completed. The VM name should come from the 1 colum of CSV file
Set-VM $vmname -NumCpu 2 -MemoryGB 5 -Confirm:$false
Write-Host "All vms deployed, " -ForegroundColor Green
Disconnect-VIServer -Confirm:$false
CSV file looks like this
Name,Template,host,datastore,customization
Test-VM,vm-dep-test,ESX02,P5-vm-stage,vm-deploy
Thanks in advance.....
A simpler, but not optimal, solution is to use the Wait-Task cmdlet.
Something like this
$vms = Import-CSV D:\vm-deploy\vm-deploy.csv
$tasks = @()
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
$tasks += New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization -Template $Template -VMHost $VMHost `
-Datastore $Datastore -RunAsync
}
Wait-Task -Task $tasks
foreach($vm in $vms){
Set-VM $vm.name -NumCpu 2 -MemoryGB 5 -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Since you use the RunAsync switch and do customization of the VM, you will need to have a mechanism to wait till the VM is fully deployed and customised.
An excellent method for this is to use events, like Vitali described in his Waiting for OS customization to complete post.
In step 4 of Vitali's procedure you can do your changes to CPU and memory.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
Thanks for this reply. I checked the link provided but it seems too big for me to understand as I am not a powershell expert.
Can you please modify it in my script.
Thanks a lot for supporting this community.
A simpler, but not optimal, solution is to use the Wait-Task cmdlet.
Something like this
$vms = Import-CSV D:\vm-deploy\vm-deploy.csv
$tasks = @()
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
$tasks += New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization -Template $Template -VMHost $VMHost `
-Datastore $Datastore -RunAsync
}
Wait-Task -Task $tasks
foreach($vm in $vms){
Set-VM $vm.name -NumCpu 2 -MemoryGB 5 -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The earlier approach work fine thanks for the update.
Can you please help us to modify the script .where the script need to execute the second line if only the HDD2 value exists in CSV file. I tried different option but no luck.
The CSV file is created by an front end web tool and in back we are using ps script.So some times the user may not required the 2nd HDD in vm.
Sample CSV file
Name | Template | host | datastore | customization | CPU | Memory | hdd1 | hdd2 | hdd2 |
VM-Deploy | VMTMPENT2K8 | ESXi-host1 | VM-DEPLOY-DS | win 2008 | 4 | 12 | 5 |
Script we are using is
Connect-VIServer localhost
$vms = Import-CSV E:\lafarge-vm-dep\vm-deploy.csv
foreach($vm in $vms){
Set-VM $vm.name -NumCpu $vm.CPU -MemoryGB $vm.Memory -Confirm:$false
New-HardDisk -VM $vm.name -CapacityGB $vm.hdd1 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"
New-HardDisk -VM $vm.name -CapacityGB $vm.hdd2 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"
New-HardDisk -VM $vm.name -CapacityGB $vm.hdd3 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"
}