Skip to content

Commit 1b56cdc

Browse files
Code optimization / PSScriptInfo / MIT License
Some small code optimizations for publishing on the Powershell-Gallery / Header was replaced by PSScriptInfo / License has been changed to MIT License
1 parent 1af1ebc commit 1b56cdc

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

runbook-dynamicgroup-mfa.ps1

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
<#
2-
Azure Runbook - Dynamic Group - MFA State
3-
4-
This script is designed for an Azure Runbook to assign users to two Azure AD groups based on their MFA capability (capable / non-capable).
5-
Before running the runbook, you need to set up an automation account with a managed identity.
6-
7-
The managed identity requires the following Graph Permissions:
8-
- User.Read.All
9-
- Group.Read.All
10-
- Group.ReadWrite.All
11-
- UserAuthenticationMethod.Read.All
12-
13-
The script requires the following modules:
14-
- Microsoft.Graph.Authentication
15-
- Microsoft.Graph.Groups
16-
- Microsoft.Graph.Identity.SignIns
17-
- Microsoft.Graph.Users
18-
19-
!!! Important: Define the variables for the two necessary groups in the Automation Variables as "dynamicmfa_groupid_capable" and "dynamicmfa_groupid_noncapable", or hardcode them in this script. !!!
20-
21-
Version: 0.2
22-
Creator: Dominik Gilgen (https://github.com/M365-Consultant)
23-
Date of creation: 2023-09-22
24-
License: CC BY-SA 4.0 (Attribution-ShareAlike 4.0 International)
1+
<#PSScriptInfo
2+
.VERSION 0.3
3+
.GUID 21da31c8-4f69-419d-8a3f-f16168b8f3ae
4+
.AUTHOR Dominik Gilgen
5+
.COMPANYNAME Dominik Gilgen (Personal)
6+
.COPYRIGHT 2023 Dominik Gilgen. All rights reserved.
7+
.LICENSEURI https://github.com/M365-Consultant/EntraID-MFA-DynamicGroup/blob/main/LICENSE
8+
.PROJECTURI https://github.com/M365-Consultant/EntraID-MFA-DynamicGroup
9+
.EXTERNALMODULEDEPENDENCIES Microsoft.Graph.Authentication,Microsoft.Graph.Groups,Microsoft.Graph.Identity.SignIns,Microsoft.Graph.Users
2510
#>
2611

12+
<#
13+
14+
.DESCRIPTION
15+
Azure Runbook - Dynamic Group - MFA State
16+
17+
This script is designed for an Azure Runbook to assign users to two Azure AD groups based on their MFA capability (capable / non-capable).
18+
Before running the runbook, you need to set up an automation account with a managed identity.e).
19+
20+
The managed identity requires the following Graph Permissions:
21+
- User.Read.All
22+
- Group.Read.All
23+
- Group.ReadWrite.All
24+
- UserAuthenticationMethod.Read.All
25+
26+
The script requires the following modules:
27+
- Microsoft.Graph.Authentication
28+
- Microsoft.Graph.Groups
29+
- Microsoft.Graph.Identity.SignIns
30+
- Microsoft.Graph.Users
31+
32+
!!! Important: Define the variables for the two necessary groups in the Automation Variables as "dynamicmfa_groupid_capable" and "dynamicmfa_groupid_noncapable", or hardcode them in this script. !!!
33+
34+
#>
35+
2736

2837
#variables (define them on the Automation Variables):
2938
$groupid_capable = Get-AutomationVariable -Name 'dynamicmfa_groupid_capable'
@@ -40,14 +49,14 @@ $members_noncapable = Get-MgGroupMember -GroupId $groupid_noncapable -All
4049

4150

4251
foreach ($user in $users) {
43-
$mfa = Get-MgUserAuthenticationMethod -UserId $user.UserPrincipalName -All | where {$_.AdditionalProperties."@odata.type" -ne "#microsoft.graph.passwordAuthenticationMethod"}
52+
$mfa = Get-MgUserAuthenticationMethod -UserId $user.UserPrincipalName -All | Where-Object {$_.AdditionalProperties."@odata.type" -ne "#microsoft.graph.passwordAuthenticationMethod"}
4453
if ($mfa.Count -gt 0) {
45-
if ($members_capable.Id -notcontains $user.Id){ New-MgGroupMember -GroupId $groupid_capable -DirectoryObjectId $user.Id ; Write-Output $user.UserPrincipalName "added to MFA capable group. User-ID:" $user.Id }
46-
if ($members_noncapable.Id -contains $user.Id){ Remove-MgGroupMemberByRef -GroupId $groupid_noncapable -DirectoryObjectId $user.Id ; Write-Output $user.UserPrincipalName "removed from MFA non-capable group. User-ID:" $user.Id }
54+
if ($members_capable.Id -notcontains $user.Id){ New-MgGroupMember -GroupId $groupid_capable -DirectoryObjectId $user.Id ; $output = $user.UserPrincipalName + " added to MFA capable group. User-ID: " + $user.Id ; Write-Output $output }
55+
if ($members_noncapable.Id -contains $user.Id){ Remove-MgGroupMemberByRef -GroupId $groupid_noncapable -DirectoryObjectId $user.Id ; $output = $user.UserPrincipalName + " removed from MFA non-capable group. User-ID: " + $user.Id ; Write-Output $output }
4756
}
4857
else{
49-
if($members_noncapable.Id -notcontains $user.Id){ New-MgGroupMember -GroupId $groupid_noncapable -DirectoryObjectId $user.Id ; Write-Output $user.UserPrincipalName "added to MFA non-capable group. User-ID:" $user.Id }
50-
if($members_capable.Id -contains $user.Id){ Remove-MgGroupMemberByRef -GroupId $groupid_capable -DirectoryObjectId $user.Id ; Write-Output $user.UserPrincipalName "removed from MFA capable group. User-ID:" $user.Id }
58+
if($members_noncapable.Id -notcontains $user.Id){ New-MgGroupMember -GroupId $groupid_noncapable -DirectoryObjectId $user.Id ; $output = $user.UserPrincipalName + " added to MFA non-capable group. User-ID: " + $user.Id ; Write-Output $output }
59+
if($members_capable.Id -contains $user.Id){ Remove-MgGroupMemberByRef -GroupId $groupid_capable -DirectoryObjectId $user.Id ; $output = $user.UserPrincipalName + " removed from MFA capable group. User-ID: " + $user.Id ; Write-Output $output }
5160
}
5261
}
5362

0 commit comments

Comments
 (0)