'Issue with variable in REST API powershell
Background : The REST API request works correctly when I remove the variable $Description and put a string in line 11 . If I leave the variable then it doesn't get the value of it , it just uses the name $Description. The variable is working fine since I've tested it by displaying the values through each iteration in the loop.
Location of issue : Line 11
Error Logs : No errors from powershell
$import= Import-Csv -Path C:\Users\ERESULTS.csv -Encoding UTF8 -Delimiter "," -Header "ID", "Description"
$limit = $import.id.count
$token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyZXBKPqye_X-lBgO3g_XMdlXBdgxsWlXMN2y6sVmF5nlc"
foreach ($line in $import) {
$ID= $line.ID
$Description= $line.Description
$params = @{
Uri = "https://test.com/rest-api/dataModel/ci/$ID"
Headers = @{ 'Authorization' = "Bearer $token" }
Method = 'PUT'
Body = '{ "dbId" : "$ID", "type" : "business_application", "properties" : {"description" : "$Description" }}'
ContentType = 'application/json'
}
Invoke-RestMethod @params
Start-Sleep -Second 2
}
Solution 1:[1]
Use a double-quoted here-string to have variables expand, but without having to escape "
inside the string:
$params = @{
# ...
Body = @"
{ "dbId" : "$ID", "type" : "business_application", "properties" : {"description" : "$Description" }}
"@
# ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mathias R. Jessen |