Advertisement
Zalmez_

PIM Role Activation

May 29th, 2021
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#NOTE:
  2. This script requires the AzureADPreview Powershell module. Install-Module AzureADPreview -Scope CurrentUser
  3. This script requires the Az.Accounts Powershell module. Install-Module Az.Accounts -Scope CurrentUser
  4. This script requires the Microsoft.Graph powershell module to be installed
  5. #>
  6. function Get-AzurePIMRoles
  7. {
  8.     Param
  9.     (
  10.         [Parameter(Mandatory=$true)]
  11.         [string]
  12.         $UserUPN
  13.     )
  14. #Function Logic:
  15.     $context = Get-AzContext
  16.     if([string]::IsNullOrEmpty($UserUPN)){
  17.         $UserUPN = Read-Host "UserPrincipalName"
  18.     }
  19.     $AzureUser = Get-AzureADUser -SearchString $UserUPN | Select-Object * # Get the info from logged in user
  20.     if($AzureUser -ne $null)
  21.     {
  22.         $AzureUserId = $AzureUser.ObjectId
  23.         #Get roles
  24.         $AssignedRoles = Get-AzureADMSPrivilegedRoleAssignment -ProviderId "aadroles" -ResourceId $context.Tenant.Id  -Filter "subjectId eq '$AzureUserId'" | Select-Object * #All assigned roles
  25.        
  26.         return $AssignedRoles
  27.     }else
  28.     {
  29.         #Throw Error
  30.         Write-Error "User '$UserUPN' could not be found. Aborting"
  31.     }
  32.  
  33. }
  34.  
  35. function Enable-AllPimRoles
  36. {
  37.     $context = Get-AzContext
  38.     $schedule = New-Object Microsoft.Open.MSGraph.Model.AzureADMSPrivilegedSchedule
  39.     $schedule.Type = "Once"
  40.     $schedule.StartDateTime = (Get-Date).ToUniversalTime().AddSeconds(30).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
  41.     $schedule.endDateTime = (Get-Date).ToUniversalTime().AddHours(10).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
  42.     if($context -eq $null)
  43.     {
  44.         $context = Login-Azure
  45.     }else{
  46.         Connect-AzureAD -AccountId $context.Account.Id
  47.     }
  48.     $Roles = Get-AzurePIMRoles -UserUpn $context.Account.Id
  49.     $ActivationReason = Read-Host "Activation Reason(required)"
  50.     while ([system.string]::IsNullOrEmpty($ActivationReason)) {
  51.         Write-Host "Activation Reason is required!" -ForegroundColor Yellow
  52.         $ActivationReason = "enable role"
  53.     }
  54.     $allRoles = Get-AzureADMSPrivilegedRoleDefinition -ProviderId aadRoles -ResourceId $context.Tenant.Id
  55.     $PercentPerRole = 101 / $Roles.Count
  56.     $complete = 0
  57.     foreach ($role in $Roles) {
  58.         #Write-Host $role
  59.         $CurrentRole = $allRoles | Where-Object {$_.Id -eq $role.RoleDefinitionId}
  60.         Write-Host "Role: $($CurrentRole.DisplayName)"
  61.         if($role.AssignmentState -ne "Active"){
  62.             Write-Progress -Activity "Activating $($CurrentRole.DisplayName)" -Status 'Progress->' -PercentComplete $complete -CurrentOperation "$" -Id "1"
  63.             Open-AzureADMSPrivilegedRoleAssignmentRequest -ProviderId 'aadRoles' -ResourceId $role.ResourceId -RoleDefinitionId $role.RoleDefinitionId -SubjectId $role.SubjectId -Type 'UserAdd' -AssignmentState 'Active' -schedule $schedule -reason $ActivationReason
  64.         }
  65.         $complete += $PercentPerRole
  66.     }
  67.     Start-Sleep -Seconds 1
  68.     Write-Progress -Id "1" -Activity "Completed Role Activation" -Status "Complete"
  69. }
  70.  
  71. function Enable-PimRole
  72. {
  73.     $context = Get-AzContext
  74.     $schedule = New-Object Microsoft.Open.MSGraph.Model.AzureADMSPrivilegedSchedule
  75.     $schedule.Type = "Once"
  76.     $schedule.StartDateTime = (Get-Date).ToUniversalTime().AddSeconds(30).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
  77.     $schedule.endDateTime = (Get-Date).ToUniversalTime().AddHours(10).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
  78.     if($context -eq $null)
  79.     {
  80.         $context = Login-Azure
  81.     }else{
  82.         Connect-AzureAD -AccountId $context.Account.Id
  83.     }
  84.     $Roles = Get-AzurePIMRoles -UserUpn $context.Account.Id
  85.     $allRoles = Get-AzureADMSPrivilegedRoleDefinition -ProviderId aadRoles -ResourceId $context.Tenant.Id
  86.    
  87.     do {
  88.         for ($i = 0; $i -lt $Roles.Count; $i++) {
  89.             $role = $allRoles | Where-Object {$_.Id -eq $Roles[$i].RoleDefinitionId}
  90.             if($Roles[$i].AssignmentState -eq "Active"){
  91.                 Write-Host "$($i+1): $($role.DisplayName) (Already Activated)" -ForegroundColor Green
  92.             }else{
  93.                 Write-Host "$($i+1): $($role.DisplayName)"
  94.             }
  95.         }
  96.         $RoleChoice = Read-Host "Activate Role"
  97.     } until ($RoleChoice -gt 0 -or $RoleChoice -lt $Roles.Count+1)
  98.     $ActivationReason = Read-Host "Activation Reason(required)"
  99.     while ([system.string]::IsNullOrEmpty($ActivationReason)) {
  100.         Write-Host "Activation reason is required!" -ForegroundColor Yellow
  101.         $ActivationReason = Read-Host "Activation Reason(required)"
  102.     }
  103.     $CurrentRole = $allRoles | Where-Object {$_.Id -eq $Roles[$RoleChoice-1].RoleDefinitionId}
  104.     Write-Host "Activating Role: $($CurrentRole.DisplayName)"
  105.     if($role.AssignmentState -ne "Active"){
  106.         Open-AzureADMSPrivilegedRoleAssignmentRequest -ProviderId 'aadRoles' -ResourceId $Roles[$RoleChoice-1].ResourceId -RoleDefinitionId $Roles[$RoleChoice-1].RoleDefinitionId -SubjectId $Roles[$RoleChoice-1].SubjectId -Type 'UserAdd' -AssignmentState 'Active' -schedule $schedule -reason $ActivationReason
  107.     }
  108.  
  109.     Start-Sleep -Seconds 1
  110.     Write-Progress -Id "1" -Activity "Completed Role Activation" -Status "Complete"
  111. }
  112.  
  113. function Login-Azure {
  114.     $TenantId = "Your-Tenant-Id-Here"
  115.     Login-AzAccount -TenantId $TenantId -Environment AzureCloud
  116.     $azContext = Get-AzContext
  117.     Connect-AzureAD -TenantId $TenantId -AccountId $azContext.Account.Id
  118.     return $azContext
  119. }
  120. Write-Host "Starting..." -ForegroundColor Green
  121.  
  122. Import-Module azureadpreview
  123. Write-Host "imported modules" -ForegroundColor Green
  124. $ActivationHours = 10 #Depending on your Tenant Settings this has to be changed to your max activation hours
  125. $schedule = New-Object Microsoft.Open.MSGraph.Model.AzureADMSPrivilegedSchedule
  126. $schedule.Type = "Once"
  127. $schedule.StartDateTime = (Get-Date).ToUniversalTime().AddSeconds(60).ToString("yyyy-MM-ddTHH:mm:ss.fffZ") #Start Time is set to 60 seconds from the point the script has been ran
  128. $schedule.endDateTime = (Get-Date).ToUniversalTime().AddHours($ActivationHours).ToString("yyyy-MM-ddTHH:mm:ss.fffZ") #Sets end time
  129.  
  130. do {
  131.     Write-Host "1. Activate All Roles`n2. Active Single Roles"
  132.     $Choice = Read-Host "Enter your choice (1-2)"
  133. } until ($Choice -eq 1 -or $Choice -eq 2)
  134.  
  135. switch ($choice) {
  136.     "1" {
  137.         Enable-AllPimRoles
  138.      }
  139.     "2"{
  140.         Enable-PimRole
  141.     }
  142.     Default {}
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement