我正在检查URL的csv文件中是否有返回自定义对象的状态代码。之后,我想将其写入文件。我的工作正常,但我想格式化表格,否则网址将被切断。我可以使用它,但是没有将其添加到新行中。同样,一旦我能正常工作,我想按代码状态进行排序,以便将所有404都放在顶部。
我尝试了格式表和其他一些东西
[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...
答案 0 :(得分:1)
如评论中所述,对您的问题的直接盲目的答案是使用-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'
当然,在您的情况下,您应该将分配该首选项的变量放在循环之外。