1

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
1

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

9 Comments

I'll test these now.. The first one is literally what i had before but i think i had it formatted wrong :(.. Would you mind removing the users name on the path? Forgot i had left that in there.
Done, hadn't seen it either :p
Let me know if you have issue. Obviously I couldn't test the whole thing, so I only tested bits.
Ahhh the first one is a beauty and it works wonderfully!! Second one took a while to run and gave me no output.. But i love the first one :)
Then you can just store the result of Invoke-Webrequest in a variable, without filtering the fields, and add properties based on that. I'll update the example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.