Last active
December 5, 2025 12:25
-
-
Save AlexanderHolmeset/e8e619583f30b01226b37d3dcffade9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $PolicyNameID = "Enter your Policy Name ID here" | |
| $ApplicationId = "xxxxx" | |
| $ClientSecret = "xxxxx" | |
| $TenantId = "xxxxx" | |
| # Connect to PowerApps and Microsoft Graph | |
| # Ensure you have the required modules installed: Microsoft.PowerApps.Administration.PowerShell and Microsoft.Graph | |
| Add-PowerAppsAccount -ApplicationId $ApplicationId -ClientSecret $ClientSecret -TenantID $TenantId | |
| Connect-MgGraph -identity | |
| $PolicyOld = Get-DlpPolicy -PolicyName $PolicyNameID | |
| $PolicyBusiness = $PolicyOld.connectorGroups | Where-Object{$_.classification -like "Confidential"} | |
| $PolicyNonBusiness = $PolicyOld.connectorGroups | Where-Object{$_.classification -like "General"} | |
| $PolicyBlocked = $PolicyOld.connectorGroups | Where-Object{$_.classification -like "Blocked"} | |
| $PolicyBusinessJson = @() | |
| $PolicyNonBusinessJson = @() | |
| $PolicyBlockedJson = @() | |
| foreach($policy in $PolicyBusiness.connectors){ | |
| $TempPolicyBusiness = @() | |
| $TempPolicyBusiness = @" | |
| { | |
| "id": "$($Policy.id)", | |
| "name": "$($Policy.name)", | |
| "type": "$($Policy.type)" | |
| }, | |
| "@ | |
| $PolicyBusinessJson += $TempPolicyBusiness | |
| } | |
| foreach($Policy in $PolicyBlocked.connectors){ | |
| $TempPolicyBlocked = @() | |
| $TempPolicyBlocked = @" | |
| { | |
| "id": "$($Policy.id)", | |
| "name": "$($Policy.name)", | |
| "type": "$($Policy.type)" | |
| }, | |
| "@ | |
| $PolicyBlockedJson += $TempPolicyBlocked | |
| } | |
| foreach($Policy in $PolicyNonBusiness.connectors){ | |
| $TempPolicyNonBusiness = @() | |
| $TempPolicyNonBusiness = @" | |
| { | |
| "id": "$($Policy.id)", | |
| "name": "$($Policy.name)", | |
| "type": "$($Policy.type)" | |
| }, | |
| "@ | |
| $PolicyBlockedJson += $TempPolicyNonBusiness | |
| } | |
| $PolicyBusinessJson = ([string]$PolicyBusinessJson).TrimEnd(",") | |
| $PolicyBlockedJson = ([string]$PolicyBlockedJson).TrimEnd(",") | |
| $PolicyUpdated = @" | |
| { | |
| "name": "$($PolicyOld.name)", | |
| "displayName": "$($PolicyOld.displayName)", | |
| "defaultConnectorsClassification": "General", | |
| "connectorGroups": [ | |
| { | |
| "classification": "Confidential", | |
| "connectors": [ | |
| $PolicyBusinessJson | |
| ] | |
| }, | |
| { | |
| "classification": "General", | |
| "connectors": [ | |
| ] | |
| }, | |
| { | |
| "classification": "Blocked", | |
| "connectors": [ | |
| $PolicyBlockedJson | |
| ] | |
| } | |
| ], | |
| "environmentType": "$($PolicyOld.environmentType)", | |
| "environments": [], | |
| "etag": "$($PolicyOld.etag)", | |
| "isLegacySchemaVersion": false | |
| } | |
| "@ | |
| $PolicyUpdated = $PolicyUpdated | ConvertFrom-Json -Depth 10 | |
| $UpdateDLP = Set-DlpPolicy -PolicyName $PolicyOld.name -UpdatedPolicy $PolicyUpdated | |
| $FailedSuccess = "Added to Blocked - DLP Policy." | |
| $UpdateDLP | |
| If($UpdateDLP.code -eq "400"){ | |
| $FailedSuccess = "Failed to update DLP policy: $($UpdateDLP.Error.Message)" | |
| } | |
| $emailBody = "</head><body><h1>Status: $FailedSuccess</h1>" | |
| $emailBody += "<p>Connectors: $($PolicyNonBusiness.Connectors.Name -join ",")</p>" | |
| $emailBody += "</div>" | |
| $emailBody += "</body>" | |
| $emailBody += "</html>" | |
| $params = @{ | |
| message = @{ | |
| subject = "$FailedSuccess" | |
| body = @{ | |
| contentType = "html" | |
| content = "$emailbody" | |
| } | |
| toRecipients = @( | |
| @{ | |
| emailAddress = @{ | |
| address = "[email protected]" | |
| } | |
| } | |
| ) | |
| } | |
| saveToSentItems = "false" | |
| } | |
| # Send the email | |
| $response = Send-MgUserMail -userid "[email protected]" -bodyparameter $params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment