Variable substitution in rest api in powershell

Hi All,

 

I am a beginner in powershell.I was trying to call Tableau REST APIs using powershell.To make the script generic i would need to pass the filter parameters by through a variable.In the example below,

I have a csv file filterfile,which has two columns -Fit Name,Set Name.

Fit Name Set Name

A AA

B BB

 

I would need the filter parameter in $param to get expanded down the line in the context of invoke restmethod.Since the csv has only two rows(it can have N number of rows),the expanded values should be

 

Invoke-RestMethod -Uri “$server/api/3.5/sites/$siteID/views/$responseD/PDF?vf_Filter1=A&vf_Filter2=AA

 

Invoke-RestMethod -Uri “$server/api/3.5/sites/$siteID/views/$responseD/PDF?vf_Filter1=B&vf_Filter2=BB

 

sample code:

$param=“vf_Filter1=$($.{Fit Name})&vf_Filter2=$($.{Set Name})”

Import-Csv $filterfile | ForEach-object{

Invoke-RestMethod -Uri “$server/api/3.5/sites/$siteID/views/$responseD/PDF?$Param”

}

 

Tried -f operator to assign the values.

$param = 'vf_Filter1={0}&vf_Filter2={1}'
$Filter = $param -f $_.'Fit Name', $_.'Set Name'
Import-Csv $filterfile | ForEach-object{
$Uri = "$server/api/3.5/sites/$siteID/views/$responseD/PDF?$Filter"
    Invoke-RestMethod -Uri $Uri
}
in the above case $Filter is blank in the Uri.
I had tried using invoke expression write-host still no luck.

I would need the assignment of Fit Name and Set Name to be done before the import csv and its expansion after the import csv.
Kindly help

Ensure that the parameter references are inside the loop. This is outside of the foreach, so $_ is not referring to anything:

$Filter = $param -f $_.'Fit Name', $_.'Set Name'

Here is a basic example:

$Filter = $param -f $_.'Fit Name', $_.'Set Name'
$csv = @"
"Fit Name","Set Name"
"A","AA"
"B","BB"
"@ | ConvertFrom-Csv


foreach ($row in $csv) {
    $uri = “$server/api/3.5/sites/$siteID/views/$responseD/PDF?vf_Filter1={0}&vf_Filter2={1}” -f $row.'Fit Name',$row.'Set Name'

    $uri
    #Invoke-RestMethod -Uri $uri
}