-
Notifications
You must be signed in to change notification settings - Fork 2
/
StaleIdentityRBAC-RG-Cleanup.ps1
43 lines (34 loc) · 1.54 KB
/
StaleIdentityRBAC-RG-Cleanup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Connect-AzAccount -Environment AzureUSGovernment
#Remove-AzResourceGroup -Name rg-eastus2-p-AVDAlerts -Force
$LogFile = ".\AzureRemoveStaleIdenties_RG.log"
$ReportOnly = $false
function write-log
{
Param ([string]$logstring)
Add-content $Logfile -Value $logstring
}
If($ReportOnly){write-log "REPORTING ONLY - No Actions taken`n"}
If(!$ReportOnly){write-log "LIST OF STALE ROLES REMOVED`n"}
$Runtime = Get-Date
write-log "----------------------- Runtime: $Runtime -----------------------`n"
# Clean up current subscription
# $RBACSub = Get-AzRoleAssignment -ResourceName
$resourceGroups = Get-AzResourceGroup
Foreach($resourceGroup in $resourceGroups){
Write-Host "Working on Resource Group: " $resourceGroup.ResourceGroupName -ForegroundColor Cyan
$RGId = $resourceGroup.ResourceId
$RBAC = Get-AzRoleAssignment -ResourceGroupName $resourceGroup.ResourceGroupName | Where-Object {$_.DisplayName -eq $null -and $_.Scope -eq $RGId}
If($RBAC.count -gt 0){
Foreach($role in $RBAC){
Write-Host "Found 'Identity Not Found' for" $role.RoleDefinitionName -ForegroundColor Yellow
If($ReportOnly){
Write-Host "----> Logging Only!" -ForegroundColor Yellow}
If(!$ReportOnly){
Write-Host "----> Logging and Removing!" -ForegroundColor Red
Remove-AzRoleAssignment -InputObject $role
}
write-log ($role | ConvertTo-Json)
write-log "----------------------------------------------------"
} #end foreach
} # end if
}