Update BuildUSBDrives.ps1

Update code
This commit is contained in:
Mike Kelly
2024-07-09 11:29:58 -04:00
committed by GitHub
parent 174c16ecb6
commit 67cc8c1225
+16 -41
View File
@@ -8,27 +8,14 @@ $Host.UI.RawUI.WindowTitle = 'Imaging Tool USB Creator'
if($DeployISOPath){ if($DeployISOPath){
$DevelopmentPath = $DeployISOPath | Split-Path $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(
[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)) { If ($USBDrives -and ($null -eq $USBDrives.count)) {
$USBDrivesCount = 1 $USBDrivesCount = 1
} }
@@ -40,23 +27,20 @@ If($USBDrives -and ($null -eq $USBDrives.count)) {
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 "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 $Images = Get-ChildItem -Path $FFUPath -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
Write-ProgressLog "Create Imaging Tool" "Creating partitions..." Writelog "Creating partitions..."
writelog "Create job to partition each usb drive"
foreach ($USBDrive in $Drives) { foreach ($USBDrive in $Drives) {
$DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "") $DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "")
$Model = $USBDrive.model $Model = $USBDrive.model
@@ -107,25 +91,17 @@ $Destination = $Drive + ":\"
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 + ":\Images" $Destination = $Drive + ":\"
$jobScriptBlock = { $jobScriptBlock = {
param ( param (
[string]$SFolder, [string]$SFolder,
[string]$DFolder [string]$DFolder
) )
New-Item -Path $DFolder -ItemType Directory -Force -Confirm: $false | Out-Null
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 FFU files to $Destination" 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){ if($Drivers){
@@ -152,14 +128,14 @@ if(!($Drivers)){
} }
} }
if($DrivesCount -gt 1){ 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{ }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 Get-Job | Wait-Job | Out-Null
Dismount-DiskImage -ImagePath $DeployISOPath | 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 { Function New-DeploymentUSB {
@@ -191,9 +167,11 @@ Function New-DeploymentUSB {
$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
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' Write-Host 'Input was not in correct format. Please enter a valid FFU number'
$var = $false $var = $false
@@ -218,10 +196,10 @@ Function New-DeploymentUSB {
} }
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){
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 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 #Get USB Drive and create log file
if(Test-Path "$DevelopmentPath\Script.log"){ 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 'Begin Logging'
WriteLog 'Getting USB drive information and usb drive count' WriteLog 'Getting USB drive information and usb drive count'
$USBDrives,$USBDrivesCount = Get-RemovableDrive $USBDrives,$USBDrivesCount = Get-USBDrive
WriteLog 'Setting first step for percentage progress bar'
$currentStep = 1
New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount
read-host -Prompt "USB drive creation complete. Press ENTER to exit" read-host -Prompt "USB drive creation complete. Press ENTER to exit"
Exit Exit