This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Get-RandomPIN.ps1
137 lines (117 loc) · 2.58 KB
/
Get-RandomPIN.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
###############################################################################################################
# Language : PowerShell 5.0
# Filename : Get-RandomPIN.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Generate PINs with freely definable number of numbers
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Generate PINs with freely definable number of numbers
.DESCRIPTION
Generate PINs with freely definable number of numbers. You can also set the smallest and greatest possible number.
.EXAMPLE
Get-RandomPIN -Length 8
PIN
---
18176072
.EXAMPLE
Get-RandomPIN -Length 6 -Count 5 -Minimum 4 -Maximum 8
Count PIN
----- ---
1 767756
2 755655
3 447667
4 577646
5 644665
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Get-RandomPIN.README.md
#>
function Get-RandomPIN
{
[CmdletBinding(DefaultParameterSetName='NoClipboard')]
param(
[Parameter(
Position=0,
HelpMessage='Length of the PIN (Default=4)')]
[ValidateScript({
if($_ -eq 0)
{
throw "Length of the PIN can not be 0!"
}
else
{
return $true
}
})]
[Int32]$Length=4,
[Parameter(
ParameterSetName='NoClipboard',
Position=1,
HelpMessage='Number of PINs to be generated (Default=1)')]
[ValidateScript({
if($_ -eq 0)
{
throw "Number of PINs to be generated can not be 0"
}
else
{
return $true
}
})]
[Int32]$Count=1,
[Parameter(
ParameterSetName='Clipboard',
Position=1,
HelpMessage='Copy PIN to clipboard')]
[switch]$CopyToClipboard,
[Parameter(
Position=2,
HelpMessage='Smallest possible number (Default=0)')]
[Int32]$Minimum=0,
[Parameter(
Position=3,
HelpMessage='Greatest possible number (Default=9)')]
[ValidateScript({
if($_ -lt $Minimum)
{
throw "Minimum can not be greater than maximum!"
}
})]
[Int32]$Maximum=9
)
Begin{
}
Process{
for($i = 1; $i -ne $Count + 1; $i++)
{
$PIN = [String]::Empty
while($PIN.Length -lt $Length)
{
# Create random numbers
$PIN += (Get-Random -Minimum $Minimum -Maximum $Maximum).ToString()
}
# Return result
if($Count -eq 1)
{
# Set to clipboard
if($CopyToClipboard)
{
Set-Clipboard -Value $PIN
}
[pscustomobject] @{
PIN = $PIN
}
}
else
{
[pscustomobject] @{
Count = $i
PIN = $PIN
}
}
}
}
End{
}
}