Public/New-PEBuildTask.ps1
<# .SYNOPSIS Creates a JSON Task for use with New-PEBuild .DESCRIPTION Creates a JSON Task for use with New-PEBuild .LINK https://www.osdeploy.com/osbuilder/docs/functions/pebuild/new-pebuildtask .PARAMETER SourceWim Wim to use for the PEBuild .PARAMETER TaskName Name of the Task to create .PARAMETER DeploymentShare MDT DeployRoot Full Path .PARAMETER AutoExtraFiles Add Auto ExtraFiles to WinPE .PARAMETER ScratchSpace Set the Scratch Space for WinPE #> function New-PEBuildTask { [CmdletBinding(DefaultParameterSetName='Recovery')] PARAM ( [Parameter(Mandatory,ParameterSetName='WinPE')] [Parameter(Mandatory,ParameterSetName='MDT')] [ValidateSet('WinRE','WinPE')] [string]$SourceWim, [Parameter(Mandatory)] [string]$TaskName, [Parameter(Mandatory,ParameterSetName='MDT')] [string]$DeploymentShare, [switch]$AutoExtraFiles, [ValidateSet('64','128','256','512')] [string]$ScratchSpace = '128' ) BEGIN { #Write-Host '========================================================================================' -ForegroundColor DarkGray #Write-Host "$($MyInvocation.MyCommand.Name) BEGIN" -ForegroundColor Green #=================================================================================================== Write-Verbose '19.1.1 Initialize OSBuilder' #=================================================================================================== Get-OSBuilder -CreatePaths -HideDetails } PROCESS { Write-Host '========================================================================================' -ForegroundColor DarkGray Write-Host "$($MyInvocation.MyCommand.Name) PROCESS" -ForegroundColor Green #=================================================================================================== Write-Verbose '19.1.1 Information' #=================================================================================================== $PEOutput = $($PsCmdlet.ParameterSetName) if ($PEOutput -eq 'Recovery') {$SourceWim = 'WinRE'} $TaskName = "$PEOutput $TaskName" $TaskPath = "$OSBuilderTasks\$TaskName.json" Write-Host '========================================================================================' -ForegroundColor DarkGray Write-Host "New-PEBuild Task Settings" -ForegroundColor Green Write-Host "-Task Name: $TaskName" Write-Host "-Task Path: $TaskPath" Write-Host "-PEOutput: $PEOutput" Write-Host "-Wim File: $SourceWim" Write-Host "-Deployment Share: $DeploymentShare" Write-Host "-Scratch Space: $ScratchSpace" #=================================================================================================== Write-Verbose '19.1.1 Validate Task' #=================================================================================================== if (Test-Path $TaskPath) { Write-Host '========================================================================================' -ForegroundColor DarkGray Write-Warning "Task already exists at $TaskPath" Write-Warning "Content will be overwritten!" } #=================================================================================================== Write-Verbose '19.1.1 Get-OSMedia' #=================================================================================================== $OSMedia = @() $OSMedia = Get-OSMedia | Where-Object {$_.MajorVersion -eq 10} if ($TaskName -like "*x64*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*x64*"}} if ($TaskName -like "*x86*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*x86*"}} if ($TaskName -like "*1511*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*1511*"}} if ($TaskName -like "*1607*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*1607*"}} if ($TaskName -like "*1703*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*1703*"}} if ($TaskName -like "*1709*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*1709*"}} if ($TaskName -like "*1803*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*1803*"}} if ($TaskName -like "*1809*") {$OSMedia = $OSMedia | Where-Object {$_.Name -like "*1809*"}} $OSMedia = $OSMedia | Out-GridView -OutputMode Single -Title "Select a Source OSMedia to use for this Task (Cancel to Exit)" if($null -eq $OSMedia) { Write-Warning "Source OSMedia was not selected . . . Exiting!" Return } #=================================================================================================== # OSBuildTask / PEBuildTask Write-Verbose '19.1.1 Set Proper Paths' #=================================================================================================== $OSSourcePath = $($OSMedia.FullName) $OSImagePath = "$OSSourcePath\OS\sources\install.wim" #=================================================================================================== Write-Verbose '19.1.1 Get Windows Image Information' #=================================================================================================== $OSImageIndex = 1 $WindowsImage = Get-WindowsImage -ImagePath "$OSImagePath" -Index $OSImageIndex | Select-Object -Property * $OSImageName = $($WindowsImage.ImageName) <# $OSImageName = $OSImageName -replace "Windows 7", "Win7" $OSImageName = $OSImageName -replace "Windows 10", "Win10" $OSImageName = $OSImageName -replace "Enterprise", "Ent" $OSImageName = $OSImageName -replace "Education", "Edu" $OSImageName = $OSImageName -replace "Virtual Desktops", "VD" $OSImageName = $OSImageName -replace " for ", " " $OSImageName = $OSImageName -replace "Workstations", "Wks" $OSImageName = $OSImageName -replace "Windows Server 2016", "Svr2016" $OSImageName = $OSImageName -replace "Windows Server 2019", "Svr2019" $OSImageName = $OSImageName -replace "ServerStandardACore", "Std Core" $OSImageName = $OSImageName -replace "ServerDatacenterACore", "DC Core" $OSImageName = $OSImageName -replace "ServerStandardCore", "Std Core" $OSImageName = $OSImageName -replace "ServerDatacenterCore", "DC Core" $OSImageName = $OSImageName -replace "ServerStandard", "Std" $OSImageName = $OSImageName -replace "ServerDatacenter", "DC" $OSImageName = $OSImageName -replace "Standard", "Std" $OSImageName = $OSImageName -replace "Datacenter", "DC" $OSImageName = $OSImageName -replace 'Desktop Experience', 'DTE' $OSImageName = $OSImageName -replace '\(', '' $OSImageName = $OSImageName -replace '\)', '' #> $OSImageDescription = $($WindowsImage.ImageDescription) $OSArchitecture = $($WindowsImage.Architecture) if ($OSArchitecture -eq '0') {$OSArchitecture = 'x86'} if ($OSArchitecture -eq '6') {$OSArchitecture = 'ia64'} if ($OSArchitecture -eq '9') {$OSArchitecture = 'x64'} if ($OSArchitecture -eq '12') {$OSArchitecture = 'x64 ARM'} $OSEditionID = $($WindowsImage.EditionId) $OSInstallationType = $($WindowsImage.InstallationType) $OSLanguages = $($WindowsImage.Languages) $OSMajorVersion = $($WindowsImage.MajorVersion) $OSBuild = $($WindowsImage.Build) $OSVersion = $($WindowsImage.Version) $OSSPBuild = $($WindowsImage.SPBuild) $OSSPLevel = $($WindowsImage.SPLevel) $OSImageBootable = $($WindowsImage.ImageBootable) $OSWIMBoot = $($WindowsImage.WIMBoot) $OSCreatedTime = $($WindowsImage.CreatedTime) $OSModifiedTime = $($WindowsImage.ModifiedTime) #=================================================================================================== Write-Verbose '19.1.1 Source OSMedia Windows Image Information' #=================================================================================================== Write-Host '========================================================================================' -ForegroundColor DarkGray Write-Host "Source OSMedia Windows Image Information" -ForegroundColor Green Write-Host "-Source Path: $OSSourcePath" Write-Host "-Image File: $OSImagePath" Write-Host "-Image Index: $OSImageIndex" Write-Host "-Name: $OSImageName" Write-Host "-Description: $OSImageDescription" Write-Host "-Architecture: $OSArchitecture" Write-Host "-Edition: $OSEditionID" Write-Host "-Type: $OSInstallationType" Write-Host "-Languages: $OSLanguages" Write-Host "-Major Version: $OSMajorVersion" Write-Host "-Build: $OSBuild" Write-Host "-Version: $OSVersion" Write-Host "-SPBuild: $OSSPBuild" Write-Host "-SPLevel: $OSSPLevel" Write-Host "-Bootable: $OSImageBootable" Write-Host "-WimBoot: $OSWIMBoot" Write-Host "-Created Time: $OSCreatedTime" Write-Host "-Modified Time: $OSModifiedTime" #=================================================================================================== Write-Verbose '19.1.1 Validate Registry CurrentVersion.xml' #=================================================================================================== if (Test-Path "$OSSourcePath\info\xml\CurrentVersion.xml") { $RegCurrentVersion = Import-Clixml -Path "$OSSourcePath\info\xml\CurrentVersion.xml" $OSVersionNumber = $($RegCurrentVersion.ReleaseId) if ($OSVersionNumber -gt 1809) { Write-Warning "OSBuilder does not currently support this version of Windows ... Check for an updated version" } } #=================================================================================================== Write-Verbose '19.1.1 Set OSVersionNumber' #=================================================================================================== if ($null -eq $OSVersionNumber) { if ($OSBuild -eq 7601) {$OSVersionNumber = 7601} if ($OSBuild -eq 10240) {$OSVersionNumber = 1507} if ($OSBuild -eq 14393) {$OSVersionNumber = 1607} if ($OSBuild -eq 15063) {$OSVersionNumber = 1703} if ($OSBuild -eq 16299) {$OSVersionNumber = 1709} if ($OSBuild -eq 17134) {$OSVersionNumber = 1803} if ($OSBuild -eq 17763) {$OSVersionNumber = 1809} } #=================================================================================================== Write-Verbose '19.1.1 WinPE DaRT' #=================================================================================================== $SelectedWinPEDaRT =@() $SelectedWinPEDaRT = Get-ChildItem -Path "$OSBuilderContent\WinPE\DaRT" *.cab -Recurse | Select-Object -Property Name, FullName foreach ($Pack in $SelectedWinPEDaRT) {$Pack.FullName = $($Pack.FullName).replace("$OSBuilderContent\",'')} $SelectedWinPEDaRT = $SelectedWinPEDaRT | Where-Object {$_.FullName -like "*$OSArchitecture*"} $SelectedWinPEDaRT = $SelectedWinPEDaRT | Out-GridView -Title "Select a WinPE DaRT Package to apply and press OK (Esc or Cancel to Skip)" -OutputMode Single if($null -eq $SelectedWinPEDaRT) {Write-Warning "Skipping WinPE DaRT"} #=================================================================================================== Write-Verbose '19.1.1 WinPE Drivers' #=================================================================================================== $SelectedWinPEDrivers =@() $SelectedWinPEDrivers = Get-ChildItem -Path "$OSBuilderContent\WinPE\Drivers" -Directory | Select-Object -Property Name, FullName $SelectedWinPEDrivers = $SelectedWinPEDrivers | Where-Object {(Get-ChildItem $_.FullName | Measure-Object).Count -gt 0} foreach ($Pack in $SelectedWinPEDrivers) {$Pack.FullName = $($Pack.FullName).replace("$OSBuilderContent\",'')} $SelectedWinPEDrivers = $SelectedWinPEDrivers | Out-GridView -Title "Select WinPE Drivers to apply and press OK (Esc or Cancel to Skip)" -PassThru if($null -eq $SelectedWinPEDrivers) {Write-Warning "Skipping WinPE Drivers"} #=================================================================================================== Write-Verbose '19.1.1 WinPE Scripts' #=================================================================================================== $SelectedWinPEScripts =@() $SelectedWinPEScripts = Get-ChildItem -Path "$OSBuilderContent\WinPE\Scripts" *.ps1 | Select-Object -Property Name, FullName foreach ($Pack in $SelectedWinPEScripts) {$Pack.FullName = $($Pack.FullName).replace("$OSBuilderContent\",'')} $SelectedWinPEScripts = $SelectedWinPEScripts | Out-GridView -Title "Select WinPE PowerShell Scripts to execute and press OK (Esc or Cancel to Skip)" -PassThru if($null -eq $SelectedWinPEScripts) {Write-Warning "Skipping WinPE PowerShell Scripts"} #=================================================================================================== Write-Verbose '19.1.1 WinPE Extra Files' #=================================================================================================== $SelectedWinPEExtraFiles =@() $SelectedWinPEExtraFiles = Get-ChildItem -Path "$OSBuilderContent\WinPE\ExtraFiles" -Directory | Select-Object -Property Name, FullName $SelectedWinPEExtraFiles = $SelectedWinPEExtraFiles | Where-Object {(Get-ChildItem $_.FullName | Measure-Object).Count -gt 0} foreach ($Pack in $SelectedWinPEExtraFiles) {$Pack.FullName = $($Pack.FullName).replace("$OSBuilderContent\",'')} $SelectedWinPEExtraFiles = $SelectedWinPEExtraFiles | Out-GridView -Title "Select WinPE Extra Files to apply and press OK (Esc or Cancel to Skip)" -PassThru if($null -eq $SelectedWinPEExtraFiles) {Write-Warning "Skipping WinPE Extra Files"} #=================================================================================================== Write-Verbose '19.1.1 Setup ADK Packages' #=================================================================================================== $SelectedWinPEADKPkgs =@() $SelectedWinPEADKPkgs = Get-ChildItem -Path "$OSBuilderContent\WinPE\ADK" *.cab -Recurse | Select-Object -Property Name, FullName foreach ($Pack in $SelectedWinPEADKPkgs) {$Pack.FullName = $($Pack.FullName).replace("$OSBuilderContent\",'')} $SelectedWinPEADKPkgs = $SelectedWinPEADKPkgs | Where-Object {$_.FullName -like "*$OSArchitecture*"} $SelectedWinPEADKPkgs = $SelectedWinPEADKPkgs | Where-Object {$_.FullName -like "*$OSVersionNumber*"} $SelectedWinPEADKPkgs = $SelectedWinPEADKPkgs | Out-GridView -Title "Select WinPE ADK Packages to apply and press OK (Esc or Cancel to Skip)" -PassThru if($null -eq $SelectedWinPEADKPkgs) {Write-Warning "Skipping WinPE ADK Packages"} #=================================================================================================== Write-Verbose '19.1.1 Build Task' #=================================================================================================== $Task = [ordered]@{ "TaskName" = [string]$TaskName; "TaskVersion" = [string]$($(Get-Module -Name OSBuilder).Version); "TaskType" = "PEBuild"; "AutoExtraFiles" = [string]"$AutoExtraFiles"; "DeploymentShare" = [string]"$DeploymentShare"; "MediaName" = [string]$OSMedia.Name; "PEOutput" = [string]"$PEOutput"; "ScratchSpace" = [string]"$ScratchSpace"; "SourceWim" = [string]"$SourceWim"; "WinPEAddADK" = [string[]]$SelectedWinPEADKPkgs.FullName; "WinPEAddDaRT" = [string]$SelectedWinPEDaRT.FullName; "WinPEAddWindowsDriver" = [string[]]$SelectedWinPEDrivers.FullName; "WinPEInvokeScript" = [string[]]$SelectedWinPEScripts.FullName; "WinPERobocopyExtraFiles" = [string[]]$SelectedWinPEExtraFiles.FullName; } #=================================================================================================== Write-Verbose '19.1.1 New-OSBuildTask Complete' #=================================================================================================== Write-Host '========================================================================================' -ForegroundColor DarkGray Write-Host "OSBuild Task: $TaskName" -ForegroundColor Green $Task | ConvertTo-Json | Out-File "$OSBuilderTasks\$TaskName.json" $Task } END { #Write-Host '========================================================================================' -ForegroundColor DarkGray #Write-Host "$($MyInvocation.MyCommand.Name) END" -ForegroundColor Green } } |