【问题标题】:Script for Azure Backup notificationsAzure 备份通知脚本
【发布时间】:2016-07-11 21:36:08
【问题描述】:

我只是一个基本的,我是 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,如果它在那里,它应该返回true,如果不是false。不幸的是,它每次都只返回 false。

有人可以建议吗?有没有更好的方法来做到这一点?

【问题讨论】:

    标签: powershell event-log


    【解决方案1】:
    $check = Get-WinEvent ... *>$null
    

    您的redirection 正在抑制所有 输出,因此$check 始终 具有值$null,即布尔运算中的interpreted as $false。 p>

    您要使用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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 2020-05-12
      • 2013-10-01
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      相关资源
      最近更新 更多