简体   繁体   English

如何在PowerCLI中快速找到具有串行端口的VM

[英]How can I quickly find VMs with serial ports in PowerCLI

I have a script that takes about 15 minutes to run, checking various aspects of ~700 VMs. 我有一个脚本,它运行大约15分钟,检查大约700个VM的各个方面。 This isn't a problem, but I now want to find devices that have serial ports attached. 这不是问题,但是我现在想查找连接了串行端口的设备。 This is a function I added to check for this: 这是我添加来检查的功能:

Function UsbSerialCheck ($vm)
{
    $ProbDevices = @()
    $devices = $vm.ExtensionData.Config.Hardware.Device
    foreach($device in $devices)
    {
        $devType = $device.GetType().Name
        if($devType -eq "VirtualSerialPort")
        {
            $ProbDevices += $device.DeviceInfo.Label
        }
    }
    $global:USBSerialLookup = [string]::join("/",$ProbDevices)
}

Adding this function adds an hour to the length of time the script runs, which is not acceptable. 添加此功能会使脚本运行时间增加一小时,这是不可接受的。 Is it possible to do this in a more efficient way? 有可能以更有效的方式做到这一点吗? All ways I've discovered are variants of this. 我发现的所有方式都是这种形式。

Also, I am aware that using global variables in the way shown above is not ideal. 另外,我知道以上述方式使用全局变量也不理想。 I would prefer not to do this; 我不想这样做。 however, I am adding onto an existing script, and using their style/formatting. 但是,我要添加到现有脚本中,并使用其样式/格式。

Appending to arrays ( $arr += $newItem ) in a loop doesn't perform well, because it copies all existing elements to a new array. 在循环中追加到数组( $arr += $newItem )效果不佳,因为它将所有现有元素复制到新数组中。 This should provide better performance: 这应该提供更好的性能:

$ProbDevices = $vm.ExtensionData.Config.Hardware.Device `
  | ? { $_.GetType().Name -eq 'VirtualSerialPort' } `
  | % { $_.DeviceInfo.Label }

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

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