【问题标题】:How can I quickly find VMs with serial ports in PowerCLI如何在 PowerCLI 中快速找到带串口的虚拟机
【发布时间】:2013-09-04 22:28:02
【问题描述】:

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

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

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

另外,我知道以上述方式使用全局变量并不理想。我宁愿不这样做;但是,我正在添加到现有脚本中,并使用它们的样式/格式。

【问题讨论】:

    标签: powershell powershell-2.0 vmware powercli


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 2015-05-02
      • 2017-09-22
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      相关资源
      最近更新 更多