Public/get-AllegisUsername.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
function get-AllegisUsername {
    [cmdletbinding()]
    param(       
        [Parameter(Mandatory)] 
        [ValidateSet('firstInitial','fullName')]
        [string]
        $scheme='firstInitial',
        $firstname,
        $lastname,
        $maxlength=14, 
        $iteration)
        if ($scheme -eq 'firstInitial')
        {
            $firstlength=$iteration+1
            $overflow=$firstlength-$firstname.length
            if ($overflow -ge 1)
            {
                $username=$firstname.Substring(0,1) + $lastname
                $over=($username.Length + $overflow.ToString().length)-$maxlength
                if ($over -ge 1){
                    $username=$username.Substring(0,$maxlength-$overflow.ToString().length)
                }
                $username=$username+$overflow
            }else{
                $username=$firstname.Substring(0,$iteration+1) + $lastname
                $over=($username.Length)-$maxlength
                if ($over -ge 1){
                    $username=$username.Substring(0,$maxlength-$overflow.ToString().length)
                }
            }
        }elseif($scheme -eq 'fullName')
        {
            if ($iteration -eq 0){
                $username=$firstname +'.' + $lastname
                if ($username.Length -gt $maxlength){
                    $username=$username.Substring(0,$maxlength)
                }
            }else{
                $username=$firstname +'.' + $lastname
                if ($username.Length + $iteration.ToString().length -gt $maxlength){
                    $username=$username.Substring(0,$maxlength-$iteration.ToString().length)
                }
                $username=$username+$iteration.ToString()
            }
        }
        return $username
}