我创建了一个Hyper-V复制警报脚本,但是在遇到条件时很难使它发出警报

时间:2020-06-15 10:53:45

标签: powershell

$status = Get-VMReplication | Select-Object Name, State, Health, Mode, FrequencySec, PrimaryServer, ReplicaServer, ReplicaPort | ConvertTo-Html
$Company = ""
$ReplicationHealth = (Get-VMReplication)
function ReplicationHealthFailedTelegram {
    $token = (Get-Content -Path C:\temp\telegrambot\token.txt)
    $chatid = (Get-Content -Path C:\temp\telegrambot\chatid.txt)
    $Message = "Replication Failed"
    $Company = ""
    $status = Get-VMReplication | Select-Object Name, State, Health, Mode, FrequencySec, PrimaryServer, ReplicaServer, ReplicaPort | ConvertTo-Html
    & 'C:\Program Files\PowerShell\7\pwsh.exe' -Command { $token = (Get-Content -Path C:\temp\telegrambot\token.txt);$chatid = (Get-Content -Path C:\temp\telegrambot\chatid.txt); $status = Get-VMReplication | Select-Object Name, State, Health, Mode, FrequencySec, PrimaryServer, ReplicaServer, ReplicaPort | ConvertTo-Html;$Message = "Replication Failed";$Company = "Howard Matthews Partnership - Harrogate";Send-TelegramTextMessage -BotToken $token -ChatID $chatid -Message $Company $Message}
    }
function EmailAlert {
    $User = "alert@domain.co.uk"
    $File = (Get-Content C:\Temp\pw.txt | ConvertTo-SecureString)
    $MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
    -ArgumentList $User, $File

            $To = "domain.co.uk"
            $from = "domain.co.uk"
            $EmailSubject = "Hyper-V Replication Error $Company"
            $smtp = "auth.smtp.1and1.co.uk"
            $DefaultMessage="
                <p>Dear Help,</p>
                <p>Replication has failed for $Company </p>
                <p>$status</p>
                <p>The Robot Checker .<br><br>
                </p>"

            $MailMessage = @{
                    To = $To
                    From = $from
                    # BCC = $Bcc
                    Subject = $EmailSubject
                    Body = $DefaultMessage
                    priority = "High"
                    Smtpserver = $smtp
                    Credential = $MyCredential
                    ErrorAction = "SilentlyContinue" 
                }

            Send-MailMessage @MailMessage -bodyashtml
}

if ($ReplicationHealth.health) {
    "Critical","Warning"
    EmailAlert
    ReplicationHealthFailedTelegram
} else {
    $null
}

电子邮件会发送,但是无论复制状态是正常,警告还是严重,它都会发送电子邮件。如何添加健康状态为“严重”或“警告”时发送电子邮件给我,而在正常状态下不发送电子邮件给我的逻辑。

非常感谢!

1 个答案:

答案 0 :(得分:0)

这一点并没有达到您的预期

if ($ReplicationHealth.health) {
    "Critical","Warning"
    EmailAlert
    ReplicationHealthFailedTelegram
} else {
    $null
}

与您一起,如果您基本上是在询问; “ $ ReplicationHealth.Health是一件事情吗?”而不是检查“事物”的值,并且在这种情况下,假设运行Get-VMReplication没有任何问题,答案将始终是肯定的。

一个不同的例子来说明我的意思可能类似于以下内容

if($(Get-ChildItem -Path "$env:windir\System32\WindowsPowerShell\v1.0\powershell.exe").Extension) { "Yup, .Extension is a thing" } else { "Nah, it's not!" }
if($(Get-ChildItem -Path "$env:windir\System32\WindowsPowerShell\v1.0\powershell.exe").Extension -eq ".exe") { "Yup, .Extension is a thing and its value is `'.exe`'" } else { "Nah, it's not!" }

为了解决您的问题,您可以尝试类似

if($ReplicationHealth.Health -eq "Critical" -or $ReplicationHealth.Health -eq "Warning") {
    EmailAlert
    ReplicationHealthFailedTelegram
}