NewPassword.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
## Generate Password Function
Function New-Password() {
    <#
 .SYNOPSIS
     Password Generator
  
 .DESCRIPTION
     The New-Password cmdlet allow you to generate random passwords.
    
 .PARAMETER NumberofPasswords
     Specifies Number of passwored to generate. Default is 1 random password.
 
 .PARAMETER AsSecureString
     Return passwords as secure strings.
  
 .PARAMETER Length
     Specifies the passwored length. Defualt is 12 chars.
  
 .PARAMETER SpecialCharacters
     Specifies the number of Special Characters in the password. Defualt is 3 chars.
   
 .EXAMPLE
        New-Password
        S0XOxe8SvaYW
 
  .EXAMPLE
        New-Password -NumberofPasswords 10
        VMLMu2DVLg6a
        H0faw#W6N@@H
        WZ0FV7U7jbVB
        p6vDelWGmefS
        HR9Ewjg#WQt3
        qrHYJdRTWaex
        q6meEJztl80R
        oNDals6@DrI7
        kKJVCbvAhFXm
        f4@8xTsUYpAI
 
    .EXAMPLE
            New-Password -Length 20
            e00yosTnRvthCHiJYKJs
 
    .EXAMPLE
            New-Password -NumberofPasswords 5 -Length 6
            ZSQHsL
            RqU5cY
            F7STnb
            DD34@O
            9drhe4
     
    .EXAMPLE
            New-Password -NumberofPasswords 1 -Length 10 -AsSecureString
            WARNING: Converting the Password 3@jedeVcjoBf to secure string, this is the last time you will see it.
            System.Security.SecureString
     
    .EXAMPLE
            New-Password -NumberofPasswords 5 -Length 10 -SpecialCharacters 3
            #zx4?1R@p1
            >xv=2RaAdG
            <u<4TDJAzL
 
 
 .NOTES
  Author: Saggie Haim
  Contact@saggiehaim.net
 #>

    [cmdletbinding()]
    param (
        [parameter(Position = 0,
            Mandatory = $false)]
        [string]$NumberofPasswords = 1,

        [parameter(Position = 1,
            Mandatory = $false)]
        [string]$Length = 12,

        [Parameter(Position = 2,
            Mandatory = $false)]
        [switch]$AsSecureString = $false,

        [parameter(Position = 3,
            Mandatory = $false)]
        [string]$SpecialCharacters = 3
    )
    $passwords = @()
    $inputRange = 48..122
    $SpecialCharactersRange = 33, 35, 60, 61, 62, 63, 64
    $exclude = 91..96
    $exclude += 58..64
    $randomRange = $inputRange | Where-Object { $exclude -notcontains $_ }
    For ($j = 0; $j -lt $NumberofPasswords; $j++) {
        $pass = $null
        For ($i = 0; $i -lt ($Length - $SpecialCharacters); $i++) {
            $rnd = (Get-Random -InputObject $randomRange) 
            $char = [char]$rnd
            $pass += $char
        }
        for ($k = 0; $k -lt $SpecialCharacters; $k++) {
            $rnd = (Get-Random -InputObject $SpecialCharactersRange) 
            $char = [char]$rnd
            $pass += $char
        }
        $pass = RandomisePassword $pass
        $passwords += $pass
        
    }
    if ($AsSecureString -eq $true) {
        $passwords = $passwords | ForEach-Object { Write-Warning "Converting the Password $_ to secure string, this is the last time you will see it."; ConvertTo-SecureString -String $_ -AsPlainText -Force }
        return $passwords
    }
    Return $passwords
}

function RandomisePassword {
    [cmdletbinding()]
    param (
        [parameter(Position = 0,
            Mandatory = $true)]
        [string]$Password
    ) 
    $i = 0
    while ($i -ne 3) {
        $CharArry = $Password.ToCharArray()
        $NewPass = $CharArry | Get-Random -Count $CharArry.length
        $Password = -join $NewPass
        $i++
    }
    return $Password
}