简体   繁体   English

PowerCLI / Powershell 更改 VM 的监视器计数

[英]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.我正在尝试设置一个脚本,该脚本可以运行以自动更改特定文件夹中所有 VM 的监视器计数。 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.在此对象上找不到属性“numDisplays”。 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使用“1”参数调用“ReconfigVM”的异常:“在解析 vim.vm.device.VirtualDeviceSpec 类型的序列化 DataObject 时,VirtualDeviceConfigSpec 类型的数据对象缺少所需的属性设备

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
      }

   }

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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