-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-VM01.ps1
378 lines (214 loc) · 12.5 KB
/
New-VM01.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#Generic Konfiguration
$vSwitch = "LAN"
$VHDXCurrentPath = "G:\Hyper-V-VMs\Q\Q\Virtual Hard Disks"
$VHDXDestinationPath = "G:\Hyper-V-VMs\VMauto"
$SubMaskBit = "24"
#####################################
#####################################
#####################################
############Skapa ADD01##############
#Skapa variabel för ADD01
#AD Server konfiguration variabel
$ADName = "ADK001"
$ADIP = "192.168.0.220"
#Domän information
$DomainMode = "WinThreshold"
$ForestMode = "WinThreshold"
$DomainName = "xstile.se"
$DSRMPWord = ConvertTo-SecureString -String "Sommar2020" -AsPlainText -Force
#Lokal admin konto används för att kunna logga på ADD01 för att konfigurera samt för invoke-command
$DCLocalUser = "$ADName\Administrator"
$DCLocalPWord = ConvertTo-SecureString -String "Sommar2020" -AsPlainText -Force
$DCLocalCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DCLocalUser, $DCLocalPWord
#Domän admin konto används efter att du har promota server till AD
$DomainUser = "$DomainName\administrator"
$DomainPWord = ConvertTo-SecureString -String "Sommar2020" -AsPlainText -Force
$DomainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DomainUser, $DomainPWord
#Kopiera VM template som vi har skapat.
Write-Verbose "Check if folder for VM exist"
$cpath = Test-Path "G:\Hyper-V-VMs\VMauto\$($ADName)"
if($cpath -eq $false){
Write-Verbose "Folder doest not exist, creating folder" -Verbose
New-item -Name $ADName -Path "G:\Hyper-V-VMs\VMauto\" -ItemType Directory | Out-Null
Write-Verbose "Folder is created, copy template VM" -Verbose
Copy-Item "$VHDXCurrentPath\MASTER.vhdx" "$VHDXDestinationPath\$ADNAME\$ADNAME.vhdx"
}else{
Write-Verbose "Folder already exist" -Verbose
Write-Verbose "copy template VM" -Verbose
Copy-Item "$VHDXCurrentPath\MASTER.vhdx" "$VHDXDestinationPath\$DCNAME\$ADNAME.vhdx"
}
#Skapa VM ADD01
New-VM -Name $ADName -MemoryStartupBytes 3GB -VHDPath "$VHDXDestinationPath\$ADNAME\$ADNAME.vhdx" -Generation 2 -SwitchName $vSwitch
Set-VM -Name $ADName -AutomaticCheckpointsEnabled $false
Write-Verbose "VM Creation Completed. Starting VM [$ADName]" -Verbose
Start-VM -Name $ADName
#Vänta att VM ska komma igång
Write-Verbose “Waiting for PowerShell Direct to start on VM [$ADName]” -Verbose
while ((icm -VMName $ADName -Credential $DCLocalCredential {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}
Write-Verbose “[$ADName] is alive moving to IP config and change computername” -Verbose
#Konfigurera IP Adress till ADD01 och byter namn
Invoke-Command -vmname $ADName -Credential $DCLocalCredential -ScriptBlock {
param ($ADIP, $ADName)
new-NetIPAddress -InterfaceIndex (Get-NetAdapter).ifIndex -IPAddress $ADIP -PrefixLength 24 -DefaultGateway 192.168.0.1
Set-DnsClientServerAddress -InterfaceIndex (Get-NetAdapter).ifIndex -ServerAddresses ("8.8.8.8")
Rename-Computer -NewName $ADName -Force
} -ArgumentList $ADIP, $ADName
#Start om VM efter namnbytet
Write-Verbose "Rebooting VM [$ADName] for hostname change to take effect" -Verbose
Stop-VM -Name $ADName -Force
Start-VM -Name $ADName
Write-Verbose “Waiting for PowerShell Direct to start on VM [$ADName]” -Verbose
while ((icm -VMName $ADName -Credential $DCLocalCredential {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}
Write-Verbose "PowerShell Direct responding on VM [$ADName]. Moving On...." -Verbose
#Installera ADDS rollen
Invoke-Command -VMName $ADName -Credential $DCLocalCredential -ScriptBlock {
param ($ADName, $DomainMode, $ForestMode, $DomainName, $DSRMPWord)
Write-Verbose "Installing Active Directory Services on VM [$ADName]" -Verbose
Install-WindowsFeature -Name "AD-Domain-Services" -IncludeManagementTools
Write-Verbose "Configuring New Domain with Name [$DomainName] on VM [$ADName]" -Verbose
Install-ADDSForest -ForestMode $ForestMode -DomainMode $DomainMode -DomainName $DomainName `
-InstallDns -NoDNSonNetwork -SafeModeAdministratorPassword $DSRMPWord -Force -NoRebootOnCompletion
#Restart-Computer
} -ArgumentList $ADName, $DomainMode, $ForestMode, $DomainName, $DSRMPWord
Write-Verbose "Rebooting VM [$ADName] to complete installation of new AD Forest" -Verbose
Stop-VM -Name $ADName -Force
Start-VM -Name $ADName
Write-Verbose “Waiting for PowerShell Direct to start on VM [$ADName]” -Verbose
while ((icm -VMName $ADName -Credential $DomainCredential {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}
Write-Verbose "PowerShell Direct responding on VM [$ADName]. Moving On...." -Verbose
Write-Verbose "DC Provisioning Complete!!!!" -Verbose
######################################################
######################################################
###################Skapa Fil server###################
$FileServer = "FileS01"
$FileServerIP = "192.168.10.111"
#Lokal admin lösen används innan du server blir en AD Server
$FileLocalUser = "$FileServer\Administrator"
$FileLocalPWord = ConvertTo-SecureString -String "Sommar2020" -AsPlainText -Force
$FileLocalCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $FileLocalUser, $FileLocalPWord
#Kopiera VM template till lagringplats
Write-Verbose "Check if folder for VM exist"
$cpath = Test-Path "C:\VM\$($FileServer)"
if($cpath -eq $false){
Write-Verbose "Folder doest not exist, creating folder" -Verbose
New-item -Name $FileServer -Path "C:\VM\" -ItemType Directory | Out-Null
Write-Verbose "Folder is created, copy template VM" -Verbose
Copy-Item "$VHDXCurrentPath\MASTER.vhdx" "$VHDXDestinationPath\$FileServer\$FileServer.vhdx"
}else{
Write-Verbose "Folder already exist" -Verbose
Write-Verbose "copy template VM" -Verbose
Copy-Item "$VHDXCurrentPath\MASTER.vhdx" "$VHDXDestinationPath\$FileServer\$FileServer.vhdx"
}
#Skapa VM File01
New-VM -Name $FileServer -MemoryStartupBytes 1GB -VHDPath "$VHDXDestinationPath\$FileServer\$FileServer.vhdx" -Generation 2 -SwitchName $vSwitch
Set-VM -Name $FileServer -AutomaticCheckpointsEnabled $false
Write-Verbose "VM Creation Completed. Starting VM [$FileServer]" -Verbose
Start-VM -Name $FileServer
#Vänta att VM ska komma igång
Write-Verbose “Waiting for PowerShell Direct to start on VM [$FileServer]” -Verbose
while ((icm -VMName $FileServer -Credential $FileLocalCredential {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}
Write-Verbose “[$FileServer] is alive moving to IP config and change computername” -Verbose
#Konfigurera IP Adress till Filserver och byter namn
Invoke-Command -vmname $FileServer -Credential $FileLocalCredential -ScriptBlock {
$FileServer = "FileS01"
new-NetIPAddress -InterfaceIndex (Get-NetAdapter).ifIndex -IPAddress 192.168.10.111 -PrefixLength 24 -DefaultGateway 192.168.10.1
Set-DnsClientServerAddress -InterfaceIndex (Get-NetAdapter).ifIndex -ServerAddresses ("192.168.10.100")
#Rename-Computer -NewName $FileServer
}
Write-Verbose "Rebooting VM [$FileServer] to complete name change" -Verbose
Stop-VM -Name $FileServer -Force
Start-VM -Name $FileServer
Write-Verbose “Waiting for PowerShell Direct to start on VM [$FileServer]” -Verbose
while ((icm -VMName $FileServer -Credential $FileLocalCredential {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}
#Joina File01 till Domän
Invoke-Command -vmname $FileServer -Credential $FileLocalCredential -ScriptBlock {
param ($FileServer, $DomainName, $DomainCredential )
Add-Computer -ComputerName $FileServer -DomainName $Domainname -Credential $DomainCredential
} -ArgumentList $FileServer, $DomainName, $DomainCredential
Write-Verbose "Rebooting VM [$FileServer] after domain join" -Verbose
Stop-VM -Name $FileServer -Force
Start-VM -Name $FileServer
Write-Verbose “Waiting for PowerShell Direct to start on VM [$FileServer]” -Verbose
while ((icm -VMName $FileServer -Credential $FileLocalCredential {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}
#######################File01 server konfiguration slut#######################
Write-Verbose "Nästa steg är konfiguration av ADD01" -Verbose
##############Fortsättning av konfiguration av ADD01#########################
#Skapa OU och importera användarna
Write-Verbose "Påbörja konfiguration av användare" -Verbose
$ADuserCSV = Import-Csv -Delimiter "," -LiteralPath "C:\Temp\xstile users.csv"
$OUpath = 'ou=xstile,dc=xstile,dc=se'
$Password = "Sommar2020"
Invoke-Command -VMName $ADName -Credential $DomainCredential -ScriptBlock {
param ($ADuserCSV,$OUpath,$Password)
New-ADOrganizationalUnit -Name "xstile" -Path "dc=xstile,dc=se"
foreach ($user in $ADuserCSV){
New-ADUser -Name ($user.givenname + " " + $user.surname) `
-SamAccountName $user.SamAccountName `
-UserPrincipalName ($user.SamAccountName + "@mcampus.se") `
-GivenName $user.givenname `
-Surname $user.surname `
-DisplayName ($user.givenname + " " + $user.surname) `
-AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) `
-Enabled $true `
-Path $OUpath
}
} -ArgumentList $ADuserCSV, $OUpath, $Password
Write-Verbose "Användarna är konfigurerade" -Verbose
#Crete group
Write-Verbose "Påbörja konfiguration av grupper" -Verbose
Invoke-Command -VMName $ADName -Credential $DomainCredential -ScriptBlock {
$group = "SEC_Sales","SEC_IT","SEC_Scode","SEC_Ekonomi","SEC_IT","SEC_Ägare","SEC_App","SEC_Gemensam"
foreach ($i in $group){
New-ADGroup -name $i -path "ou=xstile,dc=xstile,dc=se" -groupscope Global -groupcategory Security
}
}
Write-Verbose "Grupperna är färdig konfigurerad" -Verbose
#Add user to group
Write-Verbose "Lägger till användarna i rätt grupp" -Verbose
Invoke-Command -VMName $ADName -Credential $DomainCredential -ScriptBlock{
$OUpath = "ou=xstile,dc=xstile,dc=se"
$SEC_Sales = "Victor.carave","David.laurin"
$SEC_IT = "Victor.carave","mats.nygren","tord.berner"
$SEC_Scode = "Victor.carave","mats.nygren"
$SEC_Ekonomi = "Victor.carave","mats.nygren"
$SEC_IT = "malin.helander","karol.sek"
$SEC_Ägare = "Victor.carave","tord.berner"
$SEC_App = "Victor.carave","mats.nygren"
$SEC_Gemensam = "Victor.carave","anders.andersson"
#lägger till Sales användare
foreach($user in $SEC_Sales){
add-ADgroupmember -identity "SEC_Sales" -members $user
}
#lägger till IT användare
foreach($user in $SEC_IT){
add-ADgroupmember -identity "SEC_IT" -members $user
}
#lägger till Scode användare
foreach($user in $SEC_Scode){
add-ADgroupmember -identity "SEC_Scode" -members $user
}
}
Write-Verbose "Användarna är har lagt till i grupperna" -Verbose
##############Konfiguration av ADD01 är klar#########################
##############Konfiguration av File01#########################
#Create folder
Write-Verbose "Skapa mappar" -Verbose
Invoke-Command -VMName $FileServer -Credential $DomainCredential -ScriptBlock{
$mapp = "Gemensam","Scode","Ekonomi","IT","Ägare","App"
foreach ($i in $mapp){
New-Item -Name $i -Path "C:\" -ItemType Directory
}
}
Write-Verbose "Mapparna är skapad" -Verbose
#Dela utmapparna och sätter behörigheter
Write-Verbose "Skapa nätverksmappar och sätter rättigheter" -Verbose
Invoke-Command -VMName $FileServer -Credential $DomainCredential -ScriptBlock{
New-SmbShare -Name "App" -Path "C:\App" -FullAccess "SEC_Sales"
New-SmbShare -Name "Ekonomi" -Path "C:\Ekonomi" -FullAccess "SEC_Ekonomi"
New-SmbShare -Name "Gemensam" -Path "C:\Gemensam" -FullAccess "SEC_Gemensam"
New-SmbShare -Name "IT" -Path "C:\IT" -FullAccess "SEC_IT"
New-SmbShare -Name "Scode" -Path "C:\Scode" -FullAccess "SEC_Scode"
New-SmbShare -Name "Ägare" -Path "C:\Ägare" -FullAccess "SEC_Ägare"
}
Write-Verbose "Mapparna utdelad och konfigurarad" -Verbose
##############Konfiguration av File01 är klar#########################