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
+51 -44
View File
@@ -1,10 +1,14 @@
[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
@@ -12,8 +16,19 @@ function WriteLog($LogText) {
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
} else { } else {
@@ -24,21 +39,23 @@ 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..."
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
@@ -95,11 +112,19 @@ function Build-DeploymentUSB {
[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 $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){ if($Drivers){
@@ -126,17 +151,17 @@ function Build-DeploymentUSB {
} }
} }
if($DrivesCount -gt 1){ if($DrivesCount -gt 1){
Writelog "Building $DrivesCount drives concurrently...Please be patient..." Write-ProgressLog "Create Imaging Tool" "Building $DrivesCount drives concurrently...Please be patient..."
} else { } else {
Writelog "Building the imaging tool on $model...Please be patient..." Write-ProgressLog "Create Imaging Tool" "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..." 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,
@@ -165,22 +190,15 @@ 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
if ($Last) {
writelog "All drives selected"
}
else {
writelog "Drive $DriveSelected selected" 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
if ($DisableAutoPlay -and $DisableAutoPlayCurrentSetting -ne 1) {
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
} }
@@ -189,7 +207,7 @@ function New-DeploymentUSB {
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
@@ -199,39 +217,28 @@ 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){
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!"
} }
$Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator'
# Check if path is relative
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)"
}
if ($DeployISOPath.Exists) {
$DevelopmentPath = $DeployISOPath.Directory.FullName
#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"){
Remove-Item -Path "$DevelopmentPath\Script.log" -Force -Confirm:$false Remove-Item -Path "$DevelopmentPath\Script.log" -Force -Confirm:$false
New-item -Path $DevelopmentPath -Name 'Script.log' -ItemType "file" -Force | Out-Null New-item -Path $DevelopmentPath -Name 'Script.log' -ItemType "file" -Force | Out-Null
} }
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
} 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
} }