From 67cc8c122546d7f45b7c41f2d242ea1fdb167d9c Mon Sep 17 00:00:00 2001 From: Mike Kelly <167896478+MKellyCBSD@users.noreply.github.com> Date: Tue, 9 Jul 2024 11:29:58 -0400 Subject: [PATCH] Update BuildUSBDrives.ps1 Update code --- FFUDevelopment/BuildUSBDrives.ps1 | 67 ++++++++++--------------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/FFUDevelopment/BuildUSBDrives.ps1 b/FFUDevelopment/BuildUSBDrives.ps1 index a797701..514acce 100644 --- a/FFUDevelopment/BuildUSBDrives.ps1 +++ b/FFUDevelopment/BuildUSBDrives.ps1 @@ -8,28 +8,15 @@ $Host.UI.RawUI.WindowTitle = 'Imaging Tool USB Creator' if($DeployISOPath){ $DevelopmentPath = $DeployISOPath | Split-Path -$ImagesPath = "$DevelopmentPath\FFU" function WriteLog($LogText) { $LogFileName = '\Script.log' $LogFile = $DevelopmentPath + $LogFilename Add-Content -path $LogFile -value "$((Get-Date).ToString()) $LogText" -Force -ErrorAction SilentlyContinue Write-Verbose $LogText } - -function Write-ProgressLog { - param( - [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)) { +Function Get-USBDrive { + $USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'") + If ($USBDrives -and ($null -eq $USBDrives.count)) { $USBDrivesCount = 1 } else { @@ -40,23 +27,20 @@ If($USBDrives -and ($null -eq $USBDrives.count)) { if ($null -eq $USBDrives) { WriteLog "No removable USB drive found. Exiting" Write-Error "No removable USB drive found. Exiting" - Pause exit 1 } - return $USBDrives, $USBDrivesCount - } - + return $USBDrives, $USBDrivesCount +} Function Build-DeploymentUSB{ param( [Array]$Drives ) - writelog "Creating list of FFU image files" + writelog "Checking if ffu files are present in the ffu folder" $Images = Get-ChildItem -Path $FFUPath -Filter "*.ffu" -File -Recurse writelog "Checking if drivers are present in the drivers folder" $Drivers = Get-ChildItem -Path $DriversPath -Recurse $DrivesCount = $Drives.Count - Write-ProgressLog "Create Imaging Tool" "Creating partitions..." - writelog "Create job to partition each usb drive" + Writelog "Creating partitions..." foreach ($USBDrive in $Drives) { $DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "") $Model = $USBDrive.model @@ -107,27 +91,19 @@ $Destination = $Drive + ":\" if($Images){ writelog "Copying FFU image files to all drives labeled deploy concurrently" foreach ($Drive in $DeployDrives) { -$Destination = $Drive + ":\Images" +$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 } WriteLog "Start job to copy all FFU files to $Destination" - Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $ImagesPath, $Destination | Out-Null + Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $FFUPath, $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) { @@ -152,14 +128,14 @@ if(!($Drivers)){ } } if($DrivesCount -gt 1){ -Write-ProgressLog "Create Imaging Tool" "Building $DrivesCount drives concurrently...Please be patient..." +Writelog "Building $DrivesCount drives concurrently...Please be patient..." }else{ -Write-ProgressLog "Create Imaging Tool" "Building the imaging tool on $model...Please be patient..." +Writelog "Building the imaging tool on $model...Please be patient..." } Get-Job | Wait-Job | Out-Null Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null -Write-ProgressLog "Create Imaging Tool" "Drive creation jobs completed..." +Writelog "Drive creation jobs completed..." } Function New-DeploymentUSB { @@ -178,7 +154,7 @@ Function New-DeploymentUSB { $DriveSize = [math]::round($Drives[$i].size/1GB, 2) $DiskNumber = $Drives[$i].DeviceID.Replace("\\.\PHYSICALDRIVE", "") $Properties = [ordered]@{Number = $i + 1 ; DriveNumber = $DiskNumber ; DriveModel = $driveModel ; 'Size (GB)' = $DriveSize} - + $Drivelist += New-Object PSObject -Property $Properties } if($Count -gt 1){ @@ -191,10 +167,12 @@ Function New-DeploymentUSB { $var = $true $DriveSelected = Read-Host 'Enter the drive number to apply the .iso to' $DriveSelected = ($DriveSelected -as [int]) -1 - writelog "Drive $DriveSelected selected" + if($Last){ + 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' $var = $false } @@ -218,10 +196,10 @@ Function New-DeploymentUSB { } WriteLog "Setting the registry key to re-enable autoplay for all drives" if($DisableAutoPlay){ - Write-ProgressLog "Create Imaging Tool" "Enabling Autoplay" + Writelog "Enabling Autoplay" Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value 0 -Type DWORD } - Write-ProgressLog "Create Imaging Tool" "Completed!" + Writelog "Completed!" } #Get USB Drive and create log file if(Test-Path "$DevelopmentPath\Script.log"){ @@ -230,11 +208,8 @@ New-item -Path $DevelopmentPath -Name 'Script.log' -ItemType "file" -Force | Out } 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 +$USBDrives,$USBDrivesCount = Get-USBDrive New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount - read-host -Prompt "USB drive creation complete. Press ENTER to exit" Exit