Skip to content

Instantly share code, notes, and snippets.

@hansgafriedzal
Last active October 1, 2020 04:27
Show Gist options
  • Save hansgafriedzal/4fc0d3b063f1fe8ffb54f916471fb030 to your computer and use it in GitHub Desktop.
Save hansgafriedzal/4fc0d3b063f1fe8ffb54f916471fb030 to your computer and use it in GitHub Desktop.
<#
CSV FORMAT
+------------+--------------+--------------+
| Group Name | Group Owner1 | Group Owner2 |
+------------+--------------+--------------+
| group1 | user@domain | user@domain |
| group2 | user@domain | user@domain |
| group3 | user@domain | user@domain |
+------------+--------------+--------------+
#>
cls
$appId = ''
$appRoleName = ''
$path = ''
$filename = ''
Write-Host "searching app id $appId... " -NoNewline
$sp = Get-AzureADServicePrincipal -ObjectId $appId
if ($sp)
{
Write-Host "found"
Write-Host "searching app role $($appRoleName)... " -NoNewline
$appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $appRoleName }
$appRoleId = $appRole.Id
#$appRoleId = [System.GUID]::Empty # Default Access
if ($appRoleId)
{
Write-Host "object id" $appRoleId
cd $path
import-csv $filename | %{
Write-Host "searching $($_."Group Name")... " -NoNewline
$group = Get-AzureADGroup -Filter "displayName eq '$($_."Group Name")'"
if ($group)
{
Write-Host "object id" $group.ObjectId
Write-Host "assigning role... " -NoNewline
try
{
$assignment = New-AzureADGroupAppRoleAssignment -ObjectId $group.ObjectId -PrincipalId $group.ObjectId -ResourceId $sp.ObjectId -Id $appRoleId
Write-Host "successful " -NoNewline -ForegroundColor Green
Write-Host "object id" $assignment.ObjectId
}
catch
{
Write-Host "unsuccessful" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
}
}
else
{
Write-Host "not found" -ForegroundColor Red
}
}
}
else
{
write-host "not found" -ForegroundColor Red
}
Get-AzureADServiceAppRoleAssignment -ObjectId $sp.ObjectId -All 1 `
| where PrincipalType -eq Group `
| sort PrincipalDisplayName `
| ft ObjectId, PrincipalDisplayName, PrincipalId
}
else
{
write-host "not found" -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment