2

I have some code to retrieve an Azure Active Directory group via the Graph API using the Microsoft.Graph module:

$currentGroup = Get-MgGroup -Filter ('DisplayName eq ' + "'" + $strThisGroupDisplayName + "'")

I originally thought that this and some related code were failing when the group specified by $strThisGroupDisplayName contained a comma. It turns out that this was a miscommunication between me and another team member - a CSV download of groups from the Azure AD portal did not include the comma in the group's displayName for whatever reason. However, the group was created correctly, including the comma in the name.

But that got me thinking: are there any characters that need escaping in filters called using the Microsoft.Graph PowerShell module?

I did some searching and haven't found anything concrete except for this post on escaping a single quote/apostrophe: Single quote escaping in Microsoft Graph, this post on an ampersand: Issue in Matching Department Name while using Microsoft Graph API V1.0, and this post on a hashtag/pound sign: Microsoft Graph filter groups with # in name

I was originally thinking that I might need to replace any single quotes/apostrophes in $strThisGroupDisplayName with a double single quote, e.g.:

$strThisGroupDisplayName = $strThisGroupDisplayName.Replace("'", "''")

Next, perhaps I needed to URL-encode the whole thing? For example:

$strThisGroupDisplayName = System.Web.HttpUtility]::UrlEncode($strThisGroupDisplayName)

However, from the comments below, it seems that maybe none of this is necessary, and the encoding happens automatically in the Microsoft.Graph module?

Edited to add: I did have to escape DisplayNames that contained an apostrophe/single quote - otherwise I received an error. To do this, I used the following code:

$currentGroup = Get-MgGroup -Filter ('DisplayName eq ' + "'" + ($strThisGroupDisplayName.Replace("'", "''")) + "'")

I can also confirm that pound signs/hashtags (#) and commas (,) in the display name did not cause an issue; the Microsoft.Graph module must be encoding these.

Frank Lesniak
  • 560
  • 1
  • 5
  • 18
  • i'm just guessing here as i'm on the go but try the following `Get-MgGroup -Filter "DisplayName eq '$strThisGroupDisplayName'"`. the PowerShell module should take care of escaping. – Guenther Schmitz Oct 11 '22 at 04:48
  • Hey Guenther, thanks for the reply. What you suggested is effectively what I'm already doing above - I try to use single quoted strings wherever possible, so I used string concatenation to fold in a single quote character on both sides of $strThisGroupDisplayName – Frank Lesniak Oct 11 '22 at 04:57
  • 1
    Having just created a group with a comma in the middle in AAD (question 1), I'm unable to replicate this. `$mygroup = 'group,forthings'; Get-MgGroup -Filter "DisplayName eq '$mygroup'"` works for me. Using v1 of the API currently. In your possible simple solution you demonstrate replacing apostrophes, not commas. Is that what you meant? – Robin Oct 11 '22 at 06:20
  • 1
    I would expect that this works: $strThisGroupDisplayName = $strThisGroupDisplayName.Replace("'", "''") replacing one single quote by two single quotes – user2250152 Oct 11 '22 at 06:30
  • Agreed, the workaround for apostrophe issues does appear to be doubling up. [See here for an answer](https://stackoverflow.com/questions/71501380/cannot-parse-url-string-for-microsoft-graph-because-using-the-invoke-msgraphrequ). – Robin Oct 11 '22 at 06:32

0 Answers0