Update USBImagingToolCreator.ps1

added support for relative paths. formatted script and removed functions from inside if statement.
This commit is contained in:
w0
2024-09-12 13:54:58 -05:00
committed by GitHub
parent 60d147c71d
commit c8ef42ab21
+90 -72
View File
@@ -1,20 +1,18 @@
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $True, Position = 0)] [Parameter(Mandatory = $True, Position = 0)]
$DeployISOPath, [io.fileinfo] $DeployISOPath,
[Switch]$DisableAutoPlay [Switch]$DisableAutoPlay
) )
$Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator'
if($DeployISOPath){
$DevelopmentPath = $DeployISOPath | Split-Path
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 Get-USBDrive {
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'") $USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
If ($USBDrives -and ($null -eq $USBDrives.count)) { If ($USBDrives -and ($null -eq $USBDrives.count)) {
$USBDrivesCount = 1 $USBDrivesCount = 1
@@ -31,7 +29,8 @@ Function Get-USBDrive {
} }
return $USBDrives, $USBDrivesCount return $USBDrives, $USBDrivesCount
} }
Function Build-DeploymentUSB{
function Build-DeploymentUSB {
param( param(
[Array]$Drives [Array]$Drives
) )
@@ -49,7 +48,7 @@ Function Build-DeploymentUSB{
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
@@ -62,22 +61,23 @@ 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,
@@ -87,11 +87,11 @@ $Destination = $Drive + ":\"
} }
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){ if ($Images) {
writelog "Copying FFU image files to all drives labeled deploy concurrently" writelog "Copying FFU image files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) { foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\" $Destination = $Drive + ":\"
$jobScriptBlock = { $jobScriptBlock = {
param ( param (
[string]$SFolder, [string]$SFolder,
@@ -103,11 +103,11 @@ $Destination = $Drive + ":\"
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 $FFUPath, $Destination | Out-Null
} }
} }
if($Drivers){ if ($Drivers) {
writelog "Copying driver files to all drives labeled deploy concurrently" writelog "Copying driver files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) { foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\Drivers" $Destination = $Drive + ":\Drivers"
$jobScriptBlock = { $jobScriptBlock = {
param ( param (
[string]$SFolder, [string]$SFolder,
@@ -118,27 +118,28 @@ $Destination = $Drive + ":\Drivers"
} }
WriteLog "Start job to copy all drivers to $Destination" WriteLog "Start job to copy all drivers to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $DriversPath, $Destination | Out-Null Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $DriversPath, $Destination | Out-Null
} }
} }
if(!($Drivers)){ if (!($Drivers)) {
foreach ($Drive in $DeployDrives) { foreach ($Drive in $DeployDrives) {
WriteLog "Create drivers directory" WriteLog "Create drivers directory"
$drivepath = $Drive + ":\" $drivepath = $Drive + ":\"
New-Item -Path "$drivepath" -Name Drivers -ItemType Directory -Force -Confirm: $false | Out-Null New-Item -Path "$drivepath" -Name Drivers -ItemType Directory -Force -Confirm: $false | Out-Null
} }
} }
if($DrivesCount -gt 1){ if ($DrivesCount -gt 1) {
Writelog "Building $DrivesCount drives concurrently...Please be patient..." Writelog "Building $DrivesCount drives concurrently...Please be patient..."
}else{ }
Writelog "Building the imaging tool on $model...Please be patient..." else {
} Writelog "Building the imaging tool on $model...Please be patient..."
Get-Job | Wait-Job | Out-Null }
Get-Job | Wait-Job | Out-Null
Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null
Writelog "Drive creation jobs completed..." Writelog "Drive creation jobs completed..."
} }
Function New-DeploymentUSB { function New-DeploymentUSB {
param( param(
[Array]$Drives, [Array]$Drives,
[int]$Count, [int]$Count,
@@ -149,16 +150,16 @@ 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)'
@@ -166,20 +167,22 @@ Function New-DeploymentUSB {
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){ if ($Last) {
writelog "All drives selected" writelog "All drives selected"
}else{ }
writelog "Drive $DriveSelected 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)
$DisableAutoPlayCurrentSetting = (Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name DisableAutoplay).DisableAutoplay $DisableAutoPlayCurrentSetting = (Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name DisableAutoplay).DisableAutoplay
if($DisableAutoPlay -and $DisableAutoPlayCurrentSetting -ne 1){ if ($DisableAutoPlay -and $DisableAutoPlayCurrentSetting -ne 1) {
writelog "Disable autoPlay current setting is $DisableAutoPlayCurrentSetting" writelog "Disable autoPlay current setting is $DisableAutoPlayCurrentSetting"
WriteLog "Setting the registry key to disable autoplay for all drives" 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 Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value 1 -Type DWORD
@@ -190,34 +193,49 @@ Function New-DeploymentUSB {
Stop-Process -Name diskpart -ErrorAction SilentlyContinue Stop-Process -Name diskpart -ErrorAction SilentlyContinue
$Selection = $Drivelist[$DriveSelected].Number $Selection = $Drivelist[$DriveSelected].Number
$totalSteps = 5 $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" Writelog "Setting disable autoplay setting back to $DisableAutoPlayCurrentSetting"
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 $DisableAutoPlayCurrentSetting -Type DWORD
} }
Writelog "Completed!" Writelog "Completed!"
} }
#Get USB Drive and create log file
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 $Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator'
}else{
Write-Host "No .ISO file selected..." # Check if path is relative
read-host "Press ENTER to Exit..." if (-not [System.IO.Path]::IsPathRooted($DeployISOPath)) {
Exit # 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)"
}
if ($DeployISOPath.Exists) {
$DevelopmentPath = $DeployISOPath.Directory.FullName
#Get USB Drive and create log file
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 {
Write-Error "Unable to locate ISO file at: $($DeployISOPath.FullName)"
} }