PoshBot.AzureAD.psm1

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Import-Module 'MSAL.PS'

# Slack text width with the formatting we use maxes out ~80 characters...
$Width = 80
$CommandsToExport = @()

function Find-AADUser {
    <#
    .SYNOPSIS
        Find Azure AD User
    .PARAMETER Identity displayName
        Azure AD displayName
    .USAGE
        !FindAADUser displayName
    .EXAMPLE
        !FindAADUser 'Darren Robinson'
        !FindAADUser 'darren.robinson@myazureaddomain.com'
    .EXAMPLE
        !SearchAADUser 'Darren Robinson'
        !SearchAADUser 'darren.robinson@myazuredomain.com'
    .DESCRIPTION !FindUser
        Search AzureAD user objects using displayName
    .LINK
        https://blog.darrenjrobinson.com
    #>

    [cmdletbinding()]
    [PoshBot.BotCommand(
        CommandName = 'FindAADUser',
        Aliases = ('SearchAADUser'),
        Permissions = 'read'
    )]
    param(      
        [parameter(position = 1,
            parametersetname = 'id', mandatory)]
        [string]$Identity,

        [PoshBot.FromConfig('AADCreds')]
        [parameter(mandatory)]
        [PSCredential]$AADCreds,

        [PoshBot.FromConfig('AADTenant')]
        [parameter(mandatory)]
        [string]$aadTenant,

        [PoshBot.FromConfig('FindAADUserAttributes')]
        [parameter(Mandatory)]
        [Array]$aadProperties #= @('givenName', 'surname', 'displayName', 'jobTitle', 'officeLocation', 'userPrincipalName', 'accountEnabled', 'employeeId', 'mail')
    )
    
    $aadID = [string]$AADCreds.UserName
    $aadSecret = [SecureString]$AADCreds.Password
    
    try {
        $myToken = Get-MsalToken -clientID $aadID -clientSecret $aadSecret -tenantID $aadTenant 
    }
    catch {
        Write-Verbose "Authentication to Azure AD failed. $($_)"   
        $o = $_ | Format-Table -AutoSize | Out-String -Width $Width 
    }

    $apiBeta = 'beta'
    $baseURI = 'https://graph.microsoft.com/'
    $usersURI = '/users'

    $resultUsers = $null
    
    $resultUsersURI = "$($baseURI)$($apiBeta)$($usersURI)?filter=startswith(displayName,`'$($Identity)`')"
    $resultUsers = Invoke-RestMethod -Headers @{Authorization = "Bearer $($myToken.AccessToken)" } `
        -Uri  $resultUsersURI `
        -Method Get

    if ($resultUsers.value.Count -ge 1) {
        $userObj = $resultUsers.value
        $o = $userObj | Select-Object -Property $aadProperties | Format-Table -AutoSize | Out-String -Width $Width
    }   
    else {
        $o = "Azure Active Directory User with displayName containing `'$($Identity)`' not found!"
    }
    New-PoshBotCardResponse -Type Normal -Text $o  
}
$CommandsToExport += 'Find-AADUser'

function Get-AADUser {
    <#
    .SYNOPSIS
        Get Azure AD User
    .PARAMETER Identity <UPN>
        Azure AD UPN
    .USAGE
        !GetAADUser <UPN>
    .EXAMPLE
        !GetAADUser 'Darren.Robinson@myaadtenant.com.au'
         
        Return MFA Info for the user
        !GetAADUser 'Darren.Robinson@myaadtenant.com.au' -mfa
 
        Return Group Memberships for the user
        !GetAADUser 'Darren.Robinson@myaadtenant.com.au' -groups
 
        Return Group and MFA info for the user
        !GetAADUser 'Darren.Robinson@myaadtenant.com.au' -groups -mfa
 
    .DESCRIPTION !GetAADUser
        Get an Azure AD User using UPN
    .LINK
        https://blog.darrenjrobinson.com
    #>

    [cmdletbinding()]
    [PoshBot.BotCommand(
        CommandName = 'GetAADUser',
        Aliases = ('AADUser'),
        Permissions = 'read'
    )]
    param(
        [parameter(position = 1,
            parametersetname = 'id')]
        [string]$Identity,

        [PoshBot.FromConfig('AADCreds')]
        [parameter(mandatory)]
        [PSCredential]$AADCreds,

        [PoshBot.FromConfig('AADTenant')]
        [parameter(mandatory)]
        [string]$aadTenant,

        [PoshBot.FromConfig('GetAADUserAttributes')]
        [parameter(Mandatory)]
        [Array]$aadProperties, #= @('givenName', 'surname', 'displayName', 'jobTitle', 'officeLocation', 'userPrincipalName', 'accountEnabled', 'employeeId', 'mail'),

        [switch]$mfa,

        [switch]$groups                
    )

    $aadID = [string]$AADCreds.UserName
    $aadSecret = [SecureString]$AADCreds.Password

    try {
        $myToken = Get-MsalToken -clientID $aadID -clientSecret $aadSecret -tenantID $aadTenant 
    }
    catch {
        Write-Verbose "Authentication to Azure AD failed. $($_)"   
        $o = $_ | Format-Table -AutoSize | Out-String -Width $Width 
    }

    $apiBeta = 'beta'
    $baseURI = 'https://graph.microsoft.com/'
    $mfaURI = '/reports/credentialUserRegistrationDetails'
    $usersURI = '/users'

    $resultUsers = $null
    $resultMFA = $null 
    $resultGroups = $null 
    
    $resultUsersURI = "$($baseURI)$($apiBeta)$($usersURI)?filter=userPrincipalName eq `'$($Identity)`'"
    $resultUsers = Invoke-RestMethod -Headers @{Authorization = "Bearer $($myToken.AccessToken)" } `
        -Uri  $resultUsersURI `
        -Method Get

    if ($resultUsers.value.Count -eq 1) {
        $userObj = $resultUsers.value
        
        if ($mfa) {
            $aadProperties = $aadProperties + @('ssprIsRegistered', 'ssprIsEnabled', 'mfaSsprIsCapable', 'mfaIsRegistered', 'mfaAuthMethods')
            $resultMFAURI = "$($baseURI)$($apiBeta)$($mfaURI)?filter=userPrincipalName eq `'$($Identity)`'"
            $resultMFA = Invoke-RestMethod -Headers @{Authorization = "Bearer $($myToken.AccessToken)" } `
                -Uri  $resultMFAURI `
                -Method Get
        
            if ($resultMFA.value) {
                $userObj | Add-Member -Type NoteProperty -Name "ssprIsRegistered" -Value $resultMFA.value.isRegistered
                $userObj | Add-Member -Type NoteProperty -Name "ssprIsEnabled" -Value $resultMFA.value.isEnabled
                $userObj | Add-Member -Type NoteProperty -Name "mfaSsprIsCapable" -Value $resultMFA.value.isCapable
                $userObj | Add-Member -Type NoteProperty -Name "mfaIsRegistered" -Value $resultMFA.value.isMfaRegistered
                $userObj | Add-Member -Type NoteProperty -Name "mfaAuthMethods" -Value $resultMFA.value.authMethods
            }
        }
        
        if ($groups) {
            $resultGroupURI = "$($baseURI)$($apiBeta)/users/$($Identity)/memberOf"
            $resultGroups = Invoke-RestMethod -Headers @{Authorization = "Bearer $($myToken.AccessToken)" } `
                -Uri  $resultGroupURI `
                -Method Get

            if ($resultGroups.value) {    
                $aadProperties = $aadProperties + @('groups')        
                $userObj | Add-Member -Type NoteProperty -Name "groups" -Value @($resultGroups.value.displayName -join ", ") 
            }
        }
        $o = $userObj | Select-Object -Property $aadProperties | Format-List | Out-String -Width $Width
    }
    else {
        $o = "`'$($Identity)`' not found. Try searching for the Azure AD user using !FindAADUser displayName"
    }
    
    New-PoshBotCardResponse -Type Normal -Text $o 
}
$CommandsToExport += 'Get-AADUser'

Export-ModuleMember -Function $CommandsToExport