Unable to pass powershell variables in graph api body

K Roja 56 Reputation points
2022-10-12T11:41:33.82+00:00

Hello @Vasil Michev ,

We are trying to pass powerShell variables as part of body for conditional access policy graph api.
But the body which we are passing is not correct. PFB the code

Is there any way to send PowerShell variables as part of body?

CODE

$body = '{
"displayName": $conditionalAccessPolicy.policyDisplayName,
"state": $conditionalAccessPolicy.state,
"conditions": {
"clientAppTypes": $conditionalAccessPolicy.clientAppTypes
"applications": {
"includeApplications": $conditionalAccessPolicy.includeApplications,
"includeUserActions": $conditionalAccessPolicy.includeUserActions
},
"users": {
"includeUsers": $conditionalAccessPolicy.includeUsers,
"excludeUsers": $conditionalAccessPolicy.excludeUsers
},
"locations": {
"includeLocations": $conditionalAccessPolicy.includeLocations,
"excludeLocations": $conditionalAccessPolicy.excludeLocations
}
},
"grantControls": {
"operator": $conditionalAccessPolicy.grantControlOperator,
"builtInControls": $conditionalAccessPolicy.grantBuiltInControls
},
"sessionControls": {
"signInFrequency": {
"isEnabled": $conditionalAccessPolicy.signInFrequency.isEnabled,
"type": $conditionalAccessPolicy.signInFrequency.type,
"value": $conditionalAccessPolicy.signInFrequency.value
},
"persistentBrowser": {
"mode": $conditionalAccessPolicy.persistentBrowser.mode,
"isEnabled" : $conditionalAccessPolicy.persistentBrowser.isEnabled
}
}
}'

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2022-10-12T12:11:43.387+00:00

    You're declaring the $body variable as a string, use a hash-table instead and then covert to JSON:

    $body = @{  
        "displayName" = $conditionalAccessPolicy.policyDisplayName  
        "state" = $conditionalAccessPolicy.state  
        "conditions" = @{  
            "clientAppTypes" = $conditionalAccessPolicy.clientAppTypes  
        }  
    } | ConvertTo-Json  
    

    Refer to the cmdlet help for more examples: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.2


  2. Sandeep G-MSFT 20,906 Reputation points Microsoft Employee Moderator
    2022-10-20T06:40:15.127+00:00

    @K Roja

    You can make use of below article with steps mentioned to handle create CA policy using Graph API

    https://github.com/Azure-Samples/azure-ad-conditional-access-apis/tree/main/01-configure/graphapi#step-2-create-a-conditional-access-policy

    Let me know if you have any further questions

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.