0

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
    )
2
  • 1
    Please show us the Invoke-GraphApi function definition (or, at the very least, its param() block) Commented Nov 5, 2020 at 19:42
  • @MathiasR.Jessen I've added the param block to the original post. Commented Nov 5, 2020 at 22:45

3 Answers 3

2

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"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, yes I understand that, but the string I'm passing in already has escaped ' characters in it, so I think there is more to this :)
Hi, you are declaring string that starts and ends with double quotes, so it is doing variable substitution, regardless of inner single quotes. If you need to have also single quote inside of the already single quoted string, you would need to use "double single quote". I have updated my answer with these additional explanation and example.
0

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"

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules

Comments

0

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

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.