祝您有美好的一天,我想分享一条简单的说明:“如何停止手动对十几台主机执行ping操作。 没有注册和短信!”
来自互联网重要说明 :我不是程序员或系统管理员,但是我必须同时使用大量服务器和服务,并且为自己方便起见编写了此脚本。
我想对所有可能觉得这
很有用,好奇的人问。
为什么选择Powershell我有编写简单的Python程序的经验,但是它需要安装的解释器或运行.exe的权限,这在我的工作中并不总是可能的。 但是Powershell脚本通常具有运行能力。
并非每个人都喜欢控制台/终端
我认为这不是矛盾,否则每个人仍然会使用控制台邮件客户端,而GUI不会出现在* nix系统中。因为在日常生活中我必须相当原始地使用Powershell,所以从一开始我就试图了解是否可以将GUI固定在它上面。 事实证明您可以做到,甚至很简单:
Add-Type -assembly System.Windows.Forms # $main_form = New-Object System.Windows.Forms.Form # "" # $main_form.Text ='Links up' # $main_form.Width = 300 # $main_form.Height = 200 # $main_form.AutoSize = $true # , $main_form.ShowDialog() # ""
向其添加输出元素并获得基本的Windwows
重言式窗口
? 。
Add-Type -assembly System.Windows.Forms # $main_form = New-Object System.Windows.Forms.Form # "" # $main_form.Text ='Links up' # $main_form.Width = 300 # $main_form.Height = 200 # $main_form.AutoSize = $true # , $Label = New-Object System.Windows.Forms.Label # , , Powershell $Label.Text = "! Windows-, ?" # $Label.Location = New-Object System.Drawing.Point(10,65) # (x,y) $Label.AutoSize = $true $main_form.Controls.Add($Label) # , $main_form.ShowDialog() #
添加发言人
因此,我用来“ ping Yandex”并安装* .iso的工具具有单独的GUI,但这还不够。 同样,您可以显示可访问性信息,但是为了进行监视,您每次都必须重新启动脚本。
首先,绘制两个图标并添加显示图像的功能。
$PictureBox = New-Object system.Windows.Forms.PictureBox # $PictureBox.width = 10 $PictureBox.height = 10 $PictureBox.location = New-Object System.Drawing.Point(178,12) # #<b>test-connection</b>, : #-Count - - #-computer - IP #-quiet - Boolean if ((test-connection -Count 1 -computer ya.ru -quiet) -eq $True) { $PictureBox.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox.imageLocation = "C:\Test\no.png" # } $PictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $main_form.Controls.Add($PictureBox)
同样,我们的DNS昵称很顽皮...并添加一个按钮,以便您可以更新读数。
$Button = New-Object System.Windows.Forms.Button $Button.Location = New-Object System.Drawing.Size(100,150) $Button.Size = New-Object System.Drawing.Size(80,30) $Button.Text = "Reload" # PictureBox $Button.Add_Click({ if ((Test-Connection -Count 1 -computer ya.ru -quiet) -eq $True) {$PictureBox.imageLocation = "C:\Test\yes.png"} Else {$PictureBox.imageLocation = "C:\Test\no.png"} if ((Test-Connection -Count 1 -computer 8.8.8.8 -quiet) -eq $True) {$PictureBox1.imageLocation = "C:\Test\yes.png"} Else {$PictureBox1.imageLocation = "C:\Test\no.png"} }) $main_form.Controls.Add($Button)
太好了,现在投入生产! ?
手动规定了大约六台主机后,我突然意识到一件事-但是该项目将结束,主机将更改,我每次都必须编辑脚本。 一方面,这没关系,这是您自己的决定。 另一方面,
这是一个为自己所爱的决定,看看发生了什么:
奇观不适合胆小的人 Add-Type -assembly System.Windows.Forms # $main_form = New-Object System.Windows.Forms.Form # "" # $main_form.Text ='Links up' # $main_form.Width = 300 # $main_form.Height = 200 # $main_form.AutoSize = $true # , $Label = New-Object System.Windows.Forms.Label # , , Powershell $Label.Text = "ya.ru ............................" # $Label.Location = New-Object System.Drawing.Point(15,10) # (x,y) $Label.AutoSize = $true $Label1 = New-Object System.Windows.Forms.Label # , , Powershell $Label1.Text = "8.8.8.8 ............................" # googl- DNS- $Label1.Location = New-Object System.Drawing.Point(15,30) # (x,y) $Label1.AutoSize = $true $Label2 = New-Object System.Windows.Forms.Label $Label2.Text = "192.168.xx ............................" $Label2.Location = New-Object System.Drawing.Point(15,50) $Label2.AutoSize = $true $Label3 = New-Object System.Windows.Forms.Label $Label3.Text = "192.168.xx ............................" $Label3.Location = New-Object System.Drawing.Point(15,70) $Label3.AutoSize = $true $Label4 = New-Object System.Windows.Forms.Label $Label4.Text = "10.0.xx ............................" $Label4.Location = New-Object System.Drawing.Point(15,90) $Label4.AutoSize = $true $Label5 = New-Object System.Windows.Forms.Label $Label5.Text = "162.102.xx ............................" $Label5.Location = New-Object System.Drawing.Point(15,110) $Label5.AutoSize = $true $PictureBox = New-Object system.Windows.Forms.PictureBox # $PictureBox.width = 10 $PictureBox.height = 10 # #<b>test-connection</b>, : #-Count - - #-computer - IP #-quiet - Boolean $PictureBox.location = New-Object System.Drawing.Point(235,12) if ((test-connection -Count 1 -computer ya.ru -quiet) -eq $True) { $PictureBox.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox.imageLocation = "C:\Test\no.png" # } $PictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $PictureBox1 = New-Object system.Windows.Forms.PictureBox # $PictureBox1.width = 10 $PictureBox1.height = 10 $PictureBox1.location = New-Object System.Drawing.Point(235,32) if ((test-connection -Count 1 -computer 8.8.8.8 -quiet) -eq $True) { $PictureBox1.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox1.imageLocation = "C:\Test\no.png" # } $PictureBox1.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $PictureBox2 = New-Object system.Windows.Forms.PictureBox # $PictureBox2.width = 10 $PictureBox2.height = 10 $PictureBox2.location = New-Object System.Drawing.Point(235,52) if ((test-connection -Count 1 -computer 192.168.xx -quiet) -eq $True) { $PictureBox2.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox2.imageLocation = "C:\Test\no.png" # } $PictureBox2.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $PictureBox3 = New-Object system.Windows.Forms.PictureBox # $PictureBox3.width = 10 $PictureBox3.height = 10 $PictureBox3.location = New-Object System.Drawing.Point(235,72) if ((test-connection -Count 1 -computer 192.168.xx -quiet) -eq $True) { $PictureBox3.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox3.imageLocation = "C:\Test\no.png" # } $PictureBox3.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $PictureBox4 = New-Object system.Windows.Forms.PictureBox # $PictureBox4.width = 10 $PictureBox4.height = 10 $PictureBox4.location = New-Object System.Drawing.Point(235,92) if ((test-connection -Count 1 -computer 10.0.xx -quiet) -eq $True) { $PictureBox4.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox4.imageLocation = "C:\Test\no.png" # } $PictureBox1.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $PictureBox5 = New-Object system.Windows.Forms.PictureBox # $PictureBox5.width = 10 $PictureBox5.height = 10 $PictureBox5.location = New-Object System.Drawing.Point(235,112) if ((test-connection -Count 1 -computer 162.102.xx -quiet) -eq $True) { $PictureBox5.imageLocation = "C:\Test\yes.png" # } Else { $PictureBox5.imageLocation = "C:\Test\no.png" # } $PictureBox5.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom $Button = New-Object System.Windows.Forms.Button $Button.Location = New-Object System.Drawing.Size(100,150) $Button.Size = New-Object System.Drawing.Size(80,30) $Button.Text = "Reload" # PictureBox $Button.Add_Click({ if ((Test-Connection -Count 1 -computer ya.ru -quiet) -eq $True) {$PictureBox.imageLocation = "C:\Test\yes.png"} Else {$PictureBox.imageLocation = "C:\Test\no.png"} if ((Test-Connection -Count 1 -computer 8.8.8.8 -quiet) -eq $True) {$PictureBox1.imageLocation = "C:\Test\yes.png"} Else {$PictureBox1.imageLocation = "C:\Test\no.png"} if ((Test-Connection -Count 1 -computer 192.168.xx -quiet) -eq $True) {$PictureBox2.imageLocation = "C:\Test\yes.png"} Else {$PictureBox2.imageLocation = "C:\Test\no.png"} if ((Test-Connection -Count 1 -computer 192.168.xx -quiet) -eq $True) {$PictureBox3.imageLocation = "C:\Test\yes.png"} Else {$PictureBox3.imageLocation = "C:\Test\no.png"} if ((Test-Connection -Count 1 -computer 10.0.xx -quiet) -eq $True) {$PictureBox4.imageLocation = "C:\Test\yes.png"} Else {$PictureBox4.imageLocation = "C:\Test\no.png"} if ((Test-Connection -Count 1 -computer 162.102.xx -quiet) -eq $True) {$PictureBox5.imageLocation = "C:\Test\yes.png"} Else {$PictureBox5.imageLocation = "C:\Test\no.png"} }) $main_form.Controls.Add($Label) # , $main_form.Controls.Add($Label1) $main_form.Controls.Add($Label2) $main_form.Controls.Add($Label3) $main_form.Controls.Add($Label4) $main_form.Controls.Add($Label5) $main_form.Controls.Add($PictureBox) $main_form.Controls.Add($PictureBox1) $main_form.Controls.Add($PictureBox2) $main_form.Controls.Add($PictureBox3) $main_form.Controls.Add($PictureBox4) $main_form.Controls.Add($PictureBox5) $main_form.Controls.Add($Button) $main_form.ShowDialog() #
即使您观察清洁度并坚持结构,定期编辑也不会产生最令人愉悦的感觉。
首先,我们在单独的函数中进行可用性检查,并在单独的变量中进行目录路径的检查:
$script_path = "C:\Test\PS ping" # PictureBox IP- . # . function get_status($PictureBox,$path_ip) { if ((Test-Connection -Count 1 -computer $path_ip -quiet) -eq $True) {$PictureBox.imageLocation = $script_path + "\yes.png"} Else {$PictureBox.imageLocation = $script_path + "\no.png"} }
我们将主机数据传输到一个外部path.txt文件中,并以“ ip / host-functional-name /”的形式编写,在文档中,我们将它们分成数组。 我们还使Y位置的计算更加自动化。 所有这些使我们有机会通过调用以下函数来创建标签和复选框:

# , : Label, IP-, Y Function Create_line($label,$path_ip,$caption, $top){ $label.Location = New-Object System.Drawing.Point(1, $top) $label.text = $path_ip+$caption $label.font = $font $Label.AutoSize = $true } # , : PictureBox, IP- Y Function Create_link($PictureBox,$path_ip, $top){ $PictureBox.width = 10 $PictureBox.height = 10 $PictureBox.location = New-Object System.Drawing.Point(210,$top) get_status -PictureBox $PictureBox -path_ip $path_ip $PictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom } $line = Get-content -LiteralPath $script_path"\path.txt" # $len = $line.Length # , $i = 0 # $ip = @() # ip $capt = @() # $Labels =@() # Label $PictureBoxs =@() # PictureBox while($i -lt $len){ $f = $line[$i].IndexOf("/") # $l = $line[$i].LastIndexOf("/") # $ip += $line[$i].Substring(0,$f) # ip $capt += $line[$i].Substring($f+1,$l-$f-1) # # Label Create_line -label ($label_obj = New-Object System.Windows.Forms.Label) -path_ip $ip[$i] -caption $capt[$i] -top $label_from_top # PictureBox Create_link -PictureBox ($PictureBox_obj = New-Object system.Windows.Forms.PictureBox) -path_ip $ip[$i] -top $label_from_top $label_from_top += 15 # "" Y $Labels += $label_obj # Label $PictureBoxs += $PictureBox_obj # PictureBox $main_form.Controls.Add($Labels[$i]) $main_form.Controls.Add($PictureBoxs[$i]) $i +=1 }
因为方便,我们将添加一个位置来显示上次检查的时间,并且当您单击按钮时,我们将稍微添加一个操作。
$Label0 = New-Object System.Windows.Forms.Label $Label0.Text = Get-Date $Label0.Location = New-Object System.Drawing.Point(80,180) $Label0.AutoSize = $true $main_form.Controls.Add($Label0) $Button.Add_Click({ while($i -lt $len){ get_status -PictureBox $PictureBoxs[$i] -path_ip $ip[$i] $i +=1 } $Label0.Text = Get-Date })
此外,我还附加了一个小功能,而
不是错误 ,当检测到无法访问的主机时,该
错误不仅报告了指示器的变化,而且还引起了扬声器的尖叫。
Add-Type -AssemblyName System.Speech $voice = New-Object System.Speech.Synthesis.SpeechSynthesizer $voice.Rate = 5 $voice.Speak("! ! !")
因此,经过一些修饰并为我提供了易于阅读的外观,结果是这样的:
完整的脚本代码 Add-Type -AssemblyName System.Speech Add-Type -assembly System.Windows.Forms $script_path = "C:\PS ping" $label_from_top = 10 $voice = New-Object System.Speech.Synthesis.SpeechSynthesizer $voice.Rate = 5 # PictureBox IP- . # . function get_status($PictureBox,$path_ip) { if ((Test-Connection -Count 1 -computer $path_ip -quiet) -eq $True) { $PictureBox.imageLocation = $script_path + "\yes.png" } Else { $PictureBox.imageLocation = $script_path + "\no.png" $voice.Speak("! " + $path_ip + ", !") } } # , : Label, IP-, Y Function Create_line($label,$path_ip,$caption, $top){ $label.Location = New-Object System.Drawing.Point(1, $top) $label.text = $path_ip+$caption $label.font = $font $Label.AutoSize = $true } # , : PictureBox, IP- Y Function Create_link($PictureBox,$path_ip, $top){ $PictureBox.width = 10 $PictureBox.height = 10 $PictureBox.location = New-Object System.Drawing.Point(210,$top) get_status -PictureBox $PictureBox -path_ip $path_ip $PictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom } $main_form = New-Object System.Windows.Forms.Form $main_form.Text ='Links up' # $main_form.Width = 300 # $main_form.Height = 200 # $main_form.AutoSize = $true # , $main_form.TopMost = $true # , $line = Get-content -LiteralPath $script_path"\path.txt" # $len = $line.Length # , $i = 0 # $ip = @() # ip $capt = @() # $Labels =@() # Label $PictureBoxs =@() # PictureBox while($i -lt $len){ $f = $line[$i].IndexOf("/") # $l = $line[$i].LastIndexOf("/") # $ip += $line[$i].Substring(0,$f) # ip $capt += $line[$i].Substring($f+1,$l-$f-1) # # Label Create_line -label ($label_obj = New-Object System.Windows.Forms.Label) -path_ip $ip[$i] -caption $capt[$i] -top $label_from_top # PictureBox Create_link -PictureBox ($PictureBox_obj = New-Object system.Windows.Forms.PictureBox) -path_ip $ip[$i] -top $label_from_top $label_from_top += 15 # "" Y $Labels += $label_obj # Label $PictureBoxs += $PictureBox_obj # PictureBox $main_form.Controls.Add($Labels[$i]) $main_form.Controls.Add($PictureBoxs[$i]) $i +=1 } # $Label0 = New-Object System.Windows.Forms.Label $Label0.Text = Get-Date $Label0.Location = New-Object System.Drawing.Point(80,180) $Label0.AutoSize = $true # PictureBox $Button = New-Object System.Windows.Forms.Button $Button.Location = New-Object System.Drawing.Size(100,200) $Button.Size = New-Object System.Drawing.Size(80,30) $Button.Text = "Reload" $Button.Add_Click({ while($i -lt $len){ get_status -PictureBox $PictureBoxs[$i] -path_ip $ip[$i] $i +=1 } $i = 0 $Label0.Text = Get-Date }) $main_form.Controls.Add($Button) $main_form.Controls.Add($Label0) $main_form.ShowDialog()
后记
我希望有人会觉得本教程有用。 如果您几乎从摇篮中做到了这一点,那么恭喜,我没有。 但我准备听听您的意见和建议。
对我来说,很高兴能在日常生活中挖掘并收集这么小的但有用的实用程序。 该计划仍然必须实现从GUI上添加和删除节点的功能,以及
执行原本应该自动进行状态更新的功能,而无需“拨动”该按钮,从而不会挂起“ powershell.exe”。
GitHub上脚本的最新版本