Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 仅当脱机IP/计算机未响应ping测试时发送电子邮件_Powershell - Fatal编程技术网

Powershell 仅当脱机IP/计算机未响应ping测试时发送电子邮件

Powershell 仅当脱机IP/计算机未响应ping测试时发送电子邮件,powershell,Powershell,我想修改下面的脚本,以便在ping未应答或服务器/IP地址脱机时发送电子邮件 以下脚本的问题是,当$FullServerFile中的IP/Computername脱机时,它不会发送电子邮件,但在这两种情况下都会发送电子邮件 # Email settings $Email = @{ # specify who gets notified To = 'xxxxxx@test.com', 'yyyy@test.com' # specify where the notificat

我想修改下面的脚本,以便在ping未应答或服务器/IP地址脱机时发送电子邮件

以下脚本的问题是,当
$FullServerFile
中的IP/Computername脱机时,它不会发送电子邮件,但在这两种情况下都会发送电子邮件

# Email settings
$Email = @{
    # specify who gets notified
    To = 'xxxxxx@test.com', 'yyyy@test.com'
    # specify where the notifications come from
    From = 'Server_Online@test.com'
    # specify the SMTP server
    SmtpServer = 'mailrelay.test.net'
}

$DownServerFile = "C:\DownServers.txt"
$FullServerFile = "C:\ServerList.txt"
# Create empty arrays for offline server lists
$PreviousDownServers = @()
$CurrentDownServers = @()
If (Test-Path -Path $DownServerFile) { $PreviousDownServers = Get-Content C:\TEMP\DownServers.txt }
Get-Content -Path $FullServerFile | ForEach-Object {
    $Server = $_
    if (test-Connection -ComputerName $Server -Count 2 -Quiet)
    {
        write-Host "$Server is alive and Pinging " -ForegroundColor Green
        #Send email if this server was down previously, but is now up
        if ($PreviousDownServers -notcontains $Server)
        {
            $date = (Get-Date).ToString('dddd dd/MM/yyyy HH:mm tt ') + "$([System.TimeZoneInfo]::FindSystemTimeZoneById((Get-WmiObject win32_timezone).StandardName))"
            $Subject = "$server Ping Monitor success" + $date
            $body = "$Server is now responding to ping."
            Send-MailMessage -Body $body -Subject $subject @Email
        }
    }
    else
    {
        $CurrentDownServers += $Server
        #Send email only if this is the first time the server is down
        if ($PreviousDownServers -notcontains $Server)
        {
            $date = (Get-Date).ToString('dddd dd/MM/yyyy HH:mm tt ') + "$([System.TimeZoneInfo]::FindSystemTimeZoneById((Get-WmiObject win32_timezone).StandardName))"
            $Subject = "$server [$($((Resolve-DnsName -Name $server -Type A).IPAddress))] Ping Monitor failure - $date "
            $body = "$Server is not responding to ping."
            Send-MailMessage -Body $body -Subject $subject @Email
        }
    }
    
}
$CurrentDownServers | Out-File $DownServerFile

如前所述,我认为第一次测试是为了查看服务器以前是否停机,错误地使用了
-notcontains

对于我(荷兰机器)
[System.TimeZoneInfo]::FindSystemTimeZoneById((Get-WmiObject win32\u timezone).StandardName)
不起作用,因为
(Get-WmiObject win32\u timezone).StandardName
返回本地化的名称,并且与时区ID不匹配

另外,我想知道为什么您保留了两个
downserver.txt
文件,而您只需要更新一个文件

应避免连接到
$CurrentDownServers+=$Server
中的数组,因为在每次迭代中,整个数组都会在内存中重新创建,而且非常耗时

我建议把代码改为

# Email settings
$Email = @{
    # specify who gets notified
    To = 'xxxxxx@test.com', 'yyyy@test.com'
    # specify where the notifications come from
    From = 'Server_Online@test.com'
    # specify the SMTP server
    SmtpServer = 'mailrelay.test.net'
}

