powershell - For-each 循环无法处理所有字符串
问题描述
我正在尝试使用一堆字符串和一个锚 URL 生成 URL
s.txt 中的字符串是
123
234
345
锚点 URL 是https://testurl.com/prod/hostdetails.php?qs=
我正在使用的代码
$ur = gc C:\temp\s.txt
foreach($u in $ur) {
$test = invoke-webrequest -uri "https://testurl.com/prod/hostdetails.php?qs=$u" -UseDefaultCredentials
}
$test
但它仅返回数据
https://testurl.com/prod/hostdetails.php?qs=345
解决方案
这是因为您在循环中每次都重新分配。 $test
我建议这个解决方案:
$test = 'https://testurl.com/prod/hostdetails.php?{0}' -f ((C:\temp\s.txt) -join '&')
您编辑了您的问题。这可能是您正在寻找的。请注意+=
运算符连接结果:
$result = @()
Get-Content C:\temp\s.txt | ForEach-Object {
$result += invoke-webrequest -uri "https://testurl.com/prod/hostdetails.php?qs=$($_)"
}
推荐阅读
- reporting-services - Tablix 将其他元素向下推
- swift - 如何检查 rxswift 中的 Observable 计数?
- linux - Golang csv在linux上写空
- python - 使用字符串列表写入带有逗号分隔符的 csv 文件的标题
- datatable - 数据表完全渲染后调用rowGroup
- spring - Oauth2 从令牌中获取用户名
- mysql - SQL 查询搜索哪里不一样
- python - 如何使用 Spyder 使用 pep8 模块
- php - 当 id 从一页传递到另一页时,如何显示 MySQL 表的内容?
- python - 获取列表中相似连续元素的索引(Python3)