0

I am using this invoke-restmethod so I can get a token so I can do some sql work. the variables come from Azure Key Vault. I have tried to write the variables as

$($SPNAppid)
$SPNAppid
${$SPNAppid} etc

Here is the code :

$request = Invoke-RestMethod -Method POST -Uri "https://login.microsoftonline.com/${$TenantId}"/oauth2/token" -Body @{ resource="https://database.windows.net/"; grant_type="client_credentials"; client_id=${$SPNAppid}; client_secret=${$SPNValue} } -ContentType "application/x-www-form-urlencoded"

Getting this error below. What is the best way to do this - whatever i do i am getting the errors below.

Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to 
delimit the name.
At C:\agent01_2\_work\_temp\b3f54d23-b7b6-4cc3-96ec-8b4b534be571.ps1:20 char:319
+ ... ervicePrincipalKey } -ContentType "application/x-www-form-urlencoded"
+                                                                         ~
The string is missing the terminator: ".

1 Answer 1

1

The code you posted, has an extra " and given the ambiguity of the long line I would suggest to use splatting like this :

$header = @{
    "Content-type"  = "application/x-www-form-urlencoded"
    "Authorization" = "Bearer $token"
}

$body = @{ 
    resource      = "https://database.windows.net/"
    grant_type    = "client_credentials"
    client_id     = $SPNAppid
    client_secret = $SPNValue
} 

$params = @{
    Method      = 'Post'
    Uri         = "https://login.microsoftonline.com/$($TenantId)/oauth2/token"
    Body        = $body
    ContentType = $header
}

$request = Invoke-RestMethod @params

I do not think the API call would work this way, usually clientId etc. are part of the URL, you can read more about it here - https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow

Sign up to request clarification or add additional context in comments.

Comments

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.