New-OSDCloudUSBModified.ps1

param (     
    #Path to an Mounted ISO
    #This file will be mounted and the contents will be copied to the OSDCloud USB
    [Parameter(Mandatory)]
    [System.String]$fromPath,

    [Parameter(Mandatory)]
    [int]$DriveNumber,

    [System.Management.Automation.SwitchParameter]$Force
)
. "$PSScriptRoot\New-BootableUSBModified.ps1"
#=================================================
# Block
#=================================================
Block-StandardUser
Block-WindowsVersionNe10
Block-PowerShellVersionLt5
Block-WindowsReleaseIdLt1703
Block-WinPE
#=================================================
# Initialize
#=================================================
$BootLabel = 'WinPE'
$DataLabel = 'OSDCloudUSB'
$ErrorActionPreference = 'Stop'
$WinpeSourcePath = $fromPath

#=================================================
# Test WinpeSourcePath
#=================================================
if (-NOT ($WinpeSourcePath)) {
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find an OSDCloud Media"
    Break
}

if (-NOT (Test-Path $WinpeSourcePath)) {
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find an OSDCloud Media at $WinpeSourcePath"
    Break
}

if (-NOT (Test-Path "$WinpeSourcePath\sources\boot.wim")) {
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find an OSDCloud WinPE at $WinpeSourcePath\sources\boot.wim"
    Break
}
#=================================================
# New-Bootable.usb
#=================================================
$params = @{
    BootLabel = $BootLabel
    DataLabel = $DataLabel
    DiskNumber = $DriveNumber }
if($PSBoundParameters.ContainsKey('Force')){
    $params.Force = $true
}
$BootableUSB = New-BootableUSBModified @params
#=================================================
# Test USB Volumes
#=================================================
$WinPEPartition = Get-Partition.usb | Where-Object {($_.DiskNumber -eq $BootableUSB.DiskNumber) -and ($_.PartitionNumber -eq 2)}
if (-NOT ($WinPEPartition)) {
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to create OSDCloud WinPE Partition"
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Something went very very wrong in this process"
    Break
}
$OSDCloudPartition = Get-Partition.usb | Where-Object {($_.DiskNumber -eq $BootableUSB.DiskNumber) -and ($_.PartitionNumber -eq 1)}
if (-NOT ($OSDCloudPartition)) {
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to create OSDCloud Data Partition"
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Something went very very wrong in this process"
    Break
}
#=================================================
# WinpeDestinationPath
#=================================================
$WinpeDestinationPath = "$($WinPEPartition.DriveLetter):\"
if (-NOT ($WinpeDestinationPath)) {
    Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find Destination Path at $WinpeDestinationPath"
    Break
}
#=================================================
# Update WinPE Volume
#=================================================
if ((Test-Path -Path "$WinpeSourcePath") -and (Test-Path -Path "$WinpeDestinationPath")) {
    Write-Host -ForegroundColor DarkGray "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Copying $WinpeSourcePath to OSDCloud WinPE partition at $WinpeDestinationPath"
    robocopy "$WinpeSourcePath" "$WinpeDestinationPath" *.* /e /ndl /njh /njs /np /r:0 /w:0 /b /zb
}
#=================================================
# Remove Read-Only Attribute
#=================================================
Get-ChildItem -Path $WinpeDestinationPath -File -Recurse -Force | foreach {
    Set-ItemProperty -Path $_.FullName -Name IsReadOnly -Value $false -Force -ErrorAction Ignore
}
#=================================================
# Dismount OSDCloudISO
#=================================================
if ($MountDiskImage) {
    Start-Sleep -Seconds 3
    Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Dismounting $($MountDiskImage.ImagePath)"
    $null = Dismount-DiskImage -ImagePath $MountDiskImage.ImagePath
}
#=================================================
# Complete
#=================================================
Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) New-OSDCloudUSB is complete"
#=================================================