forked from DeploymentBunny/ImageFactoryV3ForHyper-V
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageFactoryV3-Build.ps1
605 lines (532 loc) · 22.9 KB
/
ImageFactoryV3-Build.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<#
.Synopsis
ImageFactory 3.2
.DESCRIPTION
ImageFactory 3.2
.EXAMPLE
ImageFactoryV3-Build.ps1
.NOTES
Created: 2016-11-24
Version: 3.1
Updated: 2017-02-23
Version: 3.2
Author - Mikael Nystrom
Twitter: @mikael_nystrom
Blog : http://deploymentbunny.com
Disclaimer:
This script is provided 'AS IS' with no warranties, confers no rights and
is not supported by the author.
This script uses the PsIni module:
Blog : http://oliver.lipkau.net/blog/
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
.LINK
http://www.deploymentbunny.com
#>
[cmdletbinding(SupportsShouldProcess=$True)]
Param(
[parameter(mandatory=$false)]
[ValidateSet($True,$False)]
$UpdateBootImage = $False,
[parameter(mandatory=$false)]
[ValidateSet($True,$False)]
$EnableMDTMonitoring = $False,
[parameter(mandatory=$false)]
[ValidateSet($True,$False)]
$TestMode = $False
)
#Set start time
$StartTime = Get-Date
Function Get-VIARefTaskSequence
{
Param(
$RefTaskSequenceFolder
)
$RefTaskSequences = Get-ChildItem $RefTaskSequenceFolder
Foreach($RefTaskSequence in $RefTaskSequences){
New-Object PSObject -Property @{
TaskSequenceID = $RefTaskSequence.ID
Name = $RefTaskSequence.Name
Comments = $RefTaskSequence.Comments
Version = $RefTaskSequence.Version
Enabled = $RefTaskSequence.enable
LastModified = $RefTaskSequence.LastModifiedTime
}
}
}
Function Test-VIAHypervConnection
{
Param(
$Computername,
$ISOFolder,
$VMFolder,
$VMSwitchName
)
#Verify SMB access
$Result = Test-NetConnection -ComputerName $Computername -CommonTCPPort SMB
If ($Result.TcpTestSucceeded -eq $true){Write-Verbose "SMB Connection to $Computername is ok"}else{Write-Warning "SMB Connection to $Computername is NOT ok";Return $False}
#Verify WinRM access
$Result = Test-NetConnection -ComputerName $Computername -CommonTCPPort WINRM
If ($Result.TcpTestSucceeded -eq $true){Write-Verbose "WINRM Connection to $Computername is ok"}else{Write-Warning "WINRM Connection to $Computername is NOT ok";Return $False}
#Verify that Microsoft-Hyper-V-Management-PowerShell is installed
Invoke-Command -ComputerName $Computername -ScriptBlock {
$Result = (Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell)
Write-Verbose "$($Result.DisplayName) is $($Result.State)"
If($($Result.State) -ne "Enabled"){Write-Warning "$($Result.DisplayName) is not Enabled";Return $False}
}
#Verify that Microsoft-Hyper-V-Management-PowerShell is installed
Invoke-Command -ComputerName $Computername -ScriptBlock {
$Result = (Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V)
If($($Result.State) -ne "Enabled"){Write-Warning "$($Result.DisplayName) is not Enabled";Return $False}
}
#Verify that Hyper-V is running
Invoke-Command -ComputerName $Computername -ScriptBlock {
$Result = (Get-Service -Name vmms)
Write-Verbose "$($Result.DisplayName) is $($Result.Status)"
If($($Result.Status) -ne "Running"){Write-Warning "$($Result.DisplayName) is not Running";Return $False}
}
#Verify that the ISO Folder is created
Invoke-Command -ComputerName $Computername -ScriptBlock {
Param(
$ISOFolder
)
$result = New-Item -Path $ISOFolder -ItemType Directory -Force
} -ArgumentList $ISOFolder
#Verify that the VM Folder is created
Invoke-Command -ComputerName $Computername -ScriptBlock {
Param(
$VMFolder
)
$result = New-Item -Path $VMFolder -ItemType Directory -Force
} -ArgumentList $VMFolder
#Verify that the VMSwitch exists
Invoke-Command -ComputerName $Computername -ScriptBlock {
Param(
$VMSwitchName
)
if(((Get-VMSwitch | Where-Object -Property Name -EQ -Value $VMSwitchName).count) -eq "1"){Write-Verbose "Found $VMSwitchName"}else{Write-Warning "No switch with the name $VMSwitchName found";Return $False}
} -ArgumentList $VMSwitchName
Return $True
}
Function Update-Log
{
Param(
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0
)]
[string]$Data,
[Parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0
)]
[string]$Solution = $Solution,
[Parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=1
)]
[validateset('Information','Warning','Error')]
[string]$Class = "Information"
)
$LogString = "$Solution, $Data, $Class, $(Get-Date)"
$HostString = "$Solution, $Data, $(Get-Date)"
Add-Content -Path $Log -Value $LogString
switch ($Class)
{
'Information'{
Write-Host $HostString -ForegroundColor Gray
}
'Warning'{
Write-Host $HostString -ForegroundColor Yellow
}
'Error'{
Write-Host $HostString -ForegroundColor Red
}
Default {}
}
}
#Inititial Settings
Clear-Host
$Log = "C:\Setup\ImageFactoryV3ForHyper-V\log.txt"
$XMLFile = "C:\setup\ImageFactoryV3ForHyper-V\ImageFactoryV3.xml"
$Solution = "IMF32"
Update-Log -Data "Imagefactory 3.2 (Hyper-V)"
Update-Log -Data "Logfile is $Log"
Update-Log -Data "XMLfile is $XMLfile"
if($TestMode -eq $True){
Update-Log -Data "Testmode is now $TestMode"
}
#Importing modules
Update-Log -Data "Importing modules"
Import-Module 'C:\Program Files\Microsoft Deployment Toolkit\Bin\MicrosoftDeploymentToolkit.psd1' -ErrorAction Stop -WarningAction Stop
Import-Module C:\Setup\PsIni\PsIni.psm1 -ErrorAction Stop -WarningAction Stop
#Read Settings from XML
Update-Log -Data "Reading from $XMLFile"
[xml]$Settings = Get-Content $XMLFile -ErrorAction Stop -WarningAction Stop
#Verify Connection to DeploymentRoot
Update-Log -Data "Verify Connection to DeploymentRoot"
$Result = Test-Path -Path $Settings.Settings.MDT.DeploymentShare
If($Result -ne $true){Update-Log -Data "Cannot access $($Settings.Settings.MDT.DeploymentShare) , will break";break}
#Connect to MDT
Update-Log -Data "Connect to MDT"
$Root = $Settings.Settings.MDT.DeploymentShare
if((Test-Path -Path MDT:) -eq $false){
$MDTPSDrive = New-PSDrive -Name MDT -PSProvider MDTProvider -Root $Root -ErrorAction Stop
Update-Log -Data "Connected to $($MDTPSDrive.Root)"
}
#Get MDT Settings
Update-Log -Data "Get MDT Settings"
$MDTSettings = Get-ItemProperty MDT:
#Check if we should update the boot image
Update-Log -Data "Check if we should update the boot image"
If($UpdateBootImage -eq $True){
#Update boot image
Update-Log -Data "Updating boot image, please wait"
Update-MDTDeploymentShare -Path MDT: -ErrorAction Stop
}
#Check if we should use MDTmonitoring
Update-Log -Data "Check if we should use MDTmonitoring"
If($EnableMDTMonitoring -eq $True){
Update-Log -Data "Using MDT monitoring"
$MDTServer = $env:COMPUTERNAME
}
#Verify access to boot image
Update-Log -Data "Verify access to boot image"
$MDTImage = $($Settings.Settings.MDT.DeploymentShare) + "\boot\" + $($MDTSettings.'Boot.x86.LiteTouchISOName')
if((Test-Path -Path $MDTImage) -eq $true){Update-Log -Data "Access to $MDTImage is ok"}else{Write-Warning "Could not access $MDTImage";BREAK}
#Get TaskSequences
Update-Log -Data "Get TaskSequences"
$RefTaskSequenceIDs = (Get-VIARefTaskSequence -RefTaskSequenceFolder "MDT:\Task Sequences\$($Settings.Settings.MDT.RefTaskSequenceFolderName)" | where Enabled -EQ $true).TasksequenceID
if($RefTaskSequenceIDs.count -eq 0){
Update-Log -Data "Sorry, could not find any TaskSequences to work with"
BREAK
}
Update-Log -Data "Found $($RefTaskSequenceIDs.count) TaskSequences to work on"
#Get detailed info
Update-Log -Data "Get detailed info about the task sequences"
$Result = Get-VIARefTaskSequence -RefTaskSequenceFolder "MDT:\Task Sequences\$($Settings.Settings.MDT.RefTaskSequenceFolderName)" | where Enabled -EQ $true
foreach($obj in ($Result | Select-Object TaskSequenceID,Name,Version)){
$data = "$($obj.TaskSequenceID) $($obj.Name) $($obj.Version)"
Update-Log -Data $data
}
#Verify Connection to Hyper-V host
Update-Log -Data "Verify Connection to Hyper-V host"
$Result = Test-VIAHypervConnection -Computername $Settings.Settings.HyperV.Computername -ISOFolder $Settings.Settings.HyperV.ISOLocation -VMFolder $Settings.Settings.HyperV.VMLocation -VMSwitchName $Settings.Settings.HyperV.SwitchName
If($Result -ne $true){Update-Log -Data "$($Settings.Settings.HyperV.Computername) is not ready, will break";break}
#Upload boot image to Hyper-V host
Update-Log -Data "Upload boot image to Hyper-V host"
$DestinationFolder = "\\" + $($Settings.Settings.HyperV.Computername) + "\" + $($Settings.Settings.HyperV.ISOLocation -replace ":","$")
Copy-Item -Path $MDTImage -Destination $DestinationFolder -Force
#Remove old WIM files in the capture folder
Update-Log -Data "Remove old WIM files in the capture folder"
Foreach($Ref in $RefTaskSequenceIDs){
$FullRefPath = $(("$Root\Captures\$ref") + ".wim")
if((Test-Path -Path $FullRefPath) -eq $true){
Remove-Item -Path $FullRefPath -Force -ErrorAction Stop
}
}
#Create the VM's on Host
Update-Log -Data "Create the VM's on Host"
Foreach($Ref in $RefTaskSequenceIDs){
$VMName = $ref
$VMMemory = [int]$($Settings.Settings.HyperV.StartUpRAM) * 1GB
$VMPath = $($Settings.Settings.HyperV.VMLocation)
$VMBootimage = $($Settings.Settings.HyperV.ISOLocation) + "\" + $($MDTImage | Split-Path -Leaf)
$VMVHDSize = [int]$($Settings.Settings.HyperV.VHDSize) * 1GB
$VMVlanID = $($Settings.Settings.HyperV.VLANID)
$VMVCPU = $($Settings.Settings.HyperV.NoCPU)
$VMSwitch = $($Settings.Settings.HyperV.SwitchName)
Invoke-Command -ComputerName $($Settings.Settings.HyperV.Computername) -ScriptBlock {
Param(
$VMName,
$VMMemory,
$VMPath,
$VMBootimage,
$VMVHDSize,
$VMVlanID,
$VMVCPU,
$VMSwitch
)
Write-Verbose "Hyper-V host is $env:COMPUTERNAME"
Write-Verbose "Working on $VMName"
#Check if VM exist
if(!((Get-VM | Where-Object -Property Name -EQ -Value $VMName).count -eq 0)){Write-Warning -Message "VM exist";Break}
#Create VM
$VM = New-VM -Name $VMName -MemoryStartupBytes $VMMemory -Path $VMPath -NoVHD -Generation 1
Write-Verbose "$($VM.Name) is created"
#Disable dynamic memory
Set-VMMemory -VM $VM -DynamicMemoryEnabled $false
Write-Verbose "Dynamic memory is disabled on $($VM.Name)"
#Connect to VMSwitch
Connect-VMNetworkAdapter -VMNetworkAdapter (Get-VMNetworkAdapter -VM $VM) -SwitchName $VMSwitch
Write-Verbose "$($VM.Name) is connected to $VMSwitch"
#Set vCPU
if($VMVCPU -ne "1"){
$Result = Set-VMProcessor -Count $VMVCPU -VM $VM -Passthru
Write-Verbose "$($VM.Name) has $($Result.count) vCPU"
}
#Set VLAN
If($VMVlanID -ne "0"){
$Result = Set-VMNetworkAdapterVlan -VlanId $VMVlanID -Access -VM $VM -Passthru
Write-Verbose "$($VM.Name) is configured for VLANid $($Result.NativeVlanId)"
}
#Create empty disk
$VHD = $VMName + ".vhdx"
$result = New-VHD -Path "$VMPath\$VMName\Virtual Hard Disks\$VHD" -SizeBytes $VMVHDSize -Dynamic -ErrorAction Stop
Write-Verbose "$($result.Path) is created for $($VM.Name)"
#Add VHDx
$result = Add-VMHardDiskDrive -VMName $VMName -Path "$VMPath\$VMName\Virtual Hard Disks\$VHD" -Passthru
Write-Verbose "$($result.Path) is attached to $VMName"
#Connect ISO
$result = Set-VMDvdDrive -VMName $VMName -Path $VMBootimage -Passthru
Write-Verbose "$($result.Path) is attached to $VMName"
#Set Notes
Set-VM -VMName $VMName -Notes "REFIMAGE"
} -ArgumentList $VMName,$VMMemory,$VMPath,$VMBootimage,$VMVHDSize,$VMVlanID,$VMVCPU,$VMSwitch
}
#Get BIOS Serialnumber from each VM and update the customsettings.ini file
Update-Log -Data "Get BIOS Serialnumber from each VM and update the customsettings.ini file"
$BIOSSerialNumbers = @{}
Foreach($Ref in $RefTaskSequenceIDs){
#Get BIOS Serailnumber from the VM
$BIOSSerialNumber = Invoke-Command -ComputerName $($Settings.Settings.HyperV.Computername) -ScriptBlock {
Param(
$VMName
)
$VMObject = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName = '$VMName'"
$VMObject.GetRelated('Msvm_VirtualSystemSettingData').BIOSSerialNumber
} -ArgumentList $Ref
#Store serialnumber for the cleanup process
$BIOSSerialNumbers.Add("$Ref","$BIOSSerialNumber")
#Update CustomSettings.ini
$IniFile = "$($Settings.settings.MDT.DeploymentShare)\Control\CustomSettings.ini"
$CustomSettings = Get-IniContent -FilePath $IniFile -CommentChar ";"
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"OSDComputerName"="$Ref"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"TaskSequenceID"="$Ref"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"BackupFile"="$Ref.wim"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"SkipTaskSequence"="YES"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"SkipApplications"="YES"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"SkipCapture"="YES"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
if($TestMode -eq $True){
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"DoCapture"="NO"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
}
else{
$CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections "$BIOSSerialNumber" -NameValuePairs @{"DoCapture"="YES"}
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
}
}
#Start VM's on Host
Update-Log -Data "Start VM's on Host"
Update-Log -Data "ConcurrentRunningVMs is set to: $($Settings.Settings.ConcurrentRunningVMs)"
Invoke-Command -ComputerName $($Settings.Settings.HyperV.Computername) -ScriptBlock {
Param(
$ConcurrentRunningVMs,
$MDTServer = "",
$EnableMDTMonitoring
)
#Import Function
Function Get-MDTOData{
<#
.Synopsis
Function for getting MDTOdata
.DESCRIPTION
Function for getting MDTOdata
.EXAMPLE
Get-MDTOData -MDTMonitorServer MDTSERVER01
.NOTES
Created: 2016-03-07
Version: 1.0
Author - Mikael Nystrom
Twitter: @mikael_nystrom
Blog : http://deploymentbunny.com
.LINK
http://www.deploymentbunny.com
#>
Param(
$MDTMonitorServer
)
$URL = "http://" + $MDTMonitorServer + ":9801/MDTMonitorData/Computers"
$Data = Invoke-RestMethod $URL
foreach($property in ($Data.content.properties) ){
$Hash = [ordered]@{
Name = $($property.Name);
PercentComplete = $($property.PercentComplete.#text);
Warnings = $($property.Warnings.#text);
Errors = $($property.Errors.#text);
DeploymentStatus = $(
Switch($property.DeploymentStatus.#text){
1 { "Active/Running"}
2 { "Failed"}
3 { "Successfully completed"}
Default {"Unknown"}
}
);
StepName = $($property.StepName);
TotalSteps = $($property.TotalStepS.'#text')
CurrentStep = $($property.CurrentStep.'#text')
DartIP = $($property.DartIP);
DartPort = $($property.DartPort);
DartTicket = $($property.DartTicket);
VMHost = $($property.VMHost.'#text');
VMName = $($property.VMName.'#text');
LastTime = $($property.LastTime.'#text') -replace "T"," ";
StartTime = $($property.StartTime.#text) -replace "T"," ";
EndTime = $($property.EndTime.#text) -replace "T"," ";
}
New-Object PSObject -Property $Hash
}
}
#Get the VMs as Objects
$RefVMs = Get-VM | Where-Object -Property Notes -Like -Value "REFIMAGE"
foreach($RefVM in $RefVMs){
Write-Verbose "REFVM $($RefVM.Name) is deployed on $($RefVM.ComputerName) at $($refvm.ConfigurationLocation)"
}
#Get the VMs as Objects
$RefVMs = Get-VM | Where-Object -Property Notes -Like -Value "REFIMAGE"
foreach($RefVM in $RefVMs){
$StartedVM = Start-VM -VMName $RefVM.Name
Write-Verbose "Starting $($StartedVM.name)"
Do
{
$RunningVMs = $((Get-VM | Where-Object -Property Notes -EQ -Value "REFIMAGE" | Where-Object -Property State -EQ -Value Running))
foreach($RunningVM in $RunningVMs){
if($EnableMDTMonitoring -eq $false){
Write-Output "Currently running VM's : $($RunningVMs.Name) at $(Get-Date)"
}
else{
Get-MDTOData -MDTMonitorServer $MDTServer | Where-Object -Property Name -EQ -Value $RunningVM.Name | Select-Object Name,PercentComplete,Warnings,Errors,DeploymentStatus,StartTime,Lasttime | FT
}
}
Start-Sleep -Seconds "30"
}
While((Get-VM | Where-Object -Property Notes -EQ -Value "REFIMAGE" | Where-Object -Property State -EQ -Value Running).Count -gt ($ConcurrentRunningVMs - 1))
}
} -ArgumentList $($Settings.Settings.ConcurrentRunningVMs),$env:COMPUTERNAME,$EnableMDTMonitoring
#Wait until they are done
Update-Log -Data "Wait until they are done"
Invoke-Command -ComputerName $($Settings.Settings.HyperV.Computername) -ScriptBlock {
Param(
$MDTServer = "",
$EnableMDTMonitoring
)
#Import Function
Function Get-MDTOData{
<#
.Synopsis
Function for getting MDTOdata
.DESCRIPTION
Function for getting MDTOdata
.EXAMPLE
Get-MDTOData -MDTMonitorServer MDTSERVER01
.NOTES
Created: 2016-03-07
Version: 1.0
Author - Mikael Nystrom
Twitter: @mikael_nystrom
Blog : http://deploymentbunny.com
.LINK
http://www.deploymentbunny.com
#>
Param(
$MDTMonitorServer
)
$URL = "http://" + $MDTMonitorServer + ":9801/MDTMonitorData/Computers"
$Data = Invoke-RestMethod $URL
foreach($property in ($Data.content.properties) ){
$Hash = [ordered]@{
Name = $($property.Name);
PercentComplete = $($property.PercentComplete.#text);
Warnings = $($property.Warnings.#text);
Errors = $($property.Errors.#text);
DeploymentStatus = $(
Switch($property.DeploymentStatus.#text){
1 { "Active/Running"}
2 { "Failed"}
3 { "Successfully completed"}
Default {"Unknown"}
}
);
StepName = $($property.StepName);
TotalSteps = $($property.TotalStepS.'#text')
CurrentStep = $($property.CurrentStep.'#text')
DartIP = $($property.DartIP);
DartPort = $($property.DartPort);
DartTicket = $($property.DartTicket);
VMHost = $($property.VMHost.'#text');
VMName = $($property.VMName.'#text');
LastTime = $($property.LastTime.'#text') -replace "T"," ";
StartTime = $($property.StartTime.#text) -replace "T"," ";
EndTime = $($property.EndTime.#text) -replace "T"," ";
}
New-Object PSObject -Property $Hash
}
}
Do{
$RunningVMs = $((Get-VM | Where-Object -Property Notes -EQ -Value "REFIMAGE" | Where-Object -Property State -EQ -Value Running))
foreach($RunningVM in $RunningVMs){
if($EnableMDTMonitoring -eq $false){
Write-Output "Currently running VM's : $($RunningVMs.Name) at $(Get-Date)"
}
else{
Get-MDTOData -MDTMonitorServer $MDTServer | Where-Object -Property Name -EQ -Value $RunningVM.Name | Select-Object Name,PercentComplete,Warnings,Errors,DeploymentStatus,StartTime,Lasttime | FT
}
}
Start-Sleep -Seconds "30"
}until((Get-VM | Where-Object -Property Notes -EQ -Value "REFIMAGE" | Where-Object -Property State -EQ -Value Running).count -eq '0')
} -ArgumentList $MDTServer,$EnableMDTMonitoring
if($TestMode -eq $True){
#Read-Host -Prompt "Press any key when testing is done, the VM's will be deleted..."
}
#Cleanup VMs
Update-Log -Data "Cleanup VMs"
Invoke-Command -ComputerName $($Settings.Settings.HyperV.Computername) -ScriptBlock {
$RefVMs = Get-VM | Where-Object -Property Notes -EQ -Value "REFIMAGE"
Foreach($RefVM in $RefVMs){
$VM = Get-VM -VMName $RefVM.Name
Write-Verbose "Deleting $($VM.Name) on $($VM.Computername) at $($VM.ConfigurationLocation)"
Remove-VM -VM $VM -Force
Remove-Item -Path $VM.ConfigurationLocation -Recurse -Force
}
}
#Update CustomSettings.ini
Update-Log -Data "Update CustomSettings.ini"
Foreach($Obj in $BIOSSerialNumbers.Values){
$CSIniUpdate = Remove-IniEntry -FilePath $IniFile -Sections $Obj
Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
}
#Cleanup MDT Monitoring data
Update-Log -Data "Cleanup MDT Monitoring data"
if($EnableMDTMonitoring -eq $True){
foreach($RefTaskSequenceID in $RefTaskSequenceIDs){
Get-MDTMonitorData -Path MDT: | Where-Object -Property Name -EQ -Value $RefTaskSequenceID | Remove-MDTMonitorData -Path MDT:
}
}
if(!($TestMode -eq $True)){
#Show the WIMfiles:
Update-Log -Data "Show the WIM's"
Foreach($Ref in $RefTaskSequenceIDs){
$FullRefPath = $(("$Root\Captures\$ref") + ".wim")
if((Test-Path -Path $FullRefPath) -eq $true){
$Item = Get-Item -Path $FullRefPath
Update-Log -Data "WIM: $($Item.FullName)"
}else{
Update-Log -Data "Could not find $FullRefPath, something went wrong, sorry" -Class Warning
}
}
}
#Final update
$Endtime = Get-Date
Update-Log -Data "The script took $(($Endtime - $StartTime).Days):Days $(($Endtime - $StartTime).Hours):Hours $(($Endtime - $StartTime).Minutes):Minutes to complete."