$DownServerFile = "C:\DownServers.txt"
$FullServerFile = "C:\ServerList.txt"
$TimeZoneName   = (Get-WmiObject Win32_TimeZone).StandardName

$PreviousDownServers = if (Test-Path -Path $DownServerFile -PathType Leaf) { 
                            Get-Content -Path $DownServerFile | Where-Object { $_ -match '\S' }
                       } 
                       else { @() }
$AllServers = Get-Content -Path $FullServerFile | Where-Object { $_ -match '\S' }

$CurrentDownServers = foreach ($Server in $AllServers) {
    if (Test-Connection -ComputerName $Server -Count 2 -Quiet) {
        Write-Host "$Server is alive and Pinging" -ForegroundColor Green
        #Send email if this server was down previously, but is now up
        if ($PreviousDownServers -contains $Server) {
            $date = "{0:dddd dd/MM/yyyy HH:mm tt} $TimeZoneName" -f (Get-Date)
            $Email['Subject'] = "$Server Ping Monitor success" + $date
            $Email['Body']    = "$Server is now responding to ping."
            Send-MailMessage @Email
        }
    }
    else {
        Write-Host "$Server is down" -ForegroundColor Red
        # output the server to be collected in $CurrentDownServers
        $Server
        # Send email only if this is the first time the server is down *Commented out*
        # if ($PreviousDownServers -notcontains $Server) {
            $date = "{0:dddd dd/MM/yyyy HH:mm tt} $TimeZoneName" -f (Get-Date)
            $Email['Subject'] = "$Server [$($((Resolve-DnsName -Name $Server -Type A).IPAddress))] Ping Monitor failure - $date"
            $Email['Body']    = "$Server is not responding to ping."
            Send-MailMessage -Body $body -Subject $subject @Email
        # }
    }  
}
# re-write the $DownServerFile with servers that are down now or delete the file if all servers are up
if (!$CurrentDownServers) { 
    Remove-Item -Path $DownServerFile -ErrorAction SilentlyContinue
}
else {
    $CurrentDownServers | Set-Content $DownServerFile -Force
}

那么脚本的哪个特定部分失败了呢?你在调试器中运行过吗?问题不清楚。是否希望每次ping失败时都发送电子邮件?现在的方式是,当服务器还没有关闭时,它会发送电子邮件…第一次测试服务器以前关闭但现在启动是错误的,应该是
if($PreviousDownServers-contains$server)
(您在那里使用了
-notcontains
)。另外,在我的荷兰机器上,这个
[System.TimeZoneInfo]::FindSystemTimeZoneById((Get WmiObject win32_timezone).StandardName)
不起作用。你不是指
(获取WmiObject win32_时区)。StandardName
?是的,我想实现的是每次IP脱机时发送电子邮件。此脚本将每10分钟执行一次计划任务。感谢您在此线程中提供的更新和帮助。但是,这两个离线IP但是上面的脚本没有发送任何电子邮件?10.10.10.1处于活动状态,Pinging 33.33.33处于关闭状态5.6.7.8处于关闭状态down@SeniorSystemsEngineer很明显,在这次运行之前,活动的ip是活动的,测试是仅当上次该ip关闭时才发送电子邮件。对于停机的服务器,只有当它们不在Downservers.txt文件中时,测试才会发送电子邮件(只有当这是它们第一次没有响应ping时才有效)。尝试使用包含
10.10.10.1
的初始DownServers.txt。这将触发一封电子邮件“10.10.10.1正在响应ping。”以及另外两封包含“33.33.33.33没有响应ping”和“5.6.7.8没有响应ping”的邮件。@SeniorSystemsEngineer我现在稍微更改了脚本,以从我们读入的文件中删除任何空元素。@SeniorSystemsEngineer啊,我误解了这个问题。如果正如我现在所理解的,您希望在每次服务器脱机时发送电子邮件,那么只需删除第二个
If
条件。我已经更新了答案并注释掉了那些行。