Bonjour, je veux partager une instruction simple "Comment puis-je arrêter de cingler manuellement une douzaine d'hôtes. Sans inscription et SMS! ”
Depuis InternetImportant : je ne suis ni programmeur ni administrateur système, mais je dois travailler simultanément avec un grand nombre de serveurs et de services et j'ai écrit ce script pour ma propre convenance.
A tous ceux qui peuvent trouver cela
utile, curieux, je demande sous cat.
Pourquoi PowershellJ'ai de l'expérience dans l'écriture de programmes Python simples, mais cela nécessite soit un interpréteur installé, soit le droit d'exécuter .exe, ce qui, dans le cadre de mon travail, n'est pas toujours possible. Mais le script Powershell trouve souvent la possibilité de s'exécuter.
Tout le monde n'aime pas la console / le terminal
Je pense que ce n'est pas une contradiction, sinon tout le monde utiliserait toujours les clients de messagerie de la console et l'interface graphique n'apparaîtrait pas dans les systèmes * nix.Étant donné que dans la vie de tous les jours, je dois utiliser Powershell de manière plutôt primitive, j'ai d'abord essayé de comprendre s'il était possible d'y fixer l'interface graphique. Il s'avère que vous pouvez, et même assez simple:
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() # ""
Ajoutez un élément de sortie à cela et obtenez une fenêtre de
tautologie Windwows élémentaire
? .
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() #
Ajouter des haut-parleurs
L'outil avec lequel j'ai "envoyé un ping à Yandex" et monté * .iso a donc une interface graphique distincte, mais ce n'est pas suffisant. De même, vous pouvez afficher les informations d'accessibilité, mais pour la surveillance, vous devrez redémarrer le script à chaque fois.
Tout d'abord, dessinez deux icônes et ajoutez la possibilité d'afficher des images.
$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)
Encore une fois, nos surnoms DNS sont méchants ...Et ajoutez un bouton pour pouvoir mettre à jour les relevés.
$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)
Génial, maintenant en production! ?
Ayant prescrit manuellement environ une demi-douzaine d'hôtes, j'ai soudain réalisé une chose - mais ce projet se terminera, les hôtes changeront et je devrai éditer le script à chaque fois. D'un côté, ça va, c'est la même décision pour vous. D'un autre côté,
c'est une décision pour vous bien-aimée, regardez ce qui se passe:
Le spectacle n'est pas pour les faibles de cœur 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() #
Même si vous observez la propreté et adhérez à la structure, un montage périodique ne provoquera pas les sensations les plus agréables.
Pour commencer, nous faisons le contrôle de disponibilité dans une fonction distincte, et le chemin du répertoire dans une variable distincte:
$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"} }
Nous transférerons les données de l'hôte dans un fichier path.txt externe, en l'écrivant sous la forme «ip / host-function-name /», et dans le document, nous les prendrons en tableaux. Nous rendons également le calcul de la position Y plus automatisé. Tout cela nous donne la possibilité de créer des étiquettes et des cases à cocher en appelant la fonction:

# , : 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 }
Nous ajouterons un endroit pour afficher l'heure de la dernière vérification, car c'est pratique, et nous ajouterons légèrement une action lorsque vous cliquerez sur le bouton.
$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 })
Et j'ai également joint une petite fonctionnalité,
pas un bug , qui, lorsqu'un hôte inaccessible a été détecté, a signalé non seulement un changement d'indicateur, mais aussi des cris des haut-parleurs.
Add-Type -AssemblyName System.Speech $voice = New-Object System.Speech.Synthesis.SpeechSynthesizer $voice.Rate = 5 $voice.Speak("! ! !")
Donc, après quelques embellissements et un aspect lisible
qui me convient , cela s'est avéré comme ceci:
Code de script complet 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()
Postface
J'espère que quelqu'un trouvera ce tutoriel utile. Si vous avez presque fait ça depuis le berceau, alors félicitations, je ne l'ai pas fait. Mais je suis prêt à écouter vos commentaires et suggestions.
Pour moi, c'était agréable de creuser et de collecter un utilitaire aussi petit mais utile dans la vie de tous les jours. Les plans doivent encore implémenter l'ajout et la suppression de nœuds de l'interface graphique, ainsi que faire la
même chose qui aurait dû être implémentée principalement des mises à jour d'état automatiques sans avoir à "piquer" le bouton et à ne pas bloquer "powershell.exe".
La dernière version du script sur
GitHub