Reply to Message

View discussion in a popup

Replying to:
LucD
Leadership
Leadership

The issue is most likely because you the VirtualMachine in the hash table (with the Key of the TakId).

But such a VirtualMachine object is not updated automatically.

You will for example not see that the powerstate of the VM has changed.

For that you have to 'refresh' the object, by doing a new Get-VM.

Something like this

$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") {

            $vm = Get-VM $taskTab[$_.Id]

            Write-Host "Snapshot of $($vm.Name) 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 $vm -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") {

                $vm = Get-VM -Name ($taskTab[$_.Id]).Name

                if ($vm.Powerstate -eq "PoweredOn") {

                    return "One or more VMs is still powered on. Manually power off VMs before re-attempting."

                }

                $vid = $vm.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

                $vm.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") {

                $vm = Get-VM -Name (Get-VM $taskTab[$_.Id]).Name

                if ($vm.Powerstate -eq "PoweredOff") {

                    Write-Host "$($vm.Name) is now being powered on. Please wait five to ten minutes before verifying new configuration" -foregroundcolor green

                    $taskTab[(Start-VM -VM $vm -Confirm:$false -RunAsync).Id] = $vm

                }

            }

        }

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post