BBB36
Enthusiast
Enthusiast

Hey LucD, I made some modifications. Now the script works. I had to however remove some of the "tasktab" and error control portions in the #Shutdown VMs and #Power on VMs sections of the script for it to work. I'm sure I'm just missing something small but I needed to get this working ASAP and also don't have the knowledge yet to fix it quickly. Anyway, here's the final script. If you or anyone else can enhance it that would be greatly appreciated! Thanks so much for all your help!

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

$newVideoRamSize = 9000

$vmlist = Import-Csv .\vmlist.csv -UseCulture

$vmlist1 = $vmlist | select -ExpandProperty VM

$taskTab = @{ }

#Shutdown VMs

foreach ($vm in $vmlist1) {

    Write-Host $vm will shutdown in preparation for the Video Ram configuration change. -foregroundcolor green

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

}

Start-Sleep -Seconds 30

#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 $vm snapshot is being taken before the configuration change should a rollback be necessary. -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 30

#Update Video Ram

foreach ($vm in $vmlist1) {

$runningTasks = $taskTab.Count

while ($runningTasks -gt 0) {

   Get-Task | % {

       if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {

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

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

             return "$vm 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 $($vm.Name). Ensure it is powered off."

       }

          Write-Host $($vm.Name) video memory has been successfully set to $newVideoRamSize -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 30

#Power on VMs

foreach ($vm in $vmlist1) {

    Write-Host $vm is being powered back on. -foregroundcolor green

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

}

Start-Sleep -Seconds 30

Write-Host Configuration complete. Confirm new change in 5 to 10 minutes. -foregroundcolor green

Reply
0 Kudos