与此问题类似我试图监视一组网站链接是否已启动并运行或没有响应。我在互联网上找到了相同的 PowerShell 脚本。
但是,我需要检查更具体的链接,而不是直接的网站链接,例如:
http://mypage.global/Chemical/
http://maypage2:9080/portal/site/hotpot/
当我尝试检查这些链接的状态时,我得到以下输出:
URL StatusCode StatusDescription ResponseLength TimeTaken
http://mypage.global/Chemical/ 0
http://maypage2:9080/portal/site/hotpot/ 0
上述链接需要我连接到VPN,但我可以从浏览器访问这些链接。
Invoke-WebRequest -Uri https://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url
的输出:
PS C:\Users\682126> Invoke-WebRequest -Uri https://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url
The term 'Invoke-WebRequest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:18
+ Invoke-WebRequest <<<< -Uri https://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url > tmp.txt
+ CategoryInfo : ObjectNotFound: (Invoke-WebRequest:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
$PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5472
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
我最近设置了一个脚本来执行此操作。
正如 David Brabant 指出的,您可以使用
System.Net.WebRequest
类来执行 HTTP 请求。
要检查它是否可以运行,您应该使用以下示例代码:
# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()
# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
Write-Host "Site is OK!"
}
Else {
Write-Host "The Site may be down, please check!"
}
# Finally, we clean up the http request by closing it.
If ($HTTP_Response -ne $null) { $HTTP_Response.Close() }
对于拥有 PowerShell 3 或更高版本的用户(即带有 Windows Management Framework 4.0 更新的 Windows Server 2012+ 或 Windows Server 2008 R2),您可以执行此一行操作,而不是调用
System.Net.WebRequest
:
$statusCode = wget http://stackoverflow.com/questions/20259251/ | % {$_.StatusCode}
你可以试试这个:
function Get-UrlStatusCode([string] $Url)
{
try
{
(Invoke-WebRequest -Uri $Url -UseBasicParsing -DisableKeepAlive).StatusCode
}
catch [Net.WebException]
{
[int]$_.Exception.Response.StatusCode
}
}
$statusCode = Get-UrlStatusCode 'httpstat.us/500'
$request = [System.Net.WebRequest]::Create('http://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url')
$response = $request.GetResponse()
$response.StatusCode
$response.Close()
您必须将 Windows PowerShell 更新到最低版本 4.0 才能运行以下脚本。
[array]$SiteLinks = "http://mypage.global/Chemical/test.html"
"http://maypage2:9080/portal/site/hotpot/test.json"
foreach($url in $SiteLinks) {
try {
Write-host "Verifying $url" -ForegroundColor Yellow
$checkConnection = Invoke-WebRequest -Uri $url
if ($checkConnection.StatusCode -eq 200) {
Write-Host "Connection Verified!" -ForegroundColor Green
}
}
catch [System.Net.WebException] {
$exceptionMessage = $Error[0].Exception
if ($exceptionMessage -match "503") {
Write-Host "Server Unavaiable" -ForegroundColor Red
}
elseif ($exceptionMessage -match "404") {
Write-Host "Page Not found" -ForegroundColor Red
}
}
}
下面是我用于基本 Web URL 测试的 PowerShell 代码。它包括接受无效证书并获取有关检查证书结果的详细信息的能力。
$CertificateValidatorClass = @'
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace CertificateValidation
{
public class CertificateValidationResult
{
public string Subject { get; internal set; }
public string Thumbprint { get; internal set; }
public DateTime Expiration { get; internal set; }
public DateTime ValidationTime { get; internal set; }
public bool IsValid { get; internal set; }
public bool Accepted { get; internal set; }
public string Message { get; internal set; }
public CertificateValidationResult()
{
ValidationTime = DateTime.UtcNow;
}
}
public static class CertificateValidator
{
private static ConcurrentStack<CertificateValidationResult> certificateValidationResults = new ConcurrentStack<CertificateValidationResult>();
public static CertificateValidationResult[] CertificateValidationResults
{
get
{
return certificateValidationResults.ToArray();
}
}
public static CertificateValidationResult LastCertificateValidationResult
{
get
{
CertificateValidationResult lastCertificateValidationResult = null;
certificateValidationResults.TryPeek(out lastCertificateValidationResult);
return lastCertificateValidationResult;
}
}
public static bool ServicePointManager_ServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
StringBuilder certificateValidationMessage = new StringBuilder();
bool allowCertificate = true;
if (sslPolicyErrors != System.Net.Security.SslPolicyErrors.None)
{
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch) == System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch)
{
certificateValidationMessage.AppendFormat("The remote certificate name does not match.\r\n", certificate.Subject);
}
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) == System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors)
{
certificateValidationMessage.AppendLine("The certificate chain has the following errors:");
foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus chainStatus in chain.ChainStatus)
{
certificateValidationMessage.AppendFormat("\t{0}", chainStatus.StatusInformation);
if (chainStatus.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.Revoked)
{
allowCertificate = false;
}
}
}
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable) == System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable)
{
certificateValidationMessage.AppendLine("The remote certificate was not available.");
allowCertificate = false;
}
System.Console.WriteLine();
}
else
{
certificateValidationMessage.AppendLine("The remote certificate is valid.");
}
CertificateValidationResult certificateValidationResult = new CertificateValidationResult
{
Subject = certificate.Subject,
Thumbprint = certificate.GetCertHashString(),
Expiration = DateTime.Parse(certificate.GetExpirationDateString()),
IsValid = (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None),
Accepted = allowCertificate,
Message = certificateValidationMessage.ToString()
};
certificateValidationResults.Push(certificateValidationResult);
return allowCertificate;
}
public static void SetDebugCertificateValidation()
{
ServicePointManager.ServerCertificateValidationCallback = ServicePointManager_ServerCertificateValidationCallback;
}
public static void SetDefaultCertificateValidation()
{
ServicePointManager.ServerCertificateValidationCallback = null;
}
public static void ClearCertificateValidationResults()
{
certificateValidationResults.Clear();
}
}
}
'@
function Set-CertificateValidationMode
{
<#
.SYNOPSIS
Sets the certificate validation mode.
.DESCRIPTION
Set the certificate validation mode to one of three modes with the following behaviors:
Default -- Performs the .NET default validation of certificates. Certificates are not checked for revocation and will be rejected if invalid.
CheckRevocationList -- Cerftificate Revocation Lists are checked and certificate will be rejected if revoked or invalid.
Debug -- Certificate Revocation Lists are checked and revocation will result in rejection. Invalid certificates will be accepted. Certificate validation
information is logged and can be retrieved from the certificate handler.
.EXAMPLE
Set-CertificateValidationMode Debug
.PARAMETER Mode
The mode for certificate validation.
#>
[CmdletBinding(SupportsShouldProcess = $false)]
param
(
[Parameter()]
[ValidateSet('Default', 'CheckRevocationList', 'Debug')]
[string] $Mode
)
begin
{
$isValidatorClassLoaded = (([System.AppDomain]::CurrentDomain.GetAssemblies() | ?{ $_.GlobalAssemblyCache -eq $false }) | ?{ $_.DefinedTypes.FullName -contains 'CertificateValidation.CertificateValidator' }) -ne $null
if ($isValidatorClassLoaded -eq $false)
{
Add-Type -TypeDefinition $CertificateValidatorClass
}
}
process
{
switch ($Mode)
{
'Debug'
{
[System.Net.ServicePointManager]::CheckCertificateRevocationList = $true
[CertificateValidation.CertificateValidator]::SetDebugCertificateValidation()
}
'CheckRevocationList'
{
[System.Net.ServicePointManager]::CheckCertificateRevocationList = $true
[CertificateValidation.CertificateValidator]::SetDefaultCertificateValidation()
}
'Default'
{
[System.Net.ServicePointManager]::CheckCertificateRevocationList = $false
[CertificateValidation.CertificateValidator]::SetDefaultCertificateValidation()
}
}
}
}
function Clear-CertificateValidationResults
{
<#
.SYNOPSIS
Clears the collection of certificate validation results.
.DESCRIPTION
Clears the collection of certificate validation results.
.EXAMPLE
Get-CertificateValidationResults
#>
[CmdletBinding(SupportsShouldProcess = $false)]
param()
begin
{
$isValidatorClassLoaded = (([System.AppDomain]::CurrentDomain.GetAssemblies() | ?{ $_.GlobalAssemblyCache -eq $false }) | ?{ $_.DefinedTypes.FullName -contains 'CertificateValidation.CertificateValidator' }) -ne $null
if ($isValidatorClassLoaded -eq $false)
{
Add-Type -TypeDefinition $CertificateValidatorClass
}
}
process
{
[CertificateValidation.CertificateValidator]::ClearCertificateValidationResults()
Sleep -Milliseconds 20
}
}
function Get-CertificateValidationResults
{
<#
.SYNOPSIS
Gets the certificate validation results for all operations performed in the PowerShell session since the Debug cerificate validation mode was enabled.
.DESCRIPTION
Gets the certificate validation results for all operations performed in the PowerShell session since the Debug certificate validation mode was enabled in reverse chronological order.
.EXAMPLE
Get-CertificateValidationResults
#>
[CmdletBinding(SupportsShouldProcess = $false)]
param()
begin
{
$isValidatorClassLoaded = (([System.AppDomain]::CurrentDomain.GetAssemblies() | ?{ $_.GlobalAssemblyCache -eq $false }) | ?{ $_.DefinedTypes.FullName -contains 'CertificateValidation.CertificateValidator' }) -ne $null
if ($isValidatorClassLoaded -eq $false)
{
Add-Type -TypeDefinition $CertificateValidatorClass
}
}
process
{
return [CertificateValidation.CertificateValidator]::CertificateValidationResults
}
}
function Test-WebUrl
{
<#
.SYNOPSIS
Tests and reports information about the provided web URL.
.DESCRIPTION
Tests a web URL and reports the time taken to get and process the request and response, the HTTP status, and the error message if an error occurred.
.EXAMPLE
Test-WebUrl 'http://websitetotest.com/'
.EXAMPLE
'https://websitetotest.com/' | Test-WebUrl
.PARAMETER HostName
The Hostname to add to the back connection hostnames list.
.PARAMETER UseDefaultCredentials
If present the default Windows credential will be used to attempt to authenticate to the URL; otherwise, no credentials will be presented.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Uri] $Url,
[Parameter()]
[Microsoft.PowerShell.Commands.WebRequestMethod] $Method = 'Get',
[Parameter()]
[switch] $UseDefaultCredentials
)
process
{
[bool] $succeeded = $false
[string] $statusCode = $null
[string] $statusDescription = $null
[string] $message = $null
[int] $bytesReceived = 0
[Timespan] $timeTaken = [Timespan]::Zero
$timeTaken = Measure-Command `
{
try
{
[Microsoft.PowerShell.Commands.HtmlWebResponseObject] $response = Invoke-WebRequest -UseDefaultCredentials:$UseDefaultCredentials -Method $Method -Uri $Url
$succeeded = $true
$statusCode = $response.StatusCode.ToString('D')
$statusDescription = $response.StatusDescription
$bytesReceived = $response.RawContent.Length
Write-Verbose "$($Url.ToString()): $($statusCode) $($statusDescription) $($message)"
}
catch [System.Net.WebException]
{
$message = $Error[0].Exception.Message
[System.Net.HttpWebResponse] $exceptionResponse = $Error[0].Exception.GetBaseException().Response
if ($exceptionResponse -ne $null)
{
$statusCode = $exceptionResponse.StatusCode.ToString('D')
$statusDescription = $exceptionResponse.StatusDescription
$bytesReceived = $exceptionResponse.ContentLength
if ($statusCode -in '401', '403', '404')
{
$succeeded = $true
}
}
else
{
Write-Warning "$($Url.ToString()): $($message)"
}
}
}
return [PSCustomObject] @{ Url = $Url; Succeeded = $succeeded; BytesReceived = $bytesReceived; TimeTaken = $timeTaken.TotalMilliseconds; StatusCode = $statusCode; StatusDescription = $statusDescription; Message = $message; }
}
}
Set-CertificateValidationMode Debug
Clear-CertificateValidationResults
Write-Host 'Testing web sites:'
'https://expired.badssl.com/', 'https://wrong.host.badssl.com/', 'https://self-signed.badssl.com/', 'https://untrusted-root.badssl.com/', 'https://revoked.badssl.com/', 'https://pinning-test.badssl.com/', 'https://sha1-intermediate.badssl.com/' | Test-WebUrl | ft -AutoSize
Write-Host 'Certificate validation results (most recent first):'
Get-CertificateValidationResults | ft -AutoSize
对于 powershell (7) 核心,这有效:
curl -I gourav.io
输出:
HTTP/1.1 308 Permanent Redirect
Date: Tue, 13 Apr 2021 20:29:43 GMT
Content-Type: text/plain
Connection: keep-alive
Location: https://gourav.io/
Refresh: 0;url=https://gourav.io/
server: Vercel
x-vercel-id: bom1::zfh9m-1618345783130-62d01e38e332
或
Invoke-WebRequest https://gourav.io
输出:
StatusCode : 200
StatusDescription : OK
Content : <!DOCTYPE html><html lang="en-US"><head><script async=""
src="https://www.googletagmanager.com/gtag/js?id=G-JF3BSQ1LL2"></script><script>
window.dataLayer = window.dataLayer || [];
…
RawContent : HTTP/1.1 200 OK
...
...
我使用它并且可以工作,(如果链接可用,则返回“Ok”),但如果链接不起作用,它会显示错误的红色字母。如何删除此代码片段上的错误报告?
$DownloadLink = "https://www.tightvnc.com/download/2.8.59/tightvnc-2.8.59-gpl-setup-bit.msi"
$NetAvailable = (Invoke-WebRequest -Uri $DownloadLink -UseBasicParsing -DisableKeepAlive -方法头).StatusDescription
$NetAvailable = 好的
If ($NetAvailable -eq "OK") {Write-Host "Attempting Download";wget -O "$Destination\$FileName" "$DownloadLink"}
Else {"";Write-Warning "Could NOT Download, Manually Copy $ProgramName to $Destination BEFORE Proceeding!";"";PAUSE}
但是如果链接不起作用会给我一个错误,然后移至“其他” 它有效,我只是想去掉错误字母。
这是使用 cmd.exe 执行相同/类似操作的方法:
如果你为 Windows 安装了curl https://curl.se/windows/你可以像这样做一个衬垫:
FOR /F %i IN (myurls.txt) DO (echo %i && c:\curl\bin\curl.exe -I %i | findstr HTTP) >> output.txt
这将回显所使用的 url,并在其下方写入 HTTP 响应。
注意:output.txt 需要存在,但为空,并且位于运行命令的同一文件夹中。如果没有放在环境路径中,请更改curl.exe路径以匹配其安装位置
**
$statusCode ="0"
# I am assigning the statuscode as 0. if website is running then next step will change it it 200 if website is not running then it will be 0 only
$statusCode = wget http://yourwebsite.com -UseBasicParsing | % {$_.StatusCode}
If ($statusCode -eq 200)
{
# run your script here ($NewResult = "1")
}
else
{
# run your script here ($NewResult = "0")
}
**