functions/Remove-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
function Remove-AzureADUserAuthenticationMethod {
    <#
    .SYNOPSIS
        Removes an authentication method from the user.
    .DESCRIPTION
        Removes an authentication method from the user.
        Use to remove an existing authentication method for the user.
    .EXAMPLE
        PS C:\>Remove-AzureADUserAuthenticationMethod -Phone -PhoneType mobile user@contoso.com
        Removes the mobile phone authentication method for the user.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $True, ParameterSetName = 'pin')]
        [switch]
        $Pin,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [switch]
        $Oath,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'phone')]
        [switch]
        $Phone,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'email')]
        [switch]
        $Email,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'password')]
        [switch]
        $Password,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'FIDO2')]
        [switch]
        $FIDO2,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'passwordlessMicrosoftAuthenticator')]
        [switch]
        $PasswordlessMicrosoftAuthenticator,

        [Parameter(Mandatory = $True, ParameterSetName = 'MicrosoftAuthenticator')]
        [switch]
        $MicrosoftAuthenticator,

        [Parameter(Mandatory = $True, ParameterSetName = 'WindowsHelloForBusiness')]
        [switch]
        $WindowsHelloForBusiness,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'FIDO2')]
        [Parameter(Mandatory = $True, ParameterSetName = 'passwordlessMicrosoftAuthenticator')]
        [Parameter(Mandatory = $True, ParameterSetName = 'MicrosoftAuthenticator')]
        [Parameter(Mandatory = $True, ParameterSetName = 'WindowsHelloForBusiness')]
        [string]
        $MethodId,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'securityQuestion')]
        [switch]
        $SecurityQuestion,
        
        [Alias('UserId', 'UPN', 'UserPrincipalName')]
        [Parameter(Mandatory = $True, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string]
        $ObjectId,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'oath')]
        [string]
        $SerialNumber,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'phone')]
        [ValidateSet("mobile", "alternateMobile", "office")]
        [string]
        $PhoneType,
        
        [Parameter(Mandatory = $True, ParameterSetName = 'securityQuestion')]
        [string]
        $Question
        
    )
    
    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' }
                }
                Invoke-AzureAdRequest -Method DELETE -Query "users/$ObjectId/authentication/phoneMethods/$methodId"
                break
            }
            "email" {
                Invoke-AzureAdRequest -Method DELETE -Query "users/$ObjectId/authentication/emailMethods/3ddfcfc8-9383-446f-83cc-3ab9be4be18f"
                break
            }
            "FIDO2" {
                Invoke-AzureAdRequest -Method DELETE -Query "users/$ObjectId/authentication/fido2Methods/$MethodId"
                break
            }
            "passwordlessMicrosoftAuthenticator" {
                Invoke-AzureAdRequest -Method DELETE -Query "users/$ObjectId/authentication/passwordlessMicrosoftAuthenticatorMethods/$MethodId"
                break
            }
            "MicrosoftAuthenticator" {
                Invoke-AzureAdRequest -Method DELETE -Query "users/$ObjectId/authentication/MicrosoftAuthenticatorMethods/$MethodId"
                break
            }
            "WindowsHelloForBusiness" {
                Invoke-AzureAdRequest -Method DELETE -Query "users/$ObjectId/authentication/windowsHelloForBusinessMethods/$MethodId"
                break
            }
            default {
                throw "Removing the $($PSCmdlet.ParameterSetName) method is not yet supported."
            }
        }
    }
}