我正在检查网址的csv文件,以获取返回自定义对象的状态代码。之后我想把它写到一个文件中。我有这个工作正常,但我想格式化表,否则网址被切断。我得到了它的工作,但它没有将它添加到一个新的行。此外,一旦我开始工作,我想按代码状态排序,这样我就可以将所有的404放到顶部。
我尝试过format-table和其他一些东西
[pscustomobject]@{
Code = $statusCode
Date = Get-Date -f yyyyMMdd
URL = $url
} | Format-Table - Autosize
这可行,但不会每次创建新行,因为它没有。
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Web.Extensions
$inputPath = "\\Scripts\URLS\test.csv"
$outputPath = "\\Scripts\Results\statusCodeResults.txt"
$urlArray = Import-Csv -Path $inputPath | Select -ExpandProperty urls
function Get-WebStatus {
param($urlArray)
foreach ($url in $urlArray) {
try {
$request = Invoke-WebRequest -Uri $url -Method GET
$statusCode = $request.StatusCode
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__
}
[pscustomobject]@{
Code = $statusCode
Date = Get-Date -f yyyyMMdd
URL = $url
}
}
}
Get-WebStatus -urlArray $urlArray | Out-File $outputPath
希望它看起来像这样,但URL显示完整,然后最终排序,将所有404放在最顶层。
Code Date URL
---- ---- ---
200 20190404 bsca.com/provider/account-tools/login/home.jhtml
200 20190404 bsca.com/provider/home.jhtml
200 20190404 bsca.com/provider/eligibility-benefits/eligib...
200 20190404 bsca.com/provider/eligibility-benefits/home.j...
200 20190404 bsca.com/provider/claims/home-auth.jhtml
200 20190404 bsca.com/provider/guidelines-resources/patien...
200 20190404 bsca.com/provider/claims/search/home.jhtml
200 20190404 bsca.com/provider/claims/policies-guidelines/...
404 20190404 bsca.com/provider/claims/view-rationale/home....
200 20190404 bsca.com/provider/guidelines-resources/home.j...
正如评论中所提到的,直接盲目回答你的问题是使用-Width
cmdlet的Out-File
参数和任何适合你需要的值,例如:
Get-WebStatus -urlArray $urlArray | Out-File $outputPath -Width 200
对于那个奇怪的半泡泡排序之王你可以组合两个过滤后的数组,如下所示:
$url_responses_sorted = @(
$url_responses | Where-Object -Property Code -EQ 404
$url_responses | Where-Object -Property Code -NE 404
)
但我鼓励你考虑以[PSCustomObject]
格式保存你的CSV
数组;通过这种方式,您可以随时使用各种语言/工具加载数据并使用它。
Export-Csv -Path $outputPath
说到PowerShell,您可以通过Import-Csv
cmdlet加载数据,类似于加载$ urlArray的方式,然后对其进行排序,对其进行分组并过滤您需要的内容,如下所示:
PS C:\> $url_responses = Get-WebStatus -urlArray $urlArray
PS C:\> $url_responses
Code Date URL
---- ---- ---
200 20190405 https://stackoverflow.com/questions/55521742/issue-with-powershel-pscustomobject-only-writing-one-line...
404 20190405 https://google.com/non-existing-page
200 20190405 https://github.com/
0 20190405 https://non-existing.url/
404 20190405 https://google.com/non-existing-page-2
PS C:\> $url_responses | Sort-Object -Property Code -Descending
Code Date URL
---- ---- ---
404 20190405 https://google.com/non-existing-page-2
404 20190405 https://google.com/non-existing-page
301 20190405 https://google.com/
200 20190405 https://stackoverflow.com/questions/55521742/issue-with-powershel-pscustomobject-only-writing-one-line...
0 20190405 https://non-existing.url/
PS C:\> $url_responses | Group-Object -Property Code
Count Name Group
----- ---- -----
2 200 {@{Code=200; Date=20190405; URL=https://stackoverflow.com/questions/55521742/issue-w...
1 301 {@{Code=301; Date=20190405; URL=https://google.com/}}
2 404 {@{Code=404; Date=20190405; URL=https://google.com/non-existing-page}, @{Code=404; D...
1 0 {@{Code=0; Date=20190405; URL=https://non-existing.url/}}
PS C:\> $url_responses | Where-Object -Property Code -EQ 404
Code Date URL
---- ---- ---
404 20190405 https://google.com/non-existing-page
404 20190405 https://google.com/non-existing-page-2
Invoke-WebRequest
)HEAD
请求,那么您使用该方法的速度将受益于GET
:
$response = Invoke-WebRequest -Uri $Uri -Method HEAD
-MaximumRedirection 0
和-ErrorAction SilentlyContinue
(这种方式脚本抑制有关超出最大重定向计数的错误消息,这正是我们的意图)
$response = Invoke-WebRequest -Uri $Uri -Method HEAD -MaximumRedirection 0 -ErrorAction SilentlyContinue
Invoke-WebRequest
来抑制进度,但是我们可以像这样暂时全局禁用它:
$progressPreference = 'silentlyContinue'
$response = Invoke-WebRequest -Uri $Uri -Method HEAD -MaximumRedirection 0 -ErrorAction SilentlyContinue
$progressPreference = 'Continue'
当然,在您的情况下,您应该在循环之外放置该首选变量。