【问题标题】:PowerCLI Snapshots ArrayPowerCLI 快照阵列
【发布时间】:2012-07-06 15:21:42
【问题描述】:

现在有点难过,想知道社区是否可以快速推动我,帮助我继续我正在工作的项目。

在我正在处理的程序中,我试图从一个数组中获取 6 个最新元素。我想将快照变量放在数组中,以便获取数组中的所有快照。这是目前让我感到困惑的代码部分:

$server = "test"
$date = get-date
$tempArray = @()
$snapshot = get-snapshot -VM "test"

foreach ($item in $snapshot){
    $tempArray += $item
}

$tempArray | sort
for ($i = 0; $i -le $tempArray.length-6; $i++){
    remove-item $tempArray[$i]
}

我是否实现了在我的数组中获取 $snapshot 变量的目标,并且我的 for 循环是否正确地管理删除除了 6 个最新的变量之外的所有变量?

编辑:修复了以前没有注意到的小问题。

【问题讨论】:

    标签: arrays powershell vmware powercli


    【解决方案1】:

    您的代码有几个问题。我不确定这是否会修复您的脚本,但这些似乎是您应该首先解决的明显问题。

    foreach ($item in $snapshot){
        $tempArray++ -> this should be $tempArray += $item, right? if you are adding $item to the tempArray
    }
    
    $tempArray | sort
    for ($i = 0; $i -le $tempArray.length-6; $i++){
        remove-item $snapshot -> this should be remove-item $tempArray[$i], right?
    }
    

    【讨论】:

      【解决方案2】:

      按创建的时间戳属性反向排序,然后在选择对象中使用Skip 来获取6 个最新之后的所有内容

      $snapshot = get-snapshot -VM "test"
      
      $snapshot | sort created -descending | select -Skip 6 | Remove-Snapshot -Confirm:$false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-28
        • 2014-10-02
        相关资源
        最近更新 更多