当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 进行 Web 测试

作者:精品下载站 日期:2024-12-14 07:50:30 浏览:13 分类:玩电脑

使用 PowerShell 进行 Web 测试


我在这里运行一个自托管 WordPress 博客,作为托管包的一部分。我的预算非常紧张,所以我很确定我会与其他租户共享资源。这意味着有时服务器不可用,通常只是很短的一段时间。我将 JetPack WordPress 插件配置为监控网站何时启动或关闭。但我想我应该使用 PowerShell 添加另一层测试。

同为 MVP 的 Chrissy LeMaire 在 Twitter 上给我发了一条消息,说我的博客已关闭,其中包含一个指向网站 http://downforeveryoneorjustme.com 的链接。我喜欢该网站的一点是它生成了一个非常简单的页面。当我看到时,我意识到我可以使用 Invoke-Webrequest 并构建一个简单的 PowerShell 函数来抓取页面结果。

获取该页面非常容易。

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

生成的对象将包含 parsedHTML 作为属性。通过查看浏览器中的页面源代码,我知道我想要的信息位于 DIV 标记中,其 ID 属性称为“容器”。

[玩转系统] 使用 PowerShell 进行 Web 测试

有了这些信息,我可以使用 getElementbyID() 方法来获取该项目。

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

幸运的是,该 ID 只有一个结果。如果有几个,我就必须包含额外的过滤代码。我所需要做的就是查看 InnerText 属性。

[玩转系统] 使用 PowerShell 进行 Web 测试

我可以通过只获取第一行来完善它。

[玩转系统] 使用 PowerShell 进行 Web 测试

我现在有一些有效的命令。是时候围绕它们构建一个函数了,这样我就有了一个简单、可重用的工具。我已将脚本文件作为要点放在 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 日期时间。

现在,测试站点是否正常运行非常容易。

[玩转系统] 使用 PowerShell 进行 Web 测试

我不确定我将用它构建什么样的监控和报告工具,但希望它值得写博客。同时,请尝试一下并让我知道您的想法。如果您在代码中遇到问题,请在要点页面的讨论部分中发布一些内容。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