Update USBImagingToolCreator.ps1

This commit is contained in:
Mike Kelly
2024-12-03 15:09:00 -05:00
committed by rbalsleyMSFT
parent 6abc6f9d1a
commit a2c2b69026
+157 -150
View File
@@ -1,20 +1,35 @@
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $True, Position = 0)] [Parameter(Mandatory = $True, Position = 0)]
[io.fileinfo] $DeployISOPath, $DeployISOPath,
[Switch]$DisableAutoPlay [Switch]$DisableAutoPlay
) )
$Host.UI.RawUI.WindowTitle = 'Imaging Tool USB Creator'
if($DeployISOPath){
$DevelopmentPath = $DeployISOPath | Split-Path
$ImagesPath = "$DevelopmentPath\FFU"
function WriteLog($LogText) { function WriteLog($LogText) {
$LogFileName = '\Script.log' $LogFileName = '\Script.log'
$LogFile = $DevelopmentPath + $LogFilename $LogFile = $DevelopmentPath + $LogFilename
Add-Content -path $LogFile -value "$((Get-Date).ToString()) $LogText" -Force -ErrorAction SilentlyContinue Add-Content -path $LogFile -value "$((Get-Date).ToString()) $LogText" -Force -ErrorAction SilentlyContinue
Write-Verbose $LogText Write-Verbose $LogText
} }
function Get-USBDrive { function Write-ProgressLog {
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'") param(
If ($USBDrives -and ($null -eq $USBDrives.count)) { [string]$Activity,
[string]$Status
)
Write-Progress -Activity $Activity -Status $Status -PercentComplete (($currentStep / $totalSteps) * 100)
WriteLog $Status
$script:currentStep++
}
Function Get-RemovableDrive {
writelog "Get information for all removable drives"
$USBDrives = Get-WmiObject Win32_DiskDrive | Where-Object {$_.MediaType -eq "Removable media"}
If($USBDrives -and ($null -eq $USBDrives.count)) {
$USBDrivesCount = 1 $USBDrivesCount = 1
} else { } else {
$USBDrivesCount = $USBDrives.Count $USBDrivesCount = $USBDrives.Count
@@ -24,31 +39,33 @@ function Get-USBDrive {
if ($null -eq $USBDrives) { if ($null -eq $USBDrives) {
WriteLog "No removable USB drive found. Exiting" WriteLog "No removable USB drive found. Exiting"
Write-Error "No removable USB drive found. Exiting" Write-Error "No removable USB drive found. Exiting"
Pause
exit 1 exit 1
} }
return $USBDrives, $USBDrivesCount return $USBDrives, $USBDrivesCount
} }
function Build-DeploymentUSB { Function Build-DeploymentUSB{
param( param(
[Array]$Drives [Array]$Drives
) )
writelog "Checking if ffu files are present in the ffu folder" writelog "Creating list of FFU image files"
$Images = Get-ChildItem -Path $FFUPath -Filter "*.ffu" -File -Recurse $Images = Get-ChildItem -Path $ImagesPath -Filter "*.ffu" -File -Recurse
writelog "Checking if drivers are present in the drivers folder" writelog "Checking if drivers are present in the drivers folder"
$Drivers = Get-ChildItem -Path $DriversPath -Recurse $Drivers = Get-ChildItem -Path $DriversPath -Recurse
$DrivesCount = $Drives.Count $DrivesCount = $Drives.Count
Writelog "Creating partitions..." Write-ProgressLog "Create Imaging Tool" "Creating partitions..."
foreach ($USBDrive in $Drives) { writelog "Create job to partition each usb drive"
$DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "") foreach ($USBDrive in $Drives) {
$Model = $USBDrive.model $DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "")
$ScriptBlock = { $Model = $USBDrive.model
$ScriptBlock = {
param($DriveNumber) param($DriveNumber)
Clear-Disk -Number $DriveNumber -RemoveData -RemoveOEM -Confirm:$false Clear-Disk -Number $DriveNumber -RemoveData -RemoveOEM -Confirm:$false
$Disk = Get-Disk -Number $DriveNumber $Disk = Get-Disk -Number $DriveNumber
$PartitionStyle = $Disk.PartitionStyle $PartitionStyle = $Disk.PartitionStyle
if ($PartitionStyle -ne 'MBR') { if($PartitionStyle -ne 'MBR'){
$Disk | Set-Disk -PartitionStyle MBR $Disk | Set-Disk -PartitionStyle MBR
} }
$BootPartition = New-Partition -DiskNumber $DriveNumber -Size 2GB -IsActive -AssignDriveLetter $BootPartition = New-Partition -DiskNumber $DriveNumber -Size 2GB -IsActive -AssignDriveLetter
$DeployPartition = New-Partition -DiskNumber $DriveNumber -UseMaximumSize -AssignDriveLetter $DeployPartition = New-Partition -DiskNumber $DriveNumber -UseMaximumSize -AssignDriveLetter
@@ -60,83 +77,91 @@ function Build-DeploymentUSB {
} }
writelog "Wait for partitioning jobs to complete" writelog "Wait for partitioning jobs to complete"
Get-Job | Wait-Job | Out-Null Get-Job | Wait-Job | Out-Null
if ($DrivesCount -gt 1) { if($DrivesCount -gt 1){
writelog "Get file system information for all drives" writelog "Get file system information for all drives"
$Partitions = Get-Partition | Get-Volume $Partitions = Get-Partition | Get-Volume
} else { } else {
writelog "Get file system information for drive number $DiskNumber" writelog "Get file system information for drive number $DiskNumber"
$Partitions = Get-Partition -DiskNumber $DriveNumber | Get-Volume $Partitions = Get-Partition -DiskNumber $DriveNumber | Get-Volume
} }
writelog "Get drive letter for all volumes labeled:BOOT" writelog "Get drive letter for all volumes labeled:BOOT"
$BootDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "BOOT" }).DriveLetter $BootDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "BOOT"}).DriveLetter
writelog "Get drive letter for all volumes labeled:Deploy" writelog "Get drive letter for all volumes labeled:Deploy"
$DeployDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "Deploy" }).DriveLetter $DeployDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "Deploy"}).DriveLetter
writelog "Mount Deployment .iso image" writelog "Mount Deployment .iso image"
$ISOMountPoint = (Mount-DiskImage -ImagePath "$DeployISOPath" -PassThru | Get-Volume).DriveLetter + ":\" $ISOMountPoint = (Mount-DiskImage -ImagePath "$DeployISOPath" -PassThru | Get-Volume).DriveLetter + ":\"
writelog "Copying boot files to all drives labeled BOOT concurrently" writelog "Copying boot files to all drives labeled BOOT concurrently"
foreach ($Drive in $BootDrives) { foreach ($Drive in $BootDrives) {
$Destination = $Drive + ":\" $Destination = $Drive + ":\"
$jobScriptBlock = { $jobScriptBlock = {
param ( param (
[string]$SFolder, [string]$SFolder,
[string]$DFolder [string]$DFolder
) )
Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J
} }
WriteLog "Start job to copy all boot files to $Destination" WriteLog "Start job to copy all boot files to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $ISOMountPoint, $Destination | Out-Null Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $ISOMountPoint, $Destination | Out-Null
}
if($Images){
writelog "Copying FFU image files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\"
$jobScriptBlock = {
param (
[string]$SFolder,
[string]$DFolder
)
New-Item -Path $DFolder -ItemType Directory -Force -Confirm: $false | Out-Null
Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J
} }
if ($Images) {
writelog "Copying FFU image files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\"
$jobScriptBlock = {
param (
[string]$SFolder,
[string]$DFolder
)
Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J
}
WriteLog "Start job to copy all FFU files to $Destination" WriteLog "Start job to copy all FFU files to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $FFUPath, $Destination | Out-Null Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $ImagesPath, $Destination | Out-Null
}
}
if(!($Images)){
foreach ($Drive in $DeployDrives) {
WriteLog "Create images directory"
$drivepath = $Drive + ":\"
New-Item -Path "$drivepath" -Name Images -ItemType Directory -Force -Confirm: $false | Out-Null
} }
}
if($Drivers){
writelog "Copying driver files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\Drivers"
$jobScriptBlock = {
param (
[string]$SFolder,
[string]$DFolder
)
New-Item -Path $DFolder -ItemType Directory -Force -Confirm: $false | Out-Null
Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J
} }
if ($Drivers) { WriteLog "Start job to copy all drivers to $Destination"
writelog "Copying driver files to all drives labeled deploy concurrently" Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $DriversPath, $Destination | Out-Null
foreach ($Drive in $DeployDrives) { }
$Destination = $Drive + ":\Drivers" }
$jobScriptBlock = { if(!($Drivers)){
param ( foreach ($Drive in $DeployDrives) {
[string]$SFolder, WriteLog "Create drivers directory"
[string]$DFolder $drivepath = $Drive + ":\"
) New-Item -Path "$drivepath" -Name Drivers -ItemType Directory -Force -Confirm: $false | Out-Null
New-Item -Path $DFolder -ItemType Directory -Force -Confirm: $false | Out-Null
Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J
}
WriteLog "Start job to copy all drivers to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $DriversPath, $Destination | Out-Null
} }
} }
if (!($Drivers)) { if($DrivesCount -gt 1){
foreach ($Drive in $DeployDrives) { Write-ProgressLog "Create Imaging Tool" "Building $DrivesCount drives concurrently...Please be patient..."
WriteLog "Create drivers directory" } else {
$drivepath = $Drive + ":\" Write-ProgressLog "Create Imaging Tool" "Building the imaging tool on $model...Please be patient..."
New-Item -Path "$drivepath" -Name Drivers -ItemType Directory -Force -Confirm: $false | Out-Null }
} Get-Job | Wait-Job | Out-Null
}
if ($DrivesCount -gt 1) {
Writelog "Building $DrivesCount drives concurrently...Please be patient..."
} else {
Writelog "Building the imaging tool on $model...Please be patient..."
}
Get-Job | Wait-Job | Out-Null
Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null
Writelog "Drive creation jobs completed..." Write-ProgressLog "Create Imaging Tool" "Drive creation jobs completed..."
} }
function New-DeploymentUSB { Function New-DeploymentUSB {
param( param(
[Array]$Drives, [Array]$Drives,
[int]$Count, [int]$Count,
@@ -147,91 +172,73 @@ function New-DeploymentUSB {
$Drivelist = @() $Drivelist = @()
writelog "Creating a USB drive selection list" writelog "Creating a USB drive selection list"
for ($i = 0; $i -le $Count - 1; $i++) { for($i=0;$i -le $Count -1;$i++){
$DriveModel = $Drives[$i].Model $DriveModel = $Drives[$i].Model
$DriveSize = [math]::round($Drives[$i].size / 1GB, 2) $DriveSize = [math]::round($Drives[$i].size/1GB, 2)
$DiskNumber = $Drives[$i].DeviceID.Replace("\\.\PHYSICALDRIVE", "") $DiskNumber = $Drives[$i].DeviceID.Replace("\\.\PHYSICALDRIVE", "")
$Properties = [ordered]@{Number = $i + 1 ; DriveNumber = $DiskNumber ; DriveModel = $driveModel ; 'Size (GB)' = $DriveSize } $Properties = [ordered]@{Number = $i + 1 ; DriveNumber = $DiskNumber ; DriveModel = $driveModel ; 'Size (GB)' = $DriveSize}
$Drivelist += New-Object PSObject -Property $Properties $Drivelist += New-Object PSObject -Property $Properties
} }
if ($Count -gt 1) { if($Count -gt 1){
$Last = $Count + 1 $Last = $Count+1
$Drivelist += New-Object -TypeName PSObject -Property @{ Number = "$last"; DriveModel = "Select this option to use all ($count) inserted USB Drives" } $Drivelist += New-Object -TypeName PSObject -Property @{ Number = "$last"; DriveModel = "Select this option to use all ($count) inserted USB Drives" }
} }
$Drivelist | Format-Table -AutoSize -Property Number, DriveModel , 'Size (GB)' $Drivelist | Format-Table -AutoSize -Property Number, DriveModel , 'Size (GB)'
do { do {
try { try {
$var = $true $var = $true
$DriveSelected = Read-Host 'Enter the drive number to apply the .iso to' $DriveSelected = Read-Host 'Enter the drive number to apply the .iso to'
$DriveSelected = ($DriveSelected -as [int]) - 1 $DriveSelected = ($DriveSelected -as [int]) -1
if ($Last) { writelog "Drive $DriveSelected selected"
writelog "All drives selected"
} }
else {
writelog "Drive $DriveSelected selected"
}
}
catch { catch {
Write-Host 'Input was not in correct format. Please enter a valid FFU number' Write-Host 'Input was not in correct format. Please enter a valid FFU number'
$var = $false $var = $false
} }
} until (($DriveSelected -le $Count - 1 -or $last) -and $var) } until (($DriveSelected -le $Count -1 -or $last) -and $var)
if($DisableAutoPlay){
$DisableAutoPlayCurrentSetting = (Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name DisableAutoplay).DisableAutoplay WriteLog "Setting the registry key to disable autoplay for all drives"
if ($DisableAutoPlay -and $DisableAutoPlayCurrentSetting -ne 1) { Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value 1 -Type DWORD
writelog "Disable autoPlay current setting is $DisableAutoPlayCurrentSetting"
WriteLog "Setting the registry key to disable autoplay for all drives"
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value 1 -Type DWORD
} }
WriteLog "Closing all MMC windows to prevent drive lock errors" WriteLog "Closing all MMC windows to prevent drive lock errors"
Stop-Process -Name mmc -ErrorAction SilentlyContinue Stop-Process -Name mmc -ErrorAction SilentlyContinue
WriteLog "Closing all Diskpart windows to prevent drive lock errors" WriteLog "Closing all Diskpart windows to prevent drive lock errors"
Stop-Process -Name diskpart -ErrorAction SilentlyContinue Stop-Process -Name diskpart -ErrorAction SilentlyContinue
$Selection = $Drivelist[$DriveSelected].Number $Selection = $Drivelist[$DriveSelected].Number
$totalSteps = 5
if ($Selection -eq $last) { if($Selection -eq $last){
Read-Host -Prompt "ALL DRIVES SELECTED! WILL ERASE ALL CURRENTLY CONNECTED USB DRIVES!! Press ENTER to continue" Read-Host -Prompt "ALL DRIVES SELECTED! WILL ERASE ALL CURRENTLY CONNECTED USB DRIVES!! Press ENTER to continue"
Build-DeploymentUSB -Drives $Drives Build-DeploymentUSB -Drives $Drives
} else { } else {
Read-Host -Prompt "Drive number $Selection was selected. Press ENTER to continue" Read-Host -Prompt "Drive number $Selection was selected. Press ENTER to continue"
Build-DeploymentUSB -Drives $Drives[$DriveSelected] Build-DeploymentUSB -Drives $Drives[$DriveSelected]
} }
WriteLog "Setting the registry key to re-enable autoplay for all drives" WriteLog "Setting the registry key to re-enable autoplay for all drives"
if ($DisableAutoPlay) { if($DisableAutoPlay){
Writelog "Setting disable autoplay setting back to $DisableAutoPlayCurrentSetting" Write-ProgressLog "Create Imaging Tool" "Enabling Autoplay"
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value $DisableAutoPlayCurrentSetting -Type DWORD Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value 0 -Type DWORD
} }
Writelog "Completed!" Write-ProgressLog "Create Imaging Tool" "Completed!"
} }
#Get USB Drive and create log file
$Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator' if(Test-Path "$DevelopmentPath\Script.log"){
Remove-Item -Path "$DevelopmentPath\Script.log" -Force -Confirm:$false
# Check if path is relative New-item -Path $DevelopmentPath -Name 'Script.log' -ItemType "file" -Force | Out-Null
if (-not [System.IO.Path]::IsPathRooted($DeployISOPath)) {
# Path is relative, Build full path
[io.fileinfo] $DeployISOPath = (Resolve-Path $DeployISOPath).Path
Write-Verbose "Path to ISO was relative. Attempting to resolve at: $($DeployISOPath.FullName)"
} }
WriteLog 'Begin Logging'
WriteLog 'Getting USB drive information and usb drive count'
$USBDrives,$USBDrivesCount = Get-RemovableDrive
WriteLog 'Setting first step for percentage progress bar'
$currentStep = 1
New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount
if ($DeployISOPath.Exists) { read-host -Prompt "USB drive creation complete. Press ENTER to exit"
$DevelopmentPath = $DeployISOPath.Directory.FullName
#Get USB Drive and create log file Exit
if (Test-Path "$DevelopmentPath\Script.log") {
Remove-Item -Path "$DevelopmentPath\Script.log" -Force -Confirm:$false
New-item -Path $DevelopmentPath -Name 'Script.log' -ItemType "file" -Force | Out-Null
}
WriteLog 'Begin Logging'
WriteLog 'Getting USB drive information and usb drive count'
$USBDrives, $USBDrivesCount = Get-USBDrive
New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount
Read-Host -Prompt "USB drive creation complete. Press ENTER to exit"
Exit
} else { } else {
Write-Error "Unable to locate ISO file at: $($DeployISOPath.FullName)" Write-Host "No .ISO file selected..."
read-host "Press ENTER to Exit..."
Exit
} }