Powershell Script to temporarily activate PIM with MFA?

Bühler Gabriel 71 Reputation points
2024-05-03T13:10:03.2133333+00:00

Hello Everyone

I am trying to write a script that temporarily activated a PIM Role to my account...the issue is, that my account is secured with MFA, so this does not work:

$roleDisplayName = "License Administrator"
$roleDefinitionID = (Get-AzureADMSRoleDefinition -Filter "DisplayName eq '$roleDisplayName'").Id
$targetuserID = (Get-AzureADUser -ObjectId admgabriel@wingd.com).ObjectId  # Replace user ID
$schedule = New-Object Microsoft.Open.MSGraph.Model.AzureADMSPrivilegedSchedule
$schedule.Type = "Once"
$schedule.StartDateTime = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$schedule.EndDateTime =  ((Get-Date).AddDays(1)).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ")

# Create temporary active role assignment
Open-AzureADMSPrivilegedRoleAssignmentRequest -ProviderId 'aadRoles' -ResourceId $tenantID -RoleDefinitionId $roleDefinitionID -SubjectId $targetuserID -Type 'adminAdd' -AssignmentState 'Active' -schedule $schedule -reason "testing" -MFA



What can I do to activate it with MFA? The only solution I have found is to adjust my locations in the conditional access policy, but I don't want to do that.

Thank you for your help.

Kind regards,

Gabriel

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,131 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,767 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Thangaraj Lakshmanan 90 Reputation points
    2024-05-06T07:10:38.3733333+00:00

    Hello,

    Good day!

    Instead of using a user account with MFA, you could create a service principal and authenticate using a certificate or secret. This would bypass the MFA requirement. However, managing certificates or secrets securely is essential.

    https://learn.microsoft.com/en-us/azure/databricks/dev-tools/azure-powershell-login-service-principal

    https://learn.microsoft.com/en-us/entra/identity-platform/howto-create-service-principal-portal#set-up-authentication

    Regards,
    Lakshmanan

    0 comments No comments