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){
$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"}
Function Get-USBDrive {
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
If ($USBDrives -and ($null -eq $USBDrives.count)) {
$USBDrivesCount = 1
}
@@ -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
}
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,25 +91,17 @@ $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
}
}
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
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $FFUPath, $Destination | Out-Null
}
}
if($Drivers){
@@ -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 {
@@ -191,9 +167,11 @@ 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 {
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