此脚本从测试文件中获取输入,将结果写入屏幕,文件,并在特定条件下生成电子邮件。问题是,如果有大量主机,则速度极慢。我已经研究过多线程,但我是PS新手并且无法弄明白。任何帮助,将不胜感激。这是脚本。
# start looping here
Do{
$available = @()
$notavailable = @()
$ComputerName = @()
#Path to text file of hostnames we want to monitor
$ComputerName = Get-Content hosts.txt
Write-Host (Get-Date)
# Read the File with the Hosts every cycle, this way to can add/remove hosts
# from the list without touching the script/scheduled task,
# also hash/comment (#) out any hosts that are going for maintenance or are down.
$ComputerName | Where-Object {!($_ -match "#")} |
#"test1","test2" | Where-Object {!($_ -match "#")} |
ForEach-Object {
if(Test-Connection -ComputerName $_ -Count 1 -ea silentlycontinue)
{
# if the Host is available then write it to the screen
$value = @()
$value = $_
Write-Host "Available host ---> "$_ -ForegroundColor Green
[String[]]$available += $_
if ($value -notmatch "[a-z]")
{
$hosttoip = @()
$iptohost = @()
$parsemac = @()
$getmac = @()
$getmac = @()
$findstr = @()
$iptoip = @()
$getmac = nbtstat -A $_ | findstr "Mac Address = **-**-**-**-**" | Select-String -Pattern "Mac Address" -SimpleMatch
$iptoip = $_
$iptohost = [System.Net.Dns]::gethostbyaddress($_).Hostname
$findstr = findstr "$_" output.csv
Write-Host $_ "Responds with:" -ForegroundColor White
Write-Host "Hostname" -ForegroundColor White
Write-Host "$iptohost" -ForegroundColor Cyan
Write-Host "IP Address:" -ForegroundColor White
Write-Host "$_" -ForegroundColor Cyan
Write-Host "Mac Address:" -ForegroundColor White
Write-Host "$getmac`n" -ForegroundColor Cyan
}
if ($value -match "[a-z]")
{
$iptohost = @()
$hosttoip = @()
$hosttohost =@()
$parsemac = @()
$getmac = @()
$getmac = @()
$findstr = @()
$hosttohost = $_
$getmac = nbtstat -a $_ | findstr "Mac Address = **-**-**-**-**" | Select-String -Pattern "Mac Address" -SimpleMatch
$hosttoip = [System.Net.Dns]::GetHostByName($_).AddressList[0].IpAddressToString
$findstr = findstr "$_" output.csv
Write-Host $_ "Responds with:" -ForegroundColor White
Write-Host "Hostname" -ForegroundColor White
Write-Host "$_" -ForegroundColor Cyan
Write-Host "IP Address:" -ForegroundColor White
Write-Host "$hosttoip" -ForegroundColor Cyan
Write-Host "Mac Address:" -ForegroundColor White
Write-Host "$getmac`n" -ForegroundColor Cyan
}
if ($findstr -eq $Null)
{
$WriteFile = @()
$WriteFile = "$_,$iptohost $hosttohost,$hosttoip $iptoip,$getmac"
$WriteFile | Out-file output.csv -Encoding ascii -Append
}
# if the Host was out and is now backonline, remove it from the OutageHosts list
if ($OutageHosts -ne $Null)
{
if ($OutageHosts.ContainsKey($_))
{
Write-host $_ "was on the outages list and came back online. Sending email notification" -ForegroundColor Yellow
$OutageHosts.Remove($_)
$Now = Get-date
if ($notifyonServerBackOnline)
{
$Body = "$_ is online right now $getmac - If available, the MAC address and hostname/IP will be recorded in ScriptLocation\output.csv"
Send-MailMessage -Body "$body" -to $tonotification -from $fromnotification -Subject "Monitored host $_ is up" -SmtpServer $smtpserver
}
}
}
}
else
{
# If the host is unavailable, give a warning to screen
Write-Host "Unavailable host ------------> "$_ -ForegroundColor Red
if(!(Test-Connection -ComputerName $_ -Count 2 -ea silentlycontinue))
{
# If the host is still unavailable for 4 full pings, write error and send email
Write-Host "Unavailable host ------------> "$_ -ForegroundColor Red
[Array]$notavailable += $_
if ($OutageHosts -ne $Null)
{
if (!$OutageHosts.ContainsKey($_))
{
# First time down add to the list and send email
Write-Host "$_ Is not in the OutageHosts list, first time down"
$OutageHosts.Add($_,(get-date))
$Now = Get-date
if ($notifyonServerDown)
{
$Body = "$_ has not responded for 5 pings at $Now"
Send-MailMessage -Body "This message was generated by our ping monitoring script`n$body" -to $tonotification -from $fromnotification -Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
else
{
# If the host is in the list do nothing for 1 hour and then remove from the list.
Write-Host "$_ Is in the OutageHosts list"
if (((Get-Date) - $OutageHosts.Item($_)).TotalMinutes -gt $EmailTimeOut)
{
$OutageHosts.Remove($_)
}
}
}
else
{
# First time down create the list and send email
Write-Host "Adding $_ to OutageHosts."
$OutageHosts = @{$_=(get-date)}
$Now = Get-date
if ($notifyonServerDown)
{
$Body = "$_ has not responded for 5 pings at $Now"
Send-MailMessage -Body "This message was generated by our ping monitoring script`n$body" -to $tonotification -from $fromnotification -Subject "Host $_ is down" -SmtpServer $smtpserver
}
}
}
}
}
Write-Host ""
Write-Host "Sleeping $SleepTimeOut seconds"
Start-Sleep -Seconds $SleepTimeOut
if ($OutageHosts.Count -gt $MaxOutageCount)
{
# If there are more than a certain number of host down in an hour abort the script.
$Exit = $True
$body = $OutageHosts | Out-String
if ($notifyonMaxOutageCount)
{
Send-MailMessage -Body "This message was generated by our ping monitoring script`n$body" -to $tonotification -from $fromnotification -Subject "More than $MaxOutageCount Hosts down, monitoring aborted" -SmtpServer $smtpServer
}
}
}
while ($Exit -ne $True)