
Announcements
Param(
[Parameter(Mandatory = $true)]
[string] $RequesterBusinessUnitCode,
[Parameter(Mandatory = $true)]
[string] $TeamID
)
#convert BusinessUnitcode to a 4 digit string
$BusinessUnitCode = "{0:0000}" -f [int]$BusinessUnitCode
$VaultName = Get-AutomationVariable -name VaultName
$SecretName = Get-AutomationVariable -name sGroupRWAll
$ClientID_MSG = Get-AutomationVariable -name vGroupRWAll
$loginURL_MSG = Get-AutomationVariable -name loginURL_MSG
$resource_MSG = Get-AutomationVariable -name resource_MSG
try
{
$AzureContext = (Connect-AzAccount -Identity).context
}
catch
{
exit
}
# Get secrets from Key Vaults
$ClientSecret_MSG = Get-AzKeyVaultSecret -VaultName $VaultName -Name $SecretName -AsPlainText
# Get an access token
#Credential and authorization strings will be used based on the values above
$Cred_MSG = @{grant_type="client_credentials";resource=$resource_MSG;client_id=$ClientID_MSG;client_secret=$ClientSecret_msg}
$oauth_MSG = Invoke-RestMethod -Method Post -Uri $loginURL_MSG/oauth2/token -Body $Cred_MSG
$headerParams_MSG = @{
'Authorization'="$($oauth_MSG.token_type) $($oauth_MSG.access_token)"
'ConsistencyLevel' = 'eventual'
}
#first, locate the deleted team and get the deleted team's BusinessUnitcode
$LocateTeamURI = "https://graph.microsoft.com/v1.0/directory/deletedItems/microsoft.graph.group/$TeamID"
try {
$DeletedGroup = Invoke-RestMethod -uri $LocateTeamURI -Method get -headers $headerParams_MSG
#compare the deleted team's BusinessUnitcode to the requesters BusinessUnitcode $RequesterBusinessUnitCode
$RequesterBusinessUnitCode = "{0:0000}" -f [int]$RequesterBusinessUnitCode
$TeamBusinessUnitCode = "{0:0000}" -f [int]$($deletedgroup.extension_*removedforsecurity*_SyncSource_BusinessUnitId)
$TeamName = $DeletedGroup.displayname
if ($TeamBusinessUnitCode -ne $RequesterBusinessUnitCode) {
#if they are not the same, send a failure block back to power automate
$output = @{"Completed"="Failure"; "TeamID"="$TeamID"; "TeamName"="$TeamName"; "Comments"="User BusinessUnitCode ($RequesterBusinessUnitCode) does not match the Team BusinessUnitCode ($TeamBusinessUnitCode)"}
Write-Output ($output | ConvertTo-Json)
} else {
#if they are the same, restore the team
$RestoreTeamURI = "https://graph.microsoft.com/v1.0/directory/deletedItems/$TeamID/restore"
try {
$restored = Invoke-RestMethod -uri $RestoreTeamURI -Method post -headers $headerParams_MSG -contenttype application/json
$success = $true
} catch {
#unable to restore the team. send a failure block back to power automate
$output = @{"Completed"="Failure"; "TeamID"="$TeamID"; "TeamName"="$TeamName"; "Comments"="Error occured when trying to restore the Team."}
Write-Output ($output | ConvertTo-Json)
}
if ($success) {
#send a success block to power automate
$output = @{"Completed"="Success"; "TeamID"="$TeamID"; "TeamName"="$TeamName"; "Comments"="Restored Successfully"}
Write-Output ($output | ConvertTo-Json)
}
}
} catch {
#unable to restore the team as it is not in deleted state. send a failure block back to power automate
$output = @{"Completed"="Failure"; "TeamID"="$TeamID"; "TeamName"="$TeamName"; "Comments"="The team was not located when trying to restore. Please confirm it is still deleted, noting it may take up to 5 minutes to appear."}
Write-Output ($output | ConvertTo-Json)
}
if ($TeamBusinessUnitCode -ne $RequesterBusinessUnitCode) {