Hi all. My objective is the following:
1) Stop VM
2) Take snapshot
3) Update video ram to new value
4) Start VM
I want each task to be confirmed as complete before proceeding to the next task.
The VMs are in a CSV file.
The stop VM and take snapshot completes successfully. Although there's an error in the second part (screen output).
It doesn't actually update the video ram but just prints out the part stating the script will complete before powering on the VM and it gets stuck there because the VMs are actually not powered on.
The screen output is below:
*****************************************************************************************
PS C:\PowerCLI\scripts\set_video_ram> C:\PowerCLI\Scripts\set_video_ram\update_vram_poweroff_snapshot.ps1
vm1 is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task...
vm2 is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task...
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
vm1 PoweredOff 1 4.000
Snapshot of vm1 is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task...
Exception calling "ContainsKey" with "1" argument(s): "Key cannot be null.
Parameter name: key"
At H:\PowerCLI\Scripts\set_video_ram\update_vram_poweroff_snapshot.ps1:68 char:2
+ elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
vm2 PoweredOff 1 2.000
Snapshot of vm2 is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task...
Exception calling "ContainsKey" with "1" argument(s): "Key cannot be null.
Parameter name: key"
At H:\PowerCLI\Scripts\set_video_ram\update_vram_poweroff_snapshot.ps1:68 char:2
+ elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The scrript will wait for the configuration task to be complete before the VMs are powered on
The script snippet is below:
***********************************************************************************
$newVideoRamSize = 9000
$vmlist = Import-Csv .\vmlist.csv -UseCulture
$vmlist1 = $vmlist | select -ExpandProperty VM
$taskTab = @{}
#Shutdown VMs
foreach ($vm in $vmlist1) {
#if($vm.Powerstate -eq "PoweredOn") { //FYI If this part is not commented out, the script will run without doing anything.
Write-Host $vm is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task... -foregroundcolor green
$taskTab[(Stop-VM -VM $vm -Confirm:$false -RunAsync).Id] = $vm
}
#}
#Take snapshot of VMs
$runningTasks = $taskTab.Count
while($runningTasks -gt 0){
Get-Task | % {
if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){
Get-VM $taskTab[$_.Id]
Write-Host Snapshot of $taskTab[$_.Id] is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task... -foregroundcolor green
$tasktab[(New-Snapshot -VM $taskTab[$_.Id] -Name BeforeVideoRamChange -RunAsync).Id] = $vm
$taskTab.Remove($_.Id)
$runningTasks--
}
}
elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){
$taskTab.Remove($_.Id)
$runningTasks--
}
}
Start-Sleep -Seconds 15
#Update Video Ram
$runningTasks = $taskTab.Count
while($runningTasks -gt 0){
Get-Task | % {
if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){
Get-VM $taskTab[$_.Id]
if($taskTab[$_.Id].Powerstate -eq "PoweredOn")
{return "One or more VMs is still powered on. Manually power off VMs before re-attempting."}
$vid = $taskTab[$_.Id].ExtensionData.Config.Hardware.Device | ?{$_.GetType().Name -eq "VirtualMachineVideoCard"}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$vid.videoRamSizeInKB = $newVideoRamSize
if ((!$devChange.Operation) -or (!$vid.videoRamSizeInKB))
{return "ERROR: Unable to set video memory on $taskTab[$_.Id]}. Ensure it is powered off."}
Write-Host Video Memory on VM: $taskTab[$_.Id] has been successfully set to $vid.videoRamSizeInKB -foregroundcolor green
$devChange.Device += $vid
$spec.DeviceChange += $devChange
$taskTab[$_.Id].ExtensionData.ReconfigVM($spec)
$taskTab.Remove($_.Id)
$runningTasks--
}
elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){
$taskTab.Remove($_.Id)
$runningTasks--
}
Start-Sleep -Seconds 15
Write-Host The script will wait for the configuration task to be complete before the VMs are powered on -foregroundcolor green
}
}
#Power on VMs
$runningTasks = $taskTab.Count
while($runningTasks -gt 0){
Get-Task | % {
if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){
Get-VM $taskTab[$_.Id]
if($taskTab[$_.Id].Powerstate -eq "PoweredOff") {
Write-Host $taskTab[$_.Id] is now being powered on. Please wait five to ten minutes before verifying new configuration -foregroundcolor green
$taskTab[(Start-VM -VM $taskTab[$_.Id] -Confirm:$false -RunAsync).Id] = $vm
}
}
}
}
*****************************************************************************************
Any help to point out the issues and/or fix the script would be greatly appreciated.