Azure 备份通知脚本

Script for Azure Backup notifications

我只是一个基础,我是Powershell的新手。正在尝试使下面的语句起作用。

$date = (Get-Date).AddDays(-1)

$currentdate = Get-Date -Format d

$check = Get-WinEvent -FilterHashtable @{LogName="CloudBackup";StartTime=$date;ID=3} *>$null

if ($check -eq $true) {
  Write-Host "`nOK: Azure Backup was successful on $currentdate"
  exit 0
} else {
  Write-Host "`nCritical: Problem with Azure Backup - $currentdate"
  exit 2
}

特别是 if ($check -eq $true) 似乎没有达到预期的效果。由于 $check 正在检查事件日志中的事件 ID 3,如果它在那里,它应该 return true,如果不是 false。不幸的是,return每次都是错误的。

有人可以指点一下吗?有更好的方法吗?

$check = Get-WinEvent ... *>$null

你的 redirection is suppressing all output, so $check always has the value $null, which is interpreted as $false 在布尔运算中。

您要使用 automatic variable $? 来检查上次 PowerShell 操作是否成功。

if ($?) {
  Write-Host "OK: Azure Backup was successful on $currentdate"
  exit 0
} else {
  Write-Host "Critical: Problem with Azure Backup - $currentdate"
  exit 2
}