Function powershell按钮只工作一次
我试图创建一个网站IsitUP检查器,以确定一组网站是否已启动 -我的网络不支持ICMP出站,因此Ping/测试连接已断开 -我使用的是Powershell v2,因此Web RequestGet也可以使用 -我决定对页面进行字符串处理,然后比较行以检查是否正确 到目前为止还不错 现在我只想能够刷新结果,并在单击按钮时提供时间戳。我现在的问题是foreach语句对每个站点状态使用相同的变量,并且不会重置所有变量。因此,当按下刷新按钮时,只刷新最后检查的站点 所以我的问题是我该怎么做 每次使用foreach语句为新对象设置一个变量? 重置整个表单上的所有相同变量Function powershell按钮只工作一次,function,variables,powershell,foreach,powershell-2.0,Function,Variables,Powershell,Foreach,Powershell 2.0,我试图创建一个网站IsitUP检查器,以确定一组网站是否已启动 -我的网络不支持ICMP出站,因此Ping/测试连接已断开 -我使用的是Powershell v2,因此Web RequestGet也可以使用 -我决定对页面进行字符串处理,然后比较行以检查是否正确 到目前为止还不错 现在我只想能够刷新结果,并在单击按钮时提供时间戳。我现在的问题是foreach语句对每个站点状态使用相同的变量,并且不会重置所有变量。因此,当按下刷新按钮时,只刷新最后检查的站点 所以我的问题是我该怎么做 每次使用fo
##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()
}
问题似乎在于拆除控制装置$RefreshButton.Add_Click{$objForm.Controls.remove$STAT}只会删除最后一个标签,因为$STAT就是这样设置的 在刷新函数的开始处放入$objform.controls | foreach对象{if$jform.Tag-eq STAT{$objform.controls.remove$j}},在STAT标签创建中放入$STAT.Tag=STAT将删除所有状态标签,然后正确替换这些状态标签