SVITSO.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




# Import-Module MSOnline

function Get-SVUserAuthMethods {
    param ( $upns )
    $index = 1
    $total = $upns.count
    foreach ($upn in $upns) {
        $user = Get-MsolUser -UserPrincipalName $upn
        $user | select UserPrincipalName, DisplayName -ExpandProperty StrongAuthenticationMethods | select UserPrincipalName, DisplayName, IsDefault, MethodType
        
        $progress = [math]::Round(100 * $index / $total, 2)
        Write-Progress -Activity "gathering users authentication methods" -Status "$progress% Complete:" -PercentComplete $progress
        $index++
    }
}

function Add-SVCallAuthMethod {
    param ( 
        [Parameter(Mandatory = $true)]
        $upns,
        [switch]
        $force
    )

    $call_method = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationMethod
    $call_method.MethodType = "TwoWayVoiceMobile"
    $call_method.IsDefault = $false

    $index = 1
    $total = $upns.count
    foreach ($upn in $upns) {
        $user = Get-MsolUser -UserPrincipalName $upn
        if ($user.StrongAuthenticationMethods.count -eq 0 ) {
            $call_method.IsDefault = $true
            if(!$force) {
                Write-Host "[$upn] has no authentication method!"
                continue
            }  
        }
        if ($user.StrongAuthenticationMethods.methodtype -contains "TwoWayVoiceMobile" -and !$force) {
            Write-Host "[$upn] has already call-me option"
            continue
        }
        if ($user.StrongAuthenticationMethods.methodtype -notcontains 'OneWaySMS' -and !$force) {
            Write-Host "[$upn] does not have SMS option"
            continue
        }

        $user.StrongAuthenticationMethods.add($call_method)
        Set-MsolUser -UserPrincipalName $upn -StrongAuthenticationMethods $user.StrongAuthenticationMethods

        $progress = [math]::Round(100 * $index / $total, 2)
        Write-Progress -Activity "updating users authentication methods" -Status "$progress% Complete:" -PercentComplete $progress
        $index++
    }
}

function Add-SVSMSAuthMethod {
    param ( 
        [Parameter(Mandatory = $true)]
        $upns,
        [switch]
        $force
    )
    
    $sms_method = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationMethod
    $sms_method.MethodType = "OneWaySMS"
    $sms_method.IsDefault = $false

    $index = 1
    $total = $upns.count
    foreach ($upn in $upns) {
        $user = Get-MsolUser -UserPrincipalName $upn
        if ($user.StrongAuthenticationMethods.count -eq 0 ) {
            $sms_method.IsDefault = $true
            if(!$force) {
                Write-Host "[$upn] has no authentication method!"
                continue
            }  
        }
        if ($user.StrongAuthenticationMethods.methodtype -contains "OneWaySMS" -and !$force) {
            Write-Host "[$upn] has already SMS option"
            continue
        }

        $user.StrongAuthenticationMethods.add($sms_method)
        Set-MsolUser -UserPrincipalName $upn -StrongAuthenticationMethods $user.StrongAuthenticationMethods

        $progress = [math]::Round(100 * $index / $total, 2)
        Write-Progress -Activity "updating users authentication methods" -Status "$progress% Complete:" -PercentComplete $progress
        $index++
    }
}

function Update-SVDefaultAuthMethod {
    param ( 
        [Parameter(Mandatory = $true)]
        $upns,

        [Parameter(Mandatory = $true)]
        [ValidateSet("OneWaySMS", "TwoWayVoiceMobile", "PhoneAppNotification", "PhoneAppOTP")]
        [String]
        $method = $false
    )

    $index = 1
    $total = $upns.count
    foreach ($upn in $upns) {
        $user = Get-MsolUser -UserPrincipalName $upn
        if ($user.StrongAuthenticationMethods.count -eq 0) {
            Write-Host "[$upn] has no authentication method!"
            continue
        }

        if ($user.StrongAuthenticationMethods.MethodType -notcontains $method) {
            Write-Host "[$upn] has no `"$method`" method to make it the default!"
            continue
        }
        
        foreach ($_method in $user.StrongAuthenticationMethods) {
            $_method.isdefault = $false
            if ($_method.MethodType -eq $method) {
                $_method.IsDefault = $true
            }
        }

        Set-MsolUser -UserPrincipalName $upn -StrongAuthenticationMethods $user.StrongAuthenticationMethods

        $progress = [math]::Round(100 * $index / $total, 2)
        Write-Progress -Activity "updating users authentication methods" -Status "$progress% Complete:" -PercentComplete $progress
        $index++
    }
}

function Remove-SvDisabledDevices {
    $disabled_devices = Get-AzureADDevice -Filter "AccountEnabled eq false" -All $true

    $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', 'accept the operation'
    $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'Cancel the operation'

    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

    $devices_num = $disabled_devices.count
    $title = 'Remove Disabled Devices'
    $message = "Are you sure to delete all disabled devices which are ($devices_num) devices?"
    $result = $host.ui.PromptForChoice($title, $message, $options, 0)

    switch ($result) {
        0 {
            $index = 1
            $total = $udevices_num
            foreach ($device in $disabled_devices) {
                Remove-AzureADDevice -ObjectId $device.ObjectId
                $progress = [math]::Round(100 * $index / $total, 2)
                Write-Progress -Activity "deleteing devices" -Status "$progress% Complete:" -PercentComplete $progress
                $index++
            }
        }
        1 { 'no problem :)' }
    }
}

function Disable-SvDevices {
    param (
        [Parameter(Mandatory = $true)]
        [int]
        $Timeframe
    )
    $devices = Get-AzureADDevice -All $true
    
    $date = get-date
    if ($Timeframe -ge 0) { $Timeframe = -1 * $Timeframe }
    $date = $date.AddDays($Timeframe)

    $devices_targeted = $devices | where ApproximateLastLogonTimeStamp -LE $date

    $index = 1
    $total = $devices_targeted.count
    foreach ($device in $devices_targeted) {
        Set-AzureADDevice -ObjectId $device.ObjectId -AccountEnabled $false
        $progress = [math]::Round(100 * $index / $total, 2)
        Write-Progress -Activity "updating users authentication methods" -Status "$progress% Complete:" -PercentComplete $progress
        $index++
    }
}