我正在尝试创建一个网站“IsitUP”Checker来确定一组网站是否已启动。
- 我的网络不支持ICMP出站,因此Ping / Test Connection已经不在了
- 我正在使用Powershell v2,因此Web-RequestGet也出局了
- 我决定对页面进行分组,然后比较该行以检查是否已启动。
到目前为止一切顺利。
现在我只想在单击按钮时刷新结果并提供时间戳。我现在的问题是foreach语句对每个站点状态使用相同的变量,它不会重置所有变量。因此,当按下刷新按钮时,仅刷新最后检查的站点。
所以我的问题是我怎么做 A)每次使用foreach语句为新对象设置变量? b)重置整个表单上的所有相同变量?
##This hides the powershell cmd pmompt
PowerShell.exe -windowstyle hidden {
## This is the APAN DNS array
$sites = "https://chat.apan.org","https://connect.apan.org","https://m.apan.org","https://mail.apan.org","https://www.apan.org"
$SitelocH = 10
$sitelocW = 10
$statlocH = 10
$statlocW = 150
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "APAN GO!"
$objForm.Opacity = (.95)
$objForm.Size = New-Object System.Drawing.Size(200,200)
$objForm.StartPosition = "WindowsDefaultLocation"
## APAN ICON
$RefreshButton = New-Object System.Windows.Forms.Button
$RefreshButton.Location = New-Object System.Drawing.Size(50,130)
$RefreshButton.Size = New-Object System.Drawing.Size(75,23)
$RefreshButton.Text = "Refresh"
$RefreshButton.Add_Click({$objForm.Controls.remove($STAT)})
$RefreshButton.Add_Click({Refresh})
$objForm.Controls.Add($RefreshButton)
foreach ($site in $sites)
{
## This is going to call a string for each site, then it is going to count the characters
$StatDescrption = (New-Object System.Net.WebClient).DownloadString($site) | Measure-Object -line |Select-Object -ExpandProperty lines
if ($StatDescrption -ge 1) {$StatDescrption = "OK!"} else {$StatDescrption ="DOWN"}
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size($sitelocW,$sitelocH)
$objLabel.Size = New-Object System.Drawing.Size(140,15)
$objLabel.Text = $site
$objForm.Controls.Add($objLabel)
$sitelocH1 = $SitelocH +20
$sitelocH = $sitelocH1
$STAT= New-Object System.Windows.Forms.Label
$STAT.Location = New-Object System.Drawing.Size($statlocW,$statlocH)
$STAT.Size = New-Object System.Drawing.Size(120,20)
$STAT.ForeColor ="green"
$STAT.Text = $StatDescrption
$objForm.Controls.Add($STAT)
$statlocH1 = $statlocH +20
$statlocH = $statlocH1
}
Function Refresh {
$statlocH = 10
foreach ($site in $sites) {
## This is going to call a string for each site, then it is going to count the characters
$StatDescrption = (New-Object System.Net.WebClient).DownloadString($site) | Measure-Object -line |Select-Object -ExpandProperty lines
if ($StatDescrption -ge 1) {$StatDescrption = "OK!!"} else {$StatDescrption ="DOWN"}
$STAT.Refresh
$STAT = New-Object System.Windows.Forms.Label
$STAT.Location = New-Object System.Drawing.Size($statlocW,$statlocH)
$STAT.Size = New-Object System.Drawing.Size(120,20)
$STAT.ForeColor ="green"
$STAT.Text = $StatDescrption
$objForm.Controls.Add($STAT)
$statlocH1 = $statlocH +20
$statlocH = $statlocH1
$time.Refresh
$TIME= New-Object System.Windows.Forms.Label
$TIME.Location = New-Object System.Drawing.Size(40,110)
$TIME.Size = New-Object System.Drawing.Size(120,20)
$TIME.Text = Get-Date
$objForm.Controls.Add($TIME)
}}
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
}
答案 0 :(得分:0)
问题似乎在于删除控件。 $RefreshButton.Add_Click({$objForm.Controls.remove($STAT)})
仅删除最后一个标签,因为这是$ STAT设置的内容。
在刷新功能开始时放入$objform.controls | foreach-object {if ($_.Tag -eq "STAT") {$objform.controls.remove($_)}}
,并在创建统计信息标签中$STAT.Tag = "STAT"
删除所有正确替换的状态标签。