-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabs-Init-Parameters.ps1
222 lines (183 loc) · 8.43 KB
/
Labs-Init-Parameters.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Param(
[string] [Parameter(Mandatory=$true)] $ArtifactStagingDirectory,
[string] $imagesJson,
[int]$buildId
)
function New-SWRandomPassword {
<#
.Synopsis
Generates one or more complex passwords designed to fulfill the requirements for Active Directory
.DESCRIPTION
Generates one or more complex passwords designed to fulfill the requirements for Active Directory
.EXAMPLE
New-SWRandomPassword
C&3SX6Kn
Will generate one password with a length between 8 and 12 chars.
.EXAMPLE
New-SWRandomPassword -MinPasswordLength 8 -MaxPasswordLength 12 -Count 4
7d&5cnaB
!Bh776T"Fw
9"C"RxKcY
%mtM7#9LQ9h
Will generate four passwords, each with a length of between 8 and 12 chars.
.EXAMPLE
New-SWRandomPassword -InputStrings abc, ABC, 123 -PasswordLength 4
3ABa
Generates a password with a length of 4 containing atleast one char from each InputString
.EXAMPLE
New-SWRandomPassword -InputStrings abc, ABC, 123 -PasswordLength 4 -FirstChar abcdefghijkmnpqrstuvwxyzABCEFGHJKLMNPQRSTUVWXYZ
3ABa
Generates a password with a length of 4 containing atleast one char from each InputString that will start with a letter from
the string specified with the parameter FirstChar
.OUTPUTS
[String]
.NOTES
Written by Simon Wåhlin, blog.simonw.se
I take no responsibility for any issues caused by this script.
.FUNCTIONALITY
Generates random passwords
.LINK
http://blog.simonw.se/powershell-generating-random-password-for-active-directory/
#>
[CmdletBinding(DefaultParameterSetName='FixedLength',ConfirmImpact='None')]
[OutputType([String])]
Param
(
# Specifies minimum password length
[Parameter(Mandatory=$false,
ParameterSetName='RandomLength')]
[ValidateScript({$_ -gt 0})]
[Alias('Min')]
[int]$MinPasswordLength = 8,
# Specifies maximum password length
[Parameter(Mandatory=$false,
ParameterSetName='RandomLength')]
[ValidateScript({
if($_ -ge $MinPasswordLength){$true}
else{Throw 'Max value cannot be lesser than min value.'}})]
[Alias('Max')]
[int]$MaxPasswordLength = 12,
# Specifies a fixed password length
[Parameter(Mandatory=$false,
ParameterSetName='FixedLength')]
[ValidateRange(1,2147483647)]
[int]$PasswordLength = 8,
# Specifies an array of strings containing charactergroups from which the password will be generated.
# At least one char from each group (string) will be used.
[String[]]$InputStrings = @('abcdefghijkmnpqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!"#%&'),
# Specifies a string containing a character group from which the first character in the password will be generated.
# Useful for systems which requires first char in password to be alphabetic.
[String] $FirstChar,
# Specifies number of passwords to generate.
[ValidateRange(1,2147483647)]
[int]$Count = 1
)
Begin {
Function Get-Seed{
# Generate a seed for randomization
$RandomBytes = New-Object -TypeName 'System.Byte[]' 4
$Random = New-Object -TypeName 'System.Security.Cryptography.RNGCryptoServiceProvider'
$Random.GetBytes($RandomBytes)
[BitConverter]::ToUInt32($RandomBytes, 0)
}
}
Process {
For($iteration = 1;$iteration -le $Count; $iteration++){
$Password = @{}
# Create char arrays containing groups of possible chars
[char[][]]$CharGroups = $InputStrings
# Create char array containing all chars
$AllChars = $CharGroups | ForEach-Object {[Char[]]$_}
# Set password length
if($PSCmdlet.ParameterSetName -eq 'RandomLength')
{
if($MinPasswordLength -eq $MaxPasswordLength) {
# If password length is set, use set length
$PasswordLength = $MinPasswordLength
}
else {
# Otherwise randomize password length
$PasswordLength = ((Get-Seed) % ($MaxPasswordLength + 1 - $MinPasswordLength)) + $MinPasswordLength
}
}
# If FirstChar is defined, randomize first char in password from that string.
if($PSBoundParameters.ContainsKey('FirstChar')){
$Password.Add(0,$FirstChar[((Get-Seed) % $FirstChar.Length)])
}
# Randomize one char from each group
Foreach($Group in $CharGroups) {
if($Password.Count -lt $PasswordLength) {
$Index = Get-Seed
While ($Password.ContainsKey($Index)){
$Index = Get-Seed
}
$Password.Add($Index,$Group[((Get-Seed) % $Group.Count)])
}
}
# Fill out with chars from $AllChars
for($i=$Password.Count;$i -lt $PasswordLength;$i++) {
$Index = Get-Seed
While ($Password.ContainsKey($Index)){
$Index = Get-Seed
}
$Password.Add($Index,$AllChars[((Get-Seed) % $AllChars.Count)])
}
Write-Output -InputObject $(-join ($Password.GetEnumerator() | Sort-Object -Property Name | Select-Object -ExpandProperty Value))
}
}
}
#generate 8 length random password
echo "ArtifactStagingDirectory="
echo $ArtifactStagingDirectory
$parametersFilePath=$ArtifactStagingDirectory+'\labs\labs-azuredeploy.parameters.json'
$parametersFilePath=[System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $parametersFilePath))
echo "parametersFilePath="
echo $parametersFilePath
$password=New-SWRandomPassword -InputStrings abcdefghijkmnpqrstuvwxyz, ABCEFGHJKLMNPQRSTUVWXYZ, 1234567890 -PasswordLength 8 -FirstChar abcdefghijkmnpqrstuvwxyzABCEFGHJKLMNPQRSTUVWXYZ;
$parametersFileContent = Get-Content $parametersFilePath | Out-String
$parametersFileContent=$parametersFileContent.Replace("%{adminPassword}%", $password);
#generte unique dns name
$dns=New-SWRandomPassword -InputStrings abcdefghijkmnpqrstuvwxyz -PasswordLength 8 -FirstChar abcdefghijkmnpqrstuvwxyz;
$dns=$dns+$buildId
$dns2=New-SWRandomPassword -InputStrings abcdefghijkmnpqrstuvwxyz -PasswordLength 8 -FirstChar abcdefghijkmnpqrstuvwxyz;
$dns2=$dns2+$buildId
$parametersFileContent=$parametersFileContent.Replace("%{dnsLabelPrefix}%", $dns);
$parametersFileContent=$parametersFileContent.Replace("%{dnsLabelPrefix2}%", $dns2);
#dynamic imageId from tfs build
function ConvertFrom-Base64String([string]$string)
{
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($string))
}
$imagesJsonString=ConvertFrom-Base64String $imagesJson
$images = ConvertFrom-Json -InputObject $imagesJsonString
foreach ($image in $images)
{
$vmName,$imageNum=$image.SourceSnapshot.vmName.Split('|')
$imageId=$image.SourceSnapshot.OutImageId
$parametersFileContent=$parametersFileContent.Replace("%{$imageNum}%", $imageId);
}
#generate ssh key and replace
$isRequiredSSH=$parametersFileContent.Contains("%{sshRSAPublicKey}%")
if($isRequiredSSH)
{
$sshkeyFolderPath = $ArtifactStagingDirectory+'\labs\sshkey';
$sshkeyFolderPath=[System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $sshkeyFolderPath))
$existing = [System.Boolean](Test-Path $sshkeyFolderPath)
if($existing)
{
Remove-Item $sshkeyFolderPath -recurse
}
New-Item $sshkeyFolderPath -type directory
$Email = "[email protected]"
$sshKeyFileName = $sshkeyFolderPath + "\id_rsa"
& 'C:\Program Files\Git\usr\bin\ssh-keygen.exe' -t rsa -C $Email -f $sshKeyFileName -P """"
$sshkeyPubFileName = $sshKeyFileName+".pub"
$publicKeyContent = [IO.File]::ReadAllText($sshkeyPubFileName)
$privateKeyContent = [IO.File]::ReadAllText($sshKeyFileName)
Write-Host " publick key : " + $publicKeyContent
Write-Host " private key : " + $privateKeyContent
$parametersFileContent=$parametersFileContent.Replace("%{sshRSAPublicKey}%", $publicKeyContent);
}
#save file
echo $parametersFileContent
out-File -FilePath $parametersFilePath -InputObject $parametersFileContent