Site icon Microsoft 365 for IT Pros

Using PowerShell Here-Strings with Graph APIs

Advertisements

Use Here-Strings to Create Request Bodies for Graph API Requests

A reader was curious about the format of the request bodies for the Graph API requests in the recent article about Unified Tenant Configuration Management (UTCM). They wondered why some of the PowerShell variables used in examples start with an @ sign followed by a double quotation mark. For example:

$RequestBody = @"
{
    "displayName": "CAS Mailbox Plan snapshot",
    "description": "This is a snapshot created via the Graph API for CAS Mailbox Plans",
    "resources": [
        "microsoft.exchange.casMailboxPlan"
    ]
}
"@

$NewSnapShot = Invoke-MgGraphRequest -Uri $Uri -Method Post -Body $RequestBody

The answer is that these variables are here-strings, a PowerShell feature inherited from UNIX (the term is still used in Linux scripting). As you can see from the example above, a here-string allows a string variable to include quote characters matching the quotation marks used to create the string.

In this case, the variable holds a request body, the input for a Graph API request to create an object. The request body is a JSON structure where keys and values must be enclosed in double quotation marks. The here-string can be delimited with single or double quotation marks. For instance, a here-string can start with:

@” or @’

And end with:

“@ or ‘@

Using Here-Strings with Documentation Examples

Microsoft documentation for Graph API requests includes examples for HTTP commands, and those that add, update, or remove objects often include request bodies formatted like the one shown above.

When testing Graph APIs, it is convenient to copy the example of a request body, make any necessary changes to reflect what you’re trying to do, and put the resulting JSON structure in a string variable. A here-string makes it easy to preserve the double quotation marks in the JSON content for the request body in a form that’s usable with the Invoke-MgGraphRequest cmdlet. Just make sure that you put the contents of the request body on separate lines to the delimiters. For example:

$RequestBody = @"

{

  "key": "value",

  "key2": "value2",

  "key3": "value3"

}

"@

The Hash Table Alternative

Using a here-string is a great way to provide input for one-off Graph API requests, especially when an example of the JSON structure is available. However, when writing scripts where the input values are likely to change, creating the request body with a hash table is often a better approach.

Take the example of creating a new Microsoft 365 group with the createGroup API. The request body in the documentation example is:

{
  "description": "Self help community for library",
  "displayName": "Library Assist",
  "groupTypes": [
    "Unified"
  ],
  "mailEnabled": true,
  "mailNickname": "library",
  "securityEnabled": false
}

The equivalent hash table is shown below (amended with some more interesting values). The value for the groupTypes key is populated as an array.

[array]$GroupTypes = "unified" 

$GroupRequestBody = @{}
$GroupRequestBody.Add("displayName", "Microsoft Graph PowerShell SDK Gurus")
$GroupRequestBody.Add("description", "A Microsoft 365 group for discussions about using the Graph PowerShell SDK")
$GroupRequestBody.Add("mailEnabled", "true")
$GroupRequestBody.Add("mailNickName", "Graph.SDK.Discussions")
$GroupRequestBody.Add("securityEnabled","false")
$GroupRequestBody.Add("groupTypes", $GroupTypes)

After populating the hash table, the request body looks like this:

$GroupRequestBody

Name                           Value
----                           -----
displayName                    Microsoft Graph PowerShell SDK Gurus
groupTypes                     {unified}
securityEnabled                false
mailEnabled                    true
description                    A Microsoft 365 group for discussions about using the Graph PowerShell SDK
mailNickName                   Graph.SDK.Discussions

The request body is then passed in the Body parameter to create the group like this:

$Group = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/groups" -Method Post -Body $GroupRequestBody

Using hash tables to create a request body is a form of PowerShell splatting. I consider it easier to create a hash table when I need to create a request body from values computed in a script than it is with a here-string, and that’s why I use hash tables instead of here-strings any time that values might change during processing.

A Matter of Personal Choice

The nice thing about PowerShell is the amount of choice it affords to developers. Some will prefer here-strings and use them all the time. Others will share my view and prefer hash tables. It’s up to you.


Need help to write and manage PowerShell scripts for Microsoft 365, including Azure Automation runbooks? Get a copy of the Automating Microsoft 365 with PowerShell eBook, available standalone or as part of the Office 365 for IT Pros eBook bundle.

Exit mobile version