BBB36
Enthusiast
Enthusiast

I'd also like to add, I have another version of this script, which actually does everything but I don't think the wait-task works. Here it is:

******************************************************************************************************************************************************

$newVideoRamSize = 9000

$vmlist1 = $vmlist | select -ExpandProperty VM

#Shutdown VMs

    foreach ($vm in $vmlist1) {

    $guestname = Get-VM $vm

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

    Write-Host $guestname is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task... -foregroundcolor green

    $shutvmtask = Stop-VM -VM $guestname -Confirm:$false -RunAsync | Out-Null }

    Wait-Task $shutvmtask

    Start-Sleep -Seconds 15

}

#Take snapshot of VMs

    foreach ($vm in $vmlist1) {

    $guestname = Get-VM $vm

    Write-Host Snapshot of $guestname is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task... -foregroundcolor green

    $snapshotvmtask = New-Snapshot -VM $guestname -Name BeforeVideoRamChange -RunAsync | Out-Null }

    Wait-Task $snapshotvmtask

    Start-Sleep -Seconds 15

#Update Video Ram

    foreach ($vm in $vmlist1) {

        $guestname = Get-VM $vm

        if($guestname.Powerstate -eq "PoweredOn")

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

         $vid = $guestname.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 $guestname}. Ensure it is powered off."}

Write-Host Video Memory on VM: $guestname has been successfully set to $vid.videoRamSizeInKB -foregroundcolor green

       

$devChange.Device += $vid

$spec.DeviceChange += $devChange

$vramconfigtask = $guestname.ExtensionData.ReconfigVM($spec)

        Wait-Task $vramconfigtask

        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

    foreach ($vm in $vmlist1) {

    $guestname = Get-VM $vm

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

    Write-Host $guestname is now being powered on. Please wait five to ten minutes before verifying new configuration -foregroundcolor green

    Start-VM -VM $guestname -Confirm:$false -RunAsync | Out-Null }

}

******************************************************************************************************************************************************

The output is below:

******************************************************************************************************************************************************

VM1 is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task...

Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At C:\PowerCLI\update_vram_poweroff_snapshot-Copy.ps1:53 char:15

+     Wait-Task $shutvmtask

+               ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask

VM2 is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task...

Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At C:\PowerCLI\update_vram_poweroff_snapshot-Copy.ps1:53 char:15

+     Wait-Task $shutvmtask

+               ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask

Snapshot of VM1 is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task...

Snapshot of VM2 is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task...

Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At C:\PowerCLI\update_vram_poweroff_snapshot-Copy.ps1:62 char:15

+     Wait-Task $snapshotvmtask

+               ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask

Video Memory on VM: VM1 has been successfully set to 9000

Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At C:\PowerCLI\update_vram_poweroff_snapshot-Copy.ps1:85 char:19

+         Wait-Task $vramconfigtask

+                   ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask

The scrript will wait for the configuration task to be complete before the VMs are powered on

Video Memory on VM: VM2 has been successfully set to 9000

Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At C:\PowerCLI\update_vram_poweroff_snapshot-Copy.ps1:85 char:19

+         Wait-Task $vramconfigtask

+                   ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask

The scrript will wait for the configuration task to be complete before the VMs are powered on

VM1 is now being powered on. Please wait five to ten minutes before verifying new configuration

VM2 is now being powered on. Please wait five to ten minutes before verifying new configuration

Reply
0 Kudos