locked
Passing rows from CSV into foreach loop RRS feed

  • Question

  • Hello-

    I have been struggling to figure out a solution to this question;  but I'm sure for you guys, this will be easy.

    Let's say I have a CSV file that looks like this:

              Names     Expressions     Accounts

    Row 1: Name1     Exp1            Acct1 

    Row 2: Name2     Exp2            Acct2

    Row 3: Name3     Exp3            Acct3

    What I would like to do is loop the data into an Invoke-WebRequest POST command, like this:

    foreach ($row in $CSV-file) {

        $Body= @"
        {
    "name" : "$Row.Names.Value",

    "Expression" : "$Row.Expressions.Value",

    "account" : "$Row.Accounts.Value",
    "static-field" : "staticvalue",
        }
    "@

    Invoke-WebRequest -Uri $Url-ContentType "application/json" -Method POST -Headers $Headers -Body $Body

    }

    This would mean that the above $Body gets looped 3 times, one for each row:

        $Body= @"
        {
    "name" : "Names1",

    "Expression" : "Exp1",

    "account" : "Acct1",
    "static-field" : "staticvalue",
        }
    "@

    Invoke-WebRequest -Uri $Url-ContentType "application/json" -Method POST -Headers $Headers -Body $Body

    }

        $Body= @"
        {
    "name" : "Names2",

    "Expression" : "Exp2",

    "account" : "Acct2",
    "static-field" : "staticvalue",
        }
    "@

    Invoke-WebRequest -Uri $Url-ContentType "application/json" -Method POST -Headers $Headers -Body $Body

    }

    Etc.

    What is the best way to do something like this?

    Wednesday, October 2, 2019 9:30 PM

Answers

  • Here is how to create a Json body:

    Import-Csv $filename |
    	ForEach-Object{
    		$json = @{
    			name = $_.Names
    			Expression = $_.Expressions
    			account = $_.Accounts
    			'static-field' = 'staticvalue'
    		} | ConvertTo-Json
    		Invoke-WebRequest -Uri $Url-ContentType application/json -Method POST -Headers $Headers -Body $json
    	}


    \_(ツ)_/


    • Edited by jrv Wednesday, October 2, 2019 9:46 PM
    • Marked as answer by Ryan_O_84 Wednesday, October 2, 2019 9:50 PM
    Wednesday, October 2, 2019 9:45 PM

All replies

  • Please post code properly formatted using the code posting tool provided:

    How to post code in Technet Forums

    Edit your original post and fix according to the linked instructions.


    \_(ツ)_/


    • Edited by jrv Wednesday, October 2, 2019 9:39 PM
    Wednesday, October 2, 2019 9:39 PM
  • Here is how to create a Json body:

    Import-Csv $filename |
    	ForEach-Object{
    		$json = @{
    			name = $_.Names
    			Expression = $_.Expressions
    			account = $_.Accounts
    			'static-field' = 'staticvalue'
    		} | ConvertTo-Json
    		Invoke-WebRequest -Uri $Url-ContentType application/json -Method POST -Headers $Headers -Body $json
    	}


    \_(ツ)_/


    • Edited by jrv Wednesday, October 2, 2019 9:46 PM
    • Marked as answer by Ryan_O_84 Wednesday, October 2, 2019 9:50 PM
    Wednesday, October 2, 2019 9:45 PM
  • I tried to edit it but when I click submit, it is telling me that my post was reported as spam :(

    Thank you.

    Wednesday, October 2, 2019 9:49 PM
  • This is just what I needed.  Thank you!
    Wednesday, October 2, 2019 9:50 PM