Public/Helpers/New-CWCMFA.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function New-CWCMFA {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'None')]
    param(
        $DisplayName = 'CW Control',
        $UserAccount
    )
    $Possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
    $Key = ""
    while ($Key.Length -lt 16) {
        $Key += $Possible.ToCharArray() | Get-Random
    }

    $otpauth = "otpauth://totp/$($DisplayName):$($UserAccount)?secret=$Key"
    Add-Type -AssemblyName System.Web
    $otpauthEncoded = [System.Web.HTTPUtility]::UrlEncode($otpauth)
    $qrUrl = "https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=$($otpauthEncoded)&chld=H|0"
    if ($PSCmdlet.ShouldProcess("New-CWCMFA")) {
        [pscustomobject]@{
            'QR' = $qrUrl
            'OTP' = "ms:$Key"
        }
    }
}