下面有一份好的工作副本。我还修改了varibles而不是缩写。
但是,根据Good部分(在其他地方之外),如果存在内容问题,则希望内容也被检查三次。
如果存在Web异常,则会正确检查三(3)次。但是,如果服务器已启动并且正在运行,则不会检查内容是否重新检查它所查找的字符串:
为了清晰起见重写这个,就在之前:catch [Net.WebException]我确实尝试写了我正在寻找的部分,但无法让它工作。我可能在这方面不正确,因为[Net.WebException]可能会为此接受错误处理。
else
{
#BAD CONTENT
if ($Retrycount -gt 2){
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td align='center'> $type </td>"
Add-Content $fileName "<td align='center'> $stringTest is not found </td>"
Add-Content $fileName "</tr>"
Write-Host $stringTest ' is not found'
}
# STOP LOOPING AND LETS GO TO THE NEXT COMPUTER OR QUIT
$Stoploop = $true
}
Write-Host "$RetryCount $servern is not up and running retrying in 10 seconds..."
Start-Sleep -Seconds 10
$Retrycount = $Retrycount + 1
}
}
$Stoploop = $true
}
catch [Net.WebException] {
WebCheck_computers.txt包含:
SERVER01,10.10.10.10,80,http://10.10.10.10/
SERVER02,10.10.10.11,80,http://10.10.10.11/
SERVER03,10.10.10.12,80,http://10.10.10.12/
SERVER04,10.10.10.13,80,http://10.10.10.13/
现在,让我们展示当前的工作代码:
# http://www.thomasmaurer.ch/2010/07/powershell-simple-retry-logic/
$mypath = "d:\WORK\ps";
$serverlist = "$($mypath)\WebCheck_computers.txt";
$fileName = "$($mypath)\webtest.htm";
$checkfile = "$($mypath)\chk.htm";
$stringTest = "have a good database connection";
$flag = "webtestflag.txt";
##########################################################################################################
### CLEAR FILES BEFORE STARTING
Remove-Item $fileName
New-Item -ItemType file $fileName -Force
New-Item -ItemType file $checkfile -Force
##########################################################################################################
### FUNCTIONS
Function writeHtmlHeader {
param($fileName)
$date = (get-date).ToString('MM/dd/yyyy hh:mm:ss')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>Webpage Health Report</title>'
Add-Content $fileName '<STYLE TYPE="text/css">'
Add-Content $fileName "<!--"
Add-Content $fileName "td {"
Add-Content $fileName "font-family: Tahoma;"
Add-Content $fileName "font-size: 11px;"
Add-Content $fileName "border-"
Add-Content $fileName "border-right: 1px solid #999999;"
Add-Content $fileName "border-bottom: 1px solid #999999;"
Add-Content $fileName "border-"
Add-Content $fileName "padding-"
Add-Content $fileName "padding-right: 0px;"
Add-Content $fileName "padding-bottom: 0px;"
Add-Content $fileName "padding-"
Add-Content $fileName "}"
Add-Content $fileName "body {"
Add-Content $fileName "margin-"
Add-Content $fileName "margin-"
Add-Content $fileName "margin-right: 0px;"
Add-Content $fileName "margin-bottom: 10px;"
Add-Content $fileName ""
Add-Content $fileName "table {"
Add-Content $fileName "border: thin solid #000000;"
Add-Content $fileName "}"
Add-Content $fileName "-->"
Add-Content $fileName "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
Add-Content $fileName "<table width='100%' border='1'>"
Add-Content $fileName "<tr bgcolor='#CCCCCC'>"
Add-Content $fileName "<td colspan='4' height='25' align='center'>"
Add-Content $fileName "<font face='tahoma' color='#003399' size='4'><strong>Web Page Health Report - $date</strong></font>"
Add-Content $fileName "</td>"
Add-Content $fileName "</tr>"
}
# Function to write the HTML Footer to the file
Function writeHtmlFooter {
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}
Function sendEmail {
param($htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "Support<support@company.com>"
$to= New-Object System.Net.Mail.MailAddress "Support<support@company.com>"
$subject="Server WebPage HealthCheck - $Date"
$smtphost="1.1.1.1"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "</table>"
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td align='center'>Server Name</td>"
Add-Content $fileName "<td align='center'>Time Test Started</td>"
Add-Content $fileName "<td align='center'>Web Request Type</td>"
Add-Content $fileName "<td align='center'>Web Page Result</td>"
Add-Content $fileName "</tr>"
}
##########################################################################################################
### SCRIPT
remove-item $fileName
writeHtmlHeader $fileName
writeTableHeader $fileName
#writeTableFooter $fileName
#writeHtmlFooter $fileName
foreach ($computer in Get-Content $serverlist) {
$servern=$computer.split(",")[0]
$ip=$computer.split(",")[1]
$type = $computer.split(",")[2]
$url = $computer.split(",")[3]
$Date = Get-Date
$servern
if ($Computer -notmatch 'DB') {
$Stoploop = $false
[int]$Retrycount = "0"
do {
try {
$w = New-Object net.WebClient
$p = New-Object System.Net.WebProxy 'http://proxy:8080'
$p.UseDefaultCredentials = $true
$w.proxy=$p
$w.DownloadFile($url,$checkfile)
if (Test-Path $checkfile) {
## http://stackoverflow.com/questions/18633666/powershell-using-contains-to-check-if-files-contain-a-certain-word
$file = Get-Content $checkfile
$containsWord = $file | %{$_ -match $stringTest}
If($containsWord -contains $true -and $stringTest -gt 3) {
#GOOD CONTENT
if (Test-Path "$($mypath)\$servern$flag") {
remove-item "$($mypath)\$servern$flag"
}
}
else
{
#BAD CONTENT
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td align='center'> $type </td>"
Add-Content $fileName "<td align='center'> $stringTest is not found </td>"
Add-Content $fileName "</tr>"
Write-Host $stringTest ' is not found'
}
}
}
$Stoploop = $true
}
catch [Net.WebException] {
# AN EXCEPTION WAS PREVIOUSLY FOUND - DO NOT NEED TO GO THROUGH ALL THREE TESTS
if (Test-Path "$($mypath)\$servern$flag") {
$Stoploop = $true
}
# SERVER IS DEFINITELY OFF-LINE AND WE NEED TO GENERATE A REPORT
if ($Retrycount -gt 2){
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td bgcolor='#FF0000' align='center'><font color='Yellow'> $_.Exception.ToString() </font></td>"
Add-Content $fileName "</tr>"
Write-Host $_.Exception.ToString()
Write-Host $($mypath)\$servern$flag
Write-Host "$RetryCount $servern is not responding after 3 retries."
}
# STOP LOOPING AND LETS GO TO THE NEXT COMPUTER OR QUIT
$Stoploop = $true
}
else {
# LOOP COUNTER IS NOT DONE YET - KEEP GOING
Write-Host "$RetryCount $servern is not up and running retrying in 10 seconds..."
Start-Sleep -Seconds 10
$Retrycount = $Retrycount + 1
}
}
}
While ($Stoploop -eq $false)
}
}
#writeHtmlHeader $fileName
writeHtmlFooter $fileName
$date = (get-date).ToString('MM/dd/yyyy hh:mm:ss')
$file = Get-Content $fileName
##867
if ($(Get-Item $fileName).length -gt 1000) {
sendEmail $fileName
}
##########################################################################################################
### SCRIPT IS DONE