简体   繁体   中英

PowerCLI / Powershell to change monitor count of VM

I am trying to set up a script that can be ran to automagically change the monitor count of all VMs in a specific folder. Any help would be greatly appreciated. Below is the current iteration of the script:

#Variable Declaration
$vcenter = Gc-UserInputFromList("vcenter instances go here")
$tenant = Get-UserInput "Enter the 4 digit tenant ID"
[int]$NumDisplays

#vSphere connection
$session = Connect-Viserver -Server $vcenter

#Change Monitor Count
$vms = get-vm -Location $tenant

Foreach ($vm in $vms){
      $VideoAdapter = $vm.ExtensionData.Config.Hardware.Device | where {$vm.GetType().Name -eq "VirtualMachineVideoCard"}
      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
      $Config = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $Config.device.numDisplays = 3
      $Config.operation = "edit"
      $spec.deviceChange += $Config
      $VMView = $vm | Get-View
      $VMView.ReconfigVM($spec)
   }

Currently getting these errors despite my efforts to resolve

The property 'numDisplays' cannot be found on this object. Verify that the property exists and can be set.

Exception calling 'ReconfigVM' with "1" arguments: " Required property device is missing from data object of type VirtualDeviceConfigSpec while parsing serialized DataObject of type vim.vm.device.VirtualDeviceSpec

Was able to resolve the error.

Final version of loop is:

#Variable Declaration
$vcenter = Gc-UserInputFromList("vcenter instances go here")
$tenant = Get-UserInput "Enter the 4 digit tenant ID"
[int]$NumDisplays

#vSphere connection
$session = Connect-Viserver -Server $vcenter

#Change Monitor Count
$vms = get-vm -Location $tenant

#The script

Function setMonitorCount {

$vms = get-vm -Location $tenant

Foreach ($vm in $vms){
        if ($vm.PowerState -eq "PoweredOff") {
      $VideoAdapter = $vm.ExtensionData.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualMachineVideoCard"}
      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
      $Config = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $Config.device = $VideoAdapter
      $Config.device.numDisplays = 3
      $Config.operation = "edit"
      $spec.deviceChange += $Config
      $VMView = $vm | Get-View
      $VMView.ReconfigVM($spec)
      Write-Output "Changing monitor count for $vm and powering back on."
      $poweredOn = Start-VM $vm
      }

   }

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM