ค้นหาเว็บไซต์

การทดสอบเว็บด้วย PowerShell


ฉันใช้งานบล็อก WordPress ที่โฮสต์เองที่นี่โดยเป็นส่วนหนึ่งของแพ็คเกจที่โฮสต์ ฉันดำเนินการนี้ด้วยงบประมาณที่จำกัดมาก ดังนั้นฉันจึงค่อนข้างแน่ใจว่าจะแชร์ทรัพยากรกับผู้เช่ารายอื่น ซึ่งหมายความว่าบางครั้งเซิร์ฟเวอร์ไม่พร้อมใช้งาน ซึ่งโดยปกติจะเป็นช่วงระยะเวลาสั้นๆ เท่านั้น ฉันมีปลั๊กอิน JetPack WordPress ที่กำหนดค่าให้ตรวจสอบเมื่อไซต์ขึ้นหรือลง แต่ฉันคิดว่าฉันจะเพิ่มการทดสอบอีกชั้นหนึ่งโดยใช้ PowerShell

Chrissy LeMaire เพื่อน MVP ส่งข้อความถึงฉันทาง Twitter ว่าบล็อกของฉันใช้งานไม่ได้พร้อมลิงก์ไปยังเว็บไซต์ http://downforeveryoneorjustme.com สิ่งที่ฉันชอบเกี่ยวกับเว็บไซต์นี้คือมันสร้างเพจที่เรียบง่ายมาก เมื่อฉันเห็นว่าฉันรู้ว่าฉันสามารถใช้ Invoke-Webrequest และสร้างฟังก์ชัน PowerShell แบบง่าย ๆ เพื่อขูดผลลัพธ์ของเพจได้

มันง่ายมากที่จะได้รับหน้า

$w = Invoke-Webrequest "http://downforeveryoneorjustme.com/blog.jdhitsolutions.com" -DisableKeepAlive

วัตถุผลลัพธ์จะรวม parsedHTML เป็นคุณสมบัติ ฉันรู้จากการดูแหล่งที่มาของหน้าในเบราว์เซอร์ของฉันว่าข้อมูลที่ฉันต้องการอยู่ในแท็ก DIV ที่มีคุณสมบัติ ID ที่เรียกว่า "คอนเทนเนอร์"

ด้วยข้อมูลนี้ ฉันสามารถใช้เมธอด getElementbyID() เพื่อรับไอเท็มนี้ได้

$x = $w.ParsedHtml.getElementById("container")

โชคดีที่ ID นี้มีเพียงผลลัพธ์เดียว หากมีหลายรายการ ฉันจะต้องรวมโค้ดการกรองเพิ่มเติม สิ่งที่ฉันต้องทำคือดูคุณสมบัติ InnerText

ฉันสามารถปรับแต่งสิ่งนี้ได้โดยรับเพียงบรรทัดแรก

ตอนนี้ฉันมีคำสั่งบางอย่างที่ใช้งานได้ ถึงเวลาสร้างฟังก์ชันรอบๆ ดังนั้นฉันจึงมีเครื่องมือที่ใช้งานง่ายและนำกลับมาใช้ใหม่ได้ ฉันได้ใส่ไฟล์สคริปต์เป็นส่วนสำคัญใน GitHub

#requires -version 4.0

# get most current version at https://gist.github.com/jdhitsolutions/3046b3aafcbd89a6d857f7a7a5586e47

Function Test-UporDown {

<#
.Synopsis
Test if a web site is down or if it is just you.

.Description
The function uses the web site DownForEveryoneOrJustme.com to test is a web site or domain is up or down, or if the problem is just you. The command will write a custom object to the pipeline. See examples.
.Parameter Name
The name of a web site or domain such as google.com or powershell.org.

.Parameter UseUniversalTime
Convert the tested date value to universal or GMT time.

.Example
PS C:\> test-upordown pluralsight.com 

Name            IsUp Date                 
----            ---- ----                 
pluralsight.com True 12/22/2016 9:17:00 AM

.Example
PS S:\> test-upordown pluralsight.com -UseUniversalTime

Name            IsUp Date                 
----            ---- ----                 
pluralsight.com True 12/22/2016 2:17:41 PM

.Example
PS S:\> test-upordown foofoo.edu -Verbose
VERBOSE: [BEGIN  ] Starting: Test-UporDown
VERBOSE: [PROCESS] Testing foofoo.edu
VERBOSE: GET http://downforeveryoneorjustme.com/foofoo.edu/ with 0-byte payload
VERBOSE: received 2256-byte response of content type text/html;charset=utf-8
VERBOSE: [PROCESS] It's not just you! http://foofoo.edu looks down from here. 

VERBOSE: [END    ] Ending: Test-UporDown
Name        IsUp Date                 
----        ---- ----                 
foofoo.edu False 12/22/2016 9:19:13 AM

.Notes
version: 1.0

Learn more about PowerShell:
Essential PowerShell Learning Resources
.Link Invoke-WebRequest #> [cmdletbinding()] Param( [Parameter( Position = 0, Mandatory, HelpMessage = "Enter a web site name, such as google.com.", ValueFromPipeline )] [ValidateNotNullorEmpty()] [ValidateScript({ $_ -notmatch "^http"})] [string]$Name, [Switch]$UseUniversalTime ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { Write-Verbose "[PROCESS] Testing $Name" $response = Invoke-WebRequest -uri "http://downforeveryoneorjustme.com/$Name/" -DisableKeepAlive #get the result text from the HTML document $text = $response.ParsedHtml.getElementById("container").InnerText #the first line has the relevant information that looks like this: # It's just you. http://blog.jdhitsolutions.com is up $reply = ($text -split "`n" | Select -first 1) Write-Verbose "[PROCESS] $reply" If ($UseUniversalTime) { Write-Verbose "[PROCESS] Using universal time (GMT)" $testDate = (Get-Date).ToUniversalTime() } else { $testDate = Get-Date } #write a result object to the pipeline [pscustomObject]@{ Name = $Name IsUp = $reply -match "\bis up\b" Date = $TestDate } } #process End { Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)" } #end } #define an optional alias Set-Alias -Name tup -Value Test-UporDown

สคริปต์ยังมีนามแฝงด้วย ฉันยังเพิ่มพารามิเตอร์เพื่อส่งคืนวันที่ทดสอบตามเวลา GMT หากคุณต้องการ

ตอนนี้มันง่ายมากที่จะทดสอบว่าไซต์ขึ้นหรือลง

ฉันไม่แน่ใจว่าฉันจะสร้างเครื่องมือติดตามและรายงานประเภทใดด้วยสิ่งนี้ แต่หวังว่ามันจะคุ้มค่ากับการเขียนบล็อก ในระหว่างนี้ โปรดลองดูและแจ้งให้เราทราบว่าคุณคิดอย่างไร หากคุณประสบปัญหากับโค้ด โปรดโพสต์บางอย่างในส่วนการสนทนาในหน้าสรุปสาระสำคัญ

บทความที่เกี่ยวข้อง