繁体   English   中英

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

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

我有一个脚本,它运行大约15分钟,检查大约700个VM的各个方面。 这不是问题,但是我现在想查找连接了串行端口的设备。 这是我添加来检查的功能:

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)
}

添加此功能会使脚本运行时间增加一小时,这是不可接受的。 有可能以更有效的方式做到这一点吗? 我发现的所有方式都是这种形式。

另外,我知道以上述方式使用全局变量也不理想。 我不想这样做。 但是,我要添加到现有脚本中,并使用其样式/格式。

在循环中追加到数组( $arr += $newItem )效果不佳,因为它将所有现有元素复制到新数组中。 这应该提供更好的性能:

$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