يوم جيد ، أريد مشاركة تعليمات بسيطة "كيف يمكنني التوقف عن الأزيز يدويًا لعشرات المضيفين. بدون تسجيل ورسائل نصية! "
من الانترنتهام : أنا لست مبرمجًا أو مسؤول نظام ، لكن يجب أن أعمل في وقت واحد مع مجموعة كبيرة من الخوادم والخدمات ، وقد كتبت هذا البرنامج النصي لراحتك.
لجميع الذين قد يجدون هذا
مفيدًا ، فضوليًا ، أسأل تحت القط.
لماذا بوويرشيللديّ خبرة في كتابة برامج Python البسيطة ، ولكنها تتطلب إما مترجمًا مثبتًا أو حق تشغيل exe. ، والذي ، كجزء من عملي ، غير ممكن دائمًا. ولكن في كثير من الأحيان يجد البرنامج النصي Powershell القدرة على الجري.
ليس الجميع يحب وحدة التحكم / المحطة
أعتقد أن هذا ليس تناقضًا ، وإلا فسيظل الجميع يستخدمون عملاء بريد وحدة التحكم ولن تظهر واجهة المستخدم الرسومية في أنظمة * nix.لأنني في حياتي اليومية يجب أن أستخدم Powershell بدائيًا ، لبداية حاولت أن أفهم ما إذا كان من الممكن تثبيت واجهة المستخدم الرسومية عليه. اتضح أنك تستطيع ، وحتى بسيطة للغاية:
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() # ""
إضافة عنصر الإخراج إلى هذا والحصول على نافذة
tautology 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() #
إضافة مكبرات الصوت
لذا فإن الأداة التي "قمت بالضغط عليها من خلال ياندكس" وقمت بتركيبها * .iso حصلت على واجهة المستخدم الرسومية منفصلة ، ولكن هذا لا يكفي. وبالمثل ، يمكنك عرض معلومات إمكانية الوصول ، ولكن للمراقبة سيكون عليك إعادة تشغيل البرنامج النصي في كل مرة.
أولاً ، ارسم رمزين وأضف القدرة على عرض الصور.
$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()
خاتمة
آمل أن يجد شخص ما هذا البرنامج التعليمي مفيدًا. إذا كنت قد فعلت هذا من المهد تقريبًا ، فتهانينا ، لم أقم بذلك. لكنني مستعد للاستماع إلى تعليقاتكم واقتراحاتكم.
بالنسبة لي ، كان من الجيد التنقيب وجمع مثل هذه الأداة الصغيرة ، ولكنها مفيدة في الحياة اليومية. لا يزال يتعين على الخطط تطبيق إضافة وإزالة العقد من واجهة المستخدم الرسومية ، وكذلك القيام
بنفس الشيء الذي كان يجب تطبيق تحديثات الحالة التلقائية بشكل
أساسي دون الحاجة إلى "الضغط" على الزر حتى لا يتعطل "powershell.exe".
أحدث نسخة من البرنامج النصي على
جيثب