functions/Update-AzureADUserAuthenticationMethod.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
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
function Update-AzureADUserAuthenticationMethod {
    <#
.SYNOPSIS
    Modifies an authentication method for the user. Manages SMS Sign In for mobile phone method.
.DESCRIPTION
    Modifies an authentication method for the user. Manages SMS Sign In for mobile phone method.
    Use to modify an existing authentication method for the user. To create a new method, use New-AzureADUserAuthenticationMethod.
.EXAMPLE
    PS C:\>Update-AzureADUserAuthenticationMethod user@contoso.com -Phone -PhoneNumber '+61412345679' -PhoneType mobile
    Modifies the existing mobile phone number for the user.
.EXAMPLE
    PS C:\>Update-AzureADUserAuthenticationMethod -Phone -UPN user1@contoso.com -EnableSmsSignIn
    Enables SMS sign-in for the existing mobile phone authentication method for the user.
.EXAMPLE
    PS C:\>Update-AzureADUserAuthenticationMethod user@contoso.com -Password -NewPassword "password"
    Sets "password" as a new password for the user. Doesn't return the operation result.
.EXAMPLE
    PS C:\>Update-AzureADUserAuthenticationMethod user@contoso.com -Password -NewPassword "password" -ReturnResult
    Sets "password" as a new password for the user and waits 5 seconds for the operation result.
.EXAMPLE
    PS C:\>Update-AzureADUserAuthenticationMethod clouduser@contoso.com -Password
    Sets new system generated password for the user. Not available for syncronised users.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $True, ParameterSetName = 'pin')]
        [switch]
        $Pin,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [switch]
        $Oath,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'password')]
        [switch]
        $Password,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'securityQuestion')]
        [switch]
        $SecurityQuestion,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'default')]
        [switch]
        $Default,
        
        [Alias('UserId', 'UPN', 'UserPrincipalName')]
        [Parameter(Mandatory = $True, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string]
        $ObjectId,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'pin', Position = 2)]
        [string]
        $NewPin,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [string]
        $SecretKey,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [int]
        $TimeIntervalInSeconds,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [string]
        $SerialNumber,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [string]
        $Manufacturer,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [string]
        $Model,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'phone')]
        [string]
        $PhoneNumber,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'phone')]
        [ValidateSet("mobile", "alternateMobile", "office")]
        [string]
        $PhoneType,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'email', Position = 2)]
        [string]
        $EmailAddress,
        
        [Parameter(Mandatory = $False, ParameterSetName = 'password')]
        [string]
        $NewPassword,
        
        [Parameter(Mandatory = $False, ParameterSetName = 'password')]
        [switch]
        $ReturnResult,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'securityQuestion')]
        [string]
        $Question,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'securityQuestion')]
        [string]
        $Answer,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'default')]
        [string]
        $DefaultMethod,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'enableSmsSignIn')]
        [switch]
        $EnableSmsSignIn,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'disableSmsSignIn')]
        [switch]
        $DisableSmsSignIn
    )
    
    begin {
        Assert-GraphConnection -Cmdlet $PSCmdlet
    }
    process {
        switch ($PSCmdlet.ParameterSetName) {
            "phone" {
                $methodId = switch ($PhoneType) {
                    'alternateMobile' { 'b6332ec1-7057-4abe-9331-3d72feddfe41' }
                    'mobile' { '3179e48a-750b-4051-897c-87b9720928f7' }
                    'office' { 'e37fc753-ff3b-4958-9484-eaa9425c82bc' }
                }
                
                $postParams = @{
                    phoneNumber = $PhoneNumber
                    phoneType   = $PhoneType
                }
                $json = $postparams | ConvertTo-Json -Depth 99 -Compress
                Invoke-AzureAdRequest -Method PUT -Query "users/$ObjectId/authentication/phoneMethods/$methodId" -Body $json
                break
            }
            "email" {
                $postParams = @{
                    emailAddress = $EmailAddress
                }
                $json = $postparams | ConvertTo-Json -Depth 99 -Compress
                Invoke-AzureAdRequest -Method PUT -Query "users/$ObjectId/authentication/emailMethods/3ddfcfc8-9383-446f-83cc-3ab9be4be18f" -Body $json
                break
            }
            "password" {
                $parameters = @{
                    Method = 'POST'
                    Query = "users/$ObjectId/authentication/passwordMethods/28c10230-6103-485e-b985-444c60001490/resetPassword"
                }
                if ($NewPassword) {
                    $parameters.Body = @{
                        newPassword = $NewPassword
                    } | ConvertTo-Json -Depth 99 -Compress
                }
                $response = Invoke-AzureAdRequest @parameters -Raw
                if (-not $ReturnResult) { return $response }
                
                # TODO: If RAW, use invoke-webrequest to get response headers.
                # Check password reset result
                if ($response.StatusCode -eq "202") {
                    Write-Host "Waiting for a response..."
                    Start-Sleep -Seconds 5
                    (Invoke-WebRequest -UseBasicParsing -Headers (Get-Token | ConvertTo-AuthHeader) -Uri $response.Headers.Location -Method Get).Content
                }
                
                return $response.Content
            }
            "enableSmsSignIn" {
                Invoke-AzureAdRequest -Method PUT -Query "users/$ObjectId/authentication/phoneMethods/3179e48a-750b-4051-897c-87b9720928f7/enableSmsSignIn" -Body $json
                break
            }
            "disableSmsSignIn" {
                Invoke-AzureAdRequest -Method PUT -Query "users/$ObjectId/authentication/phoneMethods/3179e48a-750b-4051-897c-87b9720928f7/disableSmsSignIn" -Body $json
                break
            }
            default {
                throw "Setting the $($PSCmdlet.ParameterSetName) method is not yet supported."
            }
        }
    }
}