我正在使用iwr提出这样的请求:
iwr "http://someapi/param" -UseBasicParsing -Method Head
这会让我看到类似这样的标题:
$var1 = "Timing-Allow-Origin: *
X-CID: 1
Accept-Ranges: bytes
Content-Length: 43
Cache-Control: public,max-age=172800
Content-Type: image/gif
Other headers
如何检查标题是否包含此内容:
"Timing-Allow-Origin"="*"
"Cache-Control"="public,max-age=172800"
我试过
$var2.RawContent = iwr "http://someapi/param" -UseBasicParsing -Method Head
Write-Host ($var2.RawContent -like "*Timing-Allow-Origin: *")
但由于某种原因,这会返回false。有没有办法做到这一点?
答案 0 :(得分:3)
Invoke-Webrequest
返回的Headers为IDictionary。 您可以通过检索所需密钥的值来进行测试。
$response = iwr "http://someapi/param" -UseBasicParsing -Method Head
if ($response.Headers["Cache-Control"] -eq "public,max-age=172800") {
Write-Output "Found"
} else {
Write-Output "Not found"
}