Reputation: 17
I've got a script for interacting with Microsoft GraphAPI. In the script I define a variable for the URL to use, e.g.
$UriGet = "https://graph.microsoft.com/v1.0/identity/conditionalAccess/policies?`$filter=startswith(displayName,`'$Identifier`')"
The function then reads from a CSV, and processes each line in the CSV against the $UriGet URI. The function is called like:
Invoke-GraphApi -UriGet $UriGet
However, inside the function, the actual "$Identifier"
variable is substituted with nothing, e.g. its like the $UriGet has been passed with the pre-function variable evaluated, at which time its $null.
e.g., within the "Invoke-GraphApi function, write-host $UriGet provides
URIGet value: "https://graph.microsoft.com/v1.0/identity/conditionalAccess/policies?$filter=startswith(displayName,'')"
So the question is, how do I pass the full string into the function without variables been evaluated?
Edit - here is the Param block for the function
Param (
[parameter(Mandatory=$true)]
[Hashtable]$MsGraphAuthenticationHeader,
[parameter(Mandatory=$true)]
[String]$UriGet,
[parameter(Mandatory=$true)]
[String]$UriPatch,
[parameter(Mandatory=$true)]
[String]$UriPost,
[parameter(Mandatory=$true)]
[String]$Identifier,
[parameter(Mandatory=$true)]
[String]$RequestBody
)
Upvotes: 0
Views: 391
Reputation: 17
Thanks everyone for the input :) I ended up using the simplified quoting rules as per @igor. It still didn't work due to the '
characters needed in the query string. So I then used the -replace
capability to substitute in the actual search string as per the code block below.
$UriGet = 'https://graph.microsoft.com/v1.0/identity/conditionalAccess/policies?$filter=startswith(displayName,#___PolicyName___#)'
$UriGet = $UriGet -replace '#___PolicyName___#', $Identifier
Upvotes: 0
Reputation: 1445
Single quotes at the beginning and at the end of string will prevent variable expansions. Quotes inside of the string do not impact if variable is substituted with its value or not.
PS > $a="string"
PS > write-host "some $a"
some string
PS > write-host 'some $a'
some $a
If you need quotes of the same type inside of resulting string, you would need to double those quotes. It does not impact whether string is substituted with its value or not.
PS > write-host "some ""$a"" in quotes"
some "string" in quotes
PS > write-host 'some ''$a'' in quotes'
some '$a'
If you need quotes of the different type inside of resulting string, just write them as it is. Again, it will not impact variable substitution with its value.
PS > write-host "some '$a' in quotes"
some 'string' in quotes
PS > write-host 'some "$a"'
some "$a"
Upvotes: 2
Reputation: 746
Before each variable that you do not want evaluating insert a backtick ` . Please see the below Microsoft documentation "about quoting rules" specifically this section:
"To prevent the substitution of a variable value in a double-quoted string, use the backtick character (`)(ASCII 96), which is the PowerShell escape character"
Upvotes: 0