JonnyBoy
JonnyBoy

Reputation: 417

Import-CSV add data Export

I'm having a heck of a time trying to import a CSV and run a Invoke-WebRequest to get data to add to a new Column..

$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

No i can import it fine, I make the call and i see the results on the ISE but I can't export to a CSV or append the current File.

I've tried all sorts of things, trying to add a new content trying to add a psObject and whatever I do i fail at..

Hoping someone can give me a hand here.

The CSV is basically like this.

Date,Description
Text1,text2 
text3,text4

and i want to export it like this

Date,Description,NewInfo
Text1,text2,new5
text3,text4,new6

Upvotes: 1

Views: 715

Answers (1)

Poorkenny
Poorkenny

Reputation: 1246

There are several solutions to that question. Here's one:

$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

Edit: I just realized there was an issue with your code: Export-Csv isn't exporting anything
Second edit: "-ExpandProperty" is probably mandatory to avoid some type mix-up mess.

And here's a second possible solution. Pick your favorite ;)

$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

Upvotes: 1

Related Questions