1+ $ScratchFolder = " "
2+ $WsusContent = " "
3+ $WDSRoot = " "
14function logger {
25 param (
36 $TextToLog
@@ -7,36 +10,6 @@ function logger{
710$ScriptLocation = Get-Location
811$ScriptLocation = $ScriptLocation.Path
912
10- if (! Test-Path $ScriptLocation \Images.json) {
11- logger - TextToLog " $ ( Get-Date ) ERROR: Cannot find Images.json in current folder ($ScriptLocation )\n"
12- exit
13- }
14- 15- $ConfigObject = Get-Content - Path $ScriptLocation \Images.json | ConvertFrom-Json
16- 17- if ($ConfigObject -eq $null ) {
18- logger - TextToLog " $ ( Get-Date ) ERROR: Unable to import config from file\n"
19- exit
20- }
21- 22- 23- # if (!Test-Path $ScriptLocation\DefaultUnattend.xml) {
24- # logger -TextToLog "$(Get-Date) ERROR: Cannot find DefaultUnattend.xml in current folder ($ScriptLocation)\n"
25- # exit
26- # }
27- 28- # if (!Test-Path $ScriptLocation\SpecGatherScript.vbs) {
29- # logger -TextToLog "$(Get-Date) ERROR: Cannot find SpecGatherScript.vbs in current folder ($ScriptLocation)\n"
30- # exit
31- # }
32- # $DefaultUnattend = Get-Content -Path $ScriptLocation\DefaultUnattend.xml
33- 34- # if ($DefaultUnattend -eq $null) {
35- # logger -TextToLog "$(Get-Date) ERROR: Unable to import DefaultUnattend from file\n"
36- # exit
37- # }
38- 39- 4013if (Test-Path $ScriptLocation \ImageUpdater1.log) {
4114 logger - TextToLog " $ ( Get-Date ) INFO: Removing ImageUpdater1.log\n"
4215 Remove-Item $ScriptLocation \ImageUpdater1.log
@@ -46,75 +19,6 @@ if (Test-Path $ScriptLocation\ImageUpdater.log) {
4619 Move-Item $ScriptLocation \ImageUpdater.log $ScriptLocation \ImageUpdater1.log
4720}
4821
49- # Make sure temp path exists
50- if ( (Test-Path - Path $ConfigObject.ScratchFolder ) -eq $false ) {
51- logger - TextToLog " $ ( Get-Date ) INFO: Temp Folder $ ( $ConfigObject.ScratchFolder ) doesn't exist, creating it."
52- $createPath = New-Item - Path $ConfigObject.ScratchFolder - ItemType directory
53- 54- if ( $createPath -eq $false ) {
55- logger - TextToLog " $ ( Get-Date ) ERROR: Failed to create scratch path $ ( $ConfigObject.ScratchFolder ) . Exiting."
56- exit
57- }
58- }
59- 60- 61- function Update-Images () {
62- Param (
63- [object ] $Config
64- )
65- foreach ($item in $Config.ImageDetails ) {
66- if ($item.ShouldBeDeleted ) {
67- Delete- ImageFromWDS - ImageFileName $item.FileName - ImageFilePath $item.Path
68- foreach ($DerivateImage in $item.DerivateImages ) {
69- Delete- ImageFromWDS - ImageFileName $DerivateImage.FileName - ImageFilePath $DerivateImage.Path
70- }
71- continue
72- }
73- logger - TextToLog " $ ( Get-Date ) INFO: Getting install image $ ( $item.FileName ) from WDS"
74- $Image = Get-ImageFromWDS - Config $Config - item $item
75- $UpdateResult = Update-WdsImage - Image $Image - Scratch $Config.ScratchFolder - WsusContent $Config.WsusContentFolderPath
76- if ($UpdateResult -eq $false ) {
77- logger - TextToLog " $ ( Get-Date ) ERROR: Error updating $ ( $item.Path ) "
78- continue
79- }
80- foreach ($DerivateImage in $item.DerivateImages ) {
81- if ($DerivateImage.ShouldBeDeleted ) {
82- Delete- ImageFromWDS - ImageFileName $DerivateImage.FileName - ImageFilePath $DerivateImage.Path
83- continue
84- }
85- }
86- }
87- }
88- 89- function Get-ImageFromWDS () {
90- Param (
91- [object ] $Config ,
92- [object ] $item
93- )
94- try {
95- $Image = Get-WdsInstallImage - FileName $item.FileName
96- }
97- catch {
98- logger - TextToLog $Error.ToString ()
99- if (Test-Path $item.Path ) {
100- try {
101- logger - TextToLog " $ ( Get-Date ) INFO: Attempting to add $ ( $item.FileName ) to WDS"
102- Import-WdsInstallImage - ImageGroup $Config.DefaultImageGroup - Path $item.Path - NewImageName $Config.DisplayName - Multicast - TransmissionName $Config.DisplayName + " MultiCast"
103- $Image = Get-WdsInstallImage - FileName $item.FileName
104- }
105- catch {
106- logger - TextToLog " $ ( Get-Date ) ERROR: Error adding $ ( $item.FileName ) to WDS"
107- logger - TextToLog $Error.ToString ()
108- }
109- }
110- # else if ($item.Path -eq "") {}
111- else {
112- logger - TextToLog " $ ( Get-Date ) ERROR: Couldn't find $ ( $item.Path ) "
113- }
114- }
115- return $Image
116- }
117- 11822 # Below came from https://github.com/breich/UpdateWdsFromWsus
11923function Update-WdsFromWsus () {
12024
@@ -124,17 +28,26 @@ function Update-WdsFromWsus() {
12428 )
12529
12630
31+ # Make sure temp path exists
32+ if ( (Test-Path - Path $ScratchFolder ) -eq $false ) {
33+ logger - TextToLog " $ ( Get-Date ) INFO: Temp Folder $ScratchFolder doesn't exist, creating it."
34+ $createPath = New-Item - Path $ScratchFolder - ItemType directory
12735
36+ if ( $createPath -eq $false ) {
37+ 38+ logger - TextToLog " $ ( Get-Date ) ERROR: Failed to create scratch path $ScratchFolder . Returning from the fuction."
39+ return $false
40+ }
41+ }
12842
12943 logger - TextToLog " $ ( Get-Date ) INFO: Getting list of install images from WDS"
13044 $Images = Get-WdsInstallImage
13145
13246 # Update each image.
13347 foreach ( $Image in $Images ) {
134- if ($Image.ImageName.Contains (" | OLD VERSION " )) {
135- continue
48+ if ($Image.ImageName.Contains (" Updatable " )) {
49+ Update-WdsImage - Image $Image - Scratch $ScratchFolder - WsusContent $WsusContent
13650 }
137- Update-WdsImage - Image $Image - Scratch $ScratchFolder - WsusContent $WsusContent
13851 }
13952}
14053
@@ -147,42 +60,39 @@ function Update-WdsImage() {
14760 $ScratchFolder ,
14861 $WsusContent
14962 )
150- 15163 $ExportDestination = " $ScratchFolder \" + $image.FileName
15264 $ImageName = $Image.ImageName
153- $ImageNameParts = $ImageName.Split (" |" )
154- $ImageName = $ImageNameParts [0 ].Trim() + " | (Updated " + (Get-Date ).ToShortDateString() + " via " + $MyInvocation.MyCommand.Name + " )"
155- $FileName = $Image.FileName
65+ $ExportFileName = $Image.FileName.split (" |" )[0 ] + " |" + (Get-Date ).ToFileTime()
15666 $ImageGroup = $Image.ImageGroup
157- $OldImageName = $Image.ImageName
15867 $Index = $Image.Index
68+ $Description = " Updated at " + (Get-Date ).ToShortDateString() + " via script"
15969
160- logger - TextToLog " $ ( Get-Date ) INFO: Updating " $OldImageName " (" $Image.FileName " )"
70+ logger - TextToLog " $ ( Get-Date ) INFO: Updating " $ImageName " (" $Image.FileName " )"
16171
162- logger - TextToLog " $ ( Get-Date ) INFO: .... Exporting $OldImageName to $ExportDestination "
163- $Export = Export-WdsInstallImage - Destination $ExportDestination - ImageName $OldImageName - FileName $FileName - ImageGroup $ImageGroup - NewImageName $ImageName - ErrorAction SilentlyContinue
72+ logger - TextToLog " $ ( Get-Date ) INFO: .... Exporting $ImageName to $ExportDestination "
73+ $Export = Export-WdsInstallImage - Destination $ExportDestination - ImageName $ImageName - FileName $ExportFileName - ImageGroup $ImageGroup - NewImageName $ImageName - ErrorAction SilentlyContinue
16474
16575
16676 # Verify that mounting succeeded.
16777 if ( $export -eq $null ) {
16878
169- logger - TextToLog " $ ( Get-Date ) ERROR: .... Exporting $OldImageName to $exportDestination failed. Quitting."
79+ logger - TextToLog " $ ( Get-Date ) ERROR: .... Exporting $ImageName to $exportDestination failed. Quitting."
17080 return $false ;
17181 }
172- 82+ Copy-Item $ExportDestination " $ScratchFolder \ " + $image .FileName.Replace ( " Updatable " , " NEWOLD " )
17383 # Create Mount path.
174- $MountPath = " $ScratchFolder \$OldImageName "
84+ $MountPath = " $ScratchFolder \$ImageName "
17585 if ( ( Test-Path - Path $MountPath ) -eq $false ) {
17686 logger - TextToLog " $ ( Get-Date ) INFO: .... Mount Folder $MountPath doesn't exist, creating it."
17787 $crap = New-Item - Path $MountPath - ItemType directory
17888
17989 }
180- logger - TextToLog " $ ( Get-Date ) INFO: .... Mounting $OldImageName to $MountPath . Please be patient."
90+ logger - TextToLog " $ ( Get-Date ) INFO: .... Mounting $ImageName to $MountPath . Please be patient."
18191 $mount = Mount-WindowsImage - ImagePath $exportDestination - Path $MountPath - Index $Index - CheckIntegrity - ErrorAction SilentlyContinue
18292
18393 # Verify Mount.
18494 if ( $mount -eq $null ) {
185- logger - TextToLog " $ ( Get-Date ) ERROR: .... Failed to mount $OldImageName to $MountPath . Quitting."
95+ logger - TextToLog " $ ( Get-Date ) ERROR: .... Failed to mount $ImageName to $MountPath . Quitting."
18696 return $false
18797 }
18898
@@ -195,28 +105,37 @@ function Update-WdsImage() {
195105 }
196106
197107 # Dismount
198- logger - TextToLog " $ ( Get-Date ) INFO: .... Dismounting and saving $OldImageName ."
108+ logger - TextToLog " $ ( Get-Date ) INFO: .... Dismounting and saving $ImageName ."
199109 $dismount = Dismount-WindowsImage - Path $MountPath - Save - ErrorAction SilentlyContinue
200110
201111 if ( $dismount -eq $null ) {
202112
203- logger - TextToLog " $ ( Get-Date ) ERROR: Failed to dismount and save changes to $OldImageName . Quitting."
113+ logger - TextToLog " $ ( Get-Date ) ERROR: Failed to dismount and save changes to $ImageName . Quitting."
204114 return $false
205115 }
206116
207117 # Delete Mount Path
208118 logger - TextToLog " $ ( Get-Date ) INFO: .... Deleting mount path $MountPath "
209119 $deleteMountPath = Remove-Item - Path $MountPath
210120
121+ $OldImage = Get-WdsInstallImage - ImageGroup $Image.ImageGroup - ImageName $ImageName.Replace (" Updatable" , " OLD" )
122+ if ($OldImage -ne $null ) {
123+ Remove-WdsInstallImage - ImageGroup $Image.ImageGroup - ImageName $ImageName.Replace (" Updatable" , " OLD" )
124+ Delete- Item " $WDSRoot \$ ( $OldImage.FileName ) "
125+ }
126+ Remove-WdsInstallImage - ImageGroup $Image.ImageGroup - ImageName $ImageName
127+ Delete- Item " $WDSRoot \$ ( $image.FileName ) "
128+ 129+ Copy-Item " $ScratchFolder \" + $image.FileName.Replace (" Updatable" , " NEWOLD" ) " $ScratchFolder \" + $image.FileName.Replace (" Updatable" , " OLD" )
130+ 211131 logger - TextToLog " $ ( Get-Date ) INFO: .... Importing image to WDS"
212- 213132 # The Import needs to be called differently depending on whether or not there's an UnattendFile.
214133 # If Import-WdsInstallImage is called with an empty or null UnattendFile, import fails.
215134 if ( $Image.UnattendFile -eq $null -or $Image.UnattendFile -eq " " ) {
216- logger - TextToLog " $ ( Get-Date ) INFO: Import-WdsInstallImage -ImageGroup $Image .ImageGroup -Path $ExportDestination -ImageName -DisplayOrder 0 - NewImageName $ImageName "
217- $import = Import-WdsInstallImage - ImageGroup $Image.ImageGroup - Path $ExportDestination - DisplayOrder 0 - NewImageName $ImageName - Multicast - TransmissionName $ImageName + " MultiCast "
135+ logger - TextToLog " $ ( Get-Date ) INFO: Import-WdsInstallImage -ImageGroup $Image .ImageGroup -Path $ExportDestination -ImageName -NewImageName $ImageName "
136+ $import = Import-WdsInstallImage - ImageGroup $Image.ImageGroup - Path $ExportDestination - NewImageName $ImageName
218137 } else {
219- $import = Import-WdsInstallImage - ImageGroup $Image.ImageGroup - UnattendFile $Image.UnattendFile - Path $ExportDestination - ImageName $OldImageName - DisplayOrder 0 - NewImageName $ImageName - Multicast - TransmissionName $ImageName + " MultiCast "
138+ $import = Import-WdsInstallImage - ImageGroup $Image.ImageGroup - UnattendFile $Image.UnattendFile - Path $ExportDestination - ImageName $ImageName - NewImageName $ImageName
220139 }
221140
222141 if ( $import -eq $null ) {
0 commit comments