我正在尝试根据CSV中的某些域来验证某些URI。
到目前为止,由于在PowerShell中使用了Invoke-WebRequest
cmdlet,该方法才有效。页面上的任何“ 200”响应,我都知道其有效。 (我还发现了一个易于使用的好功能)
但是,即时通讯卡住的地方只是使用经过验证的URL更新充满站点的CSV文件。
所以我的CSV例如
CompanyName,Domain Google, Google.com Microsoft, Microsoft.com NotARealCompany, NotARealWebsite123.com
当前,我的解决方法是只有一列(剥离标题,仅列出txt文件中的域),并且我的脚本现在将验证返回HTTP响应200的链接并将其导出到新txt文件(请参见代码)。
Function Test-URI {
<#
.Synopsis
Test a URI or URL
.Description
This command will test the validity of a given URL or URI that begins with either http or https. The default behavior is to write a Boolean value to the pipeline. But you can also ask for more detail.
Be aware that a URI may return a value of True because the server responded correctly. For example this will appear that the URI is valid.
test-uri -uri http://files.snapfiles.com/localdl936/CrystalDiskInfo7_2_0.zip
But if you look at the test in detail:
ResponseUri : http://files.snapfiles.com/localdl936/CrystalDiskInfo7_2_0.zip
ContentLength : 23070
ContentType : text/html
LastModified : 1/19/2015 11:34:44 AM
Status : 200
You'll see that the content type is Text and most likely a 404 page. By comparison, this is the desired result from the correct URI:
PS C:\> test-uri -detail -uri http://files.snapfiles.com/localdl936/CrystalDiskInfo6_3_0.zip
ResponseUri : http://files.snapfiles.com/localdl936/CrystalDiskInfo6_3_0.zip
ContentLength : 2863977
ContentType : application/x-zip-compressed
LastModified : 12/31/2014 1:48:34 PM
Status : 200
.Example
PS C:\> test-uri https://www.petri.com
True
.Example
PS C:\> test-uri https://www.petri.com -detail
ResponseUri : https://www.petri.com/
ContentLength : -1
ContentType : text/html; charset=UTF-8
LastModified : 1/19/2015 12:14:57 PM
Status : 200
.Example
PS C:\> get-content D:\temp\uris.txt | test-uri -Detail | where { $_.status -ne 200 -OR $_.contentType -notmatch "application"}
ResponseUri : http://files.snapfiles.com/localdl936/CrystalDiskInfo7_2_0.zip
ContentLength : 23070
ContentType : text/html
LastModified : 1/19/2015 11:34:44 AM
Status : 200
ResponseURI : http://download.bleepingcomputer.com/grinler/rkill
ContentLength :
ContentType :
LastModified :
Status : 404
Test a list of URIs and filter for those that are not OK or where the type is not an application.
.Notes
Last Updated: January 19, 2015
Version : 1.0
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.Link
Invoke-WebRequest
#>
[cmdletbinding(DefaultParameterSetName="Default")]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the URI path starting with HTTP or HTTPS",
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidatePattern( "^(http|https)://" )]
[Alias("url")]
[string]$URI,
[Parameter(ParameterSetName="Detail")]
[Switch]$Detail,
[ValidateScript({$_ -ge 0})]
[int]$Timeout = 30
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
Write-Verbose -message "Using parameter set $($PSCmdlet.ParameterSetName)"
} #close begin block
Process {
Write-Verbose -Message "Testing $uri"
Try {
#hash table of parameter values for Invoke-Webrequest
$paramHash = @{
UseBasicParsing = $True
DisableKeepAlive = $True
Uri = $uri
Method = 'Head'
ErrorAction = 'stop'
TimeoutSec = $Timeout
}
$test = Invoke-WebRequest @paramHash
if ($Detail) {
$test.BaseResponse |
Select ResponseURI,ContentLength,ContentType,LastModified,
@{Name="Status";Expression={$Test.StatusCode}}
} #if $detail
else {
if ($test.statuscode -ne 200) {
#it is unlikely this code will ever run but just in case
Write-Verbose -Message "Failed to request $uri"
write-Verbose -message ($test | out-string)
$False
}
else {
$True
}
} #else quiet
}
Catch {
#there was an exception getting the URI
write-verbose -message $_.exception
if ($Detail) {
#most likely the resource is 404
$objProp = [ordered]@{
ResponseURI = $uri
ContentLength = $null
ContentType = $null
LastModified = $null
Status = 404
}
#write a matching custom object to the pipeline
New-Object -TypeName psobject -Property $objProp
} #if $detail
else {
$False
}
} #close Catch block
} #close Process block
End {
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #close end block
}
$DomainList = Get-Content C:\temp\Sites.txt
foreach ($Domain in $DomainList) {
$Result = Test-URI -URI "http://$Domain" -Detail -Timeout 5
if ($Result.Status -eq "200") {
$Result.ResponseUri.OriginalString |
Out-File -FilePath C:\temp\DomainExport.txt -Append
} else {
Write-Host "$Link did not respond....."
}
}
我想发生的是要更新CSV,以便每个域都使用经过验证的响应URI(如果有效)或“无效”(例如无效)进行更新,例如
CompanyName,Domain Google, https://www.google.com Microsoft, https://www.microsoft.com NotARealCompany, InvalidDomain
这是我的尝试,但是我似乎无法将Import-CSV通过管道传输到ForEach ...
$DomainListOriginal = Import-Csv C:\temp\Sites.csv
$DomainListOriginal | ForEach ($Domain.Domain in $DomainList) {
$Result = Test-URI -URI "http://$Domain" -Detail -Timeout 5
$DomainListAfter = @()
If ($Result.Status -eq "200")
{$DomainListAfter = $Result.ResponseUri.OriginalString}
Else
{Write-Host "$Link did not respond....."}
}
$DomainListAfter | Export-Csv C:\temp\DomainExport.csv -NoTypeInformation
答案 0 :(得分:0)
要在@AnsgarWiechers上进行扩展,它将类似于以下内容:
$DomainList = Import-Csv C:\temp\Sites.csv
#Create Empty array to hold results
$Output = @()
foreach ($Domain in $DomainList) {
$Link = "http://$($Domain.Domain)"
$Result = Test-URI -URI $Link -Detail -Timeout 5
#Pre-load Temporary output object with CSV structure
$DomainOutput = $Domain
if ($Result.Status -eq "200") {
$DomainOutput.Domain = $Result.ResponseUri.OriginalString
} else {
$DomainOutput.Domain = "InvalidDomain"
Write-Host "$Link did not respond....."
}
#Add to our Output Array
$Output += $DomainOutput
}
$Output | Export-Csv C:\temp\DomainExport.csv -NoTypeInformation