Variable substitution in rest api in powershell

Welcome Forums General PowerShell Q&A Variable substitution in rest api in powershell

Viewing 1 reply thread
  • Author
    Posts
    • #249659
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

      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.

      <span class="token variable">$Filter</span> = <span class="token variable">$param</span> <span class="token operator">-</span>f <span class="token variable">$_</span><span class="token punctuation">.</span><span class="token string">'Fit Name'</span><span class="token punctuation">,</span> <span class="token variable">$_</span><span class="token punctuation">.</span><span class="token string">'Set Name'</span>[/crayon] [/crayon] [/crayon]
    • #249677
      Participant
      Topics: 15
      Replies: 1775
      Points: 3,214
      Helping Hand
      Rank: Community Hero

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

      Here is a basic example:

Viewing 1 reply thread
  • You must be logged in to reply to this topic.