Powershell 获取创建了{{{.exe的vm。仅列出第一次出现的早于xxx的快照

Powershell 获取创建了{{{.exe的vm。仅列出第一次出现的早于xxx的快照,powershell,where-clause,snapshot,Powershell,Where Clause,Snapshot,我想检查是否有超过一定天数的vm快照。第一个脚本列出了所有符合该条件的vm $Snapshots = Get-Vm | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created $Snapshots = Get-Vm | Get-snapshot where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Obje

我想检查是否有超过一定天数的vm快照。第一个脚本列出了所有符合该条件的vm

$Snapshots = Get-Vm | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created
$Snapshots = Get-Vm | Get-snapshot where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created

if ($Snapshots.count -gt 0) {
    Write-Host "Found snapshots older than X!", $vm.name -ForegroundColor Yellow

}
else {
    Write-Host "Found no snapshots older than X!" -ForegroundColor Green
}
其中,由于此脚本仅显示满足条件的第一个vm

$Snapshots = Get-Vm | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created
$Snapshots = Get-Vm | Get-snapshot where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created

if ($Snapshots.count -gt 0) {
    Write-Host "Found snapshots older than X!", $vm.name -ForegroundColor Yellow

}
else {
    Write-Host "Found no snapshots older than X!" -ForegroundColor Green
}
我想按第二个示例所示执行此操作的原因是,我需要向Icinga发送不同的状态代码。

您使用了错误的变量

$vm.name
改用:

$Snapshots.vm.Name
$Snapshots.Name
或用于所有行

Write-Host "Found snapshots older than X!", $Snapshots -ForegroundColor Yellow

如果我们假设
$Snapshots
包含您需要的数据,那么它将是一个集合。您可以使用索引
[0]
从该集合中有选择地拾取元素并显示其属性(
$Snapshots[0].VM.Name


$Snapshots=Get-VM | Get-Snapshot |选择对象VM,Name,Created |其中{$\.Created-lt(Get-Date).AddDays(-3)}
然后您可以访问实际的VM名称
$Snapshots.VM