我有一段时间试图导入CSV并运行Invoke-WebRequest来获取要添加到新列的数据..
$username = "Username"
$password = cat C:\Password.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$csv = Import-Csv -Path C:\users\Desktop\ImportTest.csv
foreach($c in $csv){
$link = "https://MyURlToCallforData"
$uri= $link + $c.stuff
Invoke-WebRequest -Uri $uri -Credential $cred -UseBasicParsing| ConvertFrom-Json | Select-Object -Property lastIdReportTime
}
Export-Csv -Path C:\Users\Desktop\TestOutput.csv -NoTypeInformation
不,我可以导入它,我打电话,我在ISE上看到结果,但我无法导出到CSV或附加当前文件。
我尝试了各种各样的东西,尝试添加一个新的内容,尝试添加一个psObject,无论我做什么,我都失败了。
希望有人能帮助我。
CSV基本上是这样的。
Date,Description
Text1,text2
text3,text4
我希望像这样导出
Date,Description,NewInfo
Text1,text2,new5
text3,text4,new6
答案 0 :(得分:1)
该问题有几种解决方案。 这是一个:
$username = "Username"
$password = cat C:\Password.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$csv = Import-Csv -Path C:\users\username\Desktop\ImportTest.csv
$link = "https://MyURlToCallforData"
foreach($c in $csv){
$uri= $link + $c.stuff
$result = Invoke-WebRequest -Uri $uri -Credential $cred -UseBasicParsing| ConvertFrom-Json
$c | Add-Member -name "NewInfo" -MemberType NoteProperty -value $($result | Select-Object -ExpandProperty lastIdReportTime)
$c | Add-Member -name "SecondField" -MemberType NoteProperty -value $($result | Select-Object -ExpandProperty SomeOtherField)
}
$csv | Export-Csv -Path C:\Users\username\Desktop\TestOutput.csv -NoTypeInformation
编辑:我刚刚意识到您的代码存在问题:Export-Csv没有导出任何内容
第二次编辑:“ - ExpandProperty”可能是强制性的,以避免某些类型的混乱。
这是第二种可能的解决方案。选择你最喜欢的;)
$username = "Username"
$password = cat C:\Password.txt | ConvertTo-SecureString
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$csv = Import-Csv -Path C:\users\username\Desktop\ImportTest.csv
$link = "https://MyURlToCallforData"
$csv = $csv | Select-Object -Property *,@{name="NewInfo";expression = {
$uri= $link + $c.stuff
Invoke-WebRequest -Uri $uri -Credential $cred -UseBasicParsing| ConvertFrom-Json | Select-Object -expandProperty lastIdReportTime
}}
$csv | Export-Csv -Path C:\Users\username\Desktop\TestOutput.csv -NoTypeInformation