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()]
param(
[Parameter(Mandatory = $True, Position = 0)]
[io.fileinfo] $DeployISOPath,
$DeployISOPath,
[Switch]$DisableAutoPlay
)
$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
@@ -12,8 +16,19 @@ function WriteLog($LogText) {
Write-Verbose $LogText
}
function Get-USBDrive {
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
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)) {
$USBDrivesCount = 1
} else {
@@ -24,21 +39,23 @@ function Get-USBDrive {
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 {
Function Build-DeploymentUSB{
param(
[Array]$Drives
)
writelog "Checking if ffu files are present in the ffu folder"
$Images = Get-ChildItem -Path $FFUPath -Filter "*.ffu" -File -Recurse
writelog "Creating list of FFU image files"
$Images = Get-ChildItem -Path $ImagesPath -Filter "*.ffu" -File -Recurse
writelog "Checking if drivers are present in the drivers folder"
$Drivers = Get-ChildItem -Path $DriversPath -Recurse
$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) {
$DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "")
$Model = $USBDrive.model
@@ -95,11 +112,19 @@ function Build-DeploymentUSB {
[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 $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){
@@ -126,17 +151,17 @@ function Build-DeploymentUSB {
}
}
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 {
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
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(
[Array]$Drives,
[int]$Count,
@@ -165,22 +190,15 @@ function New-DeploymentUSB {
$var = $true
$DriveSelected = Read-Host 'Enter the drive number to apply the .iso to'
$DriveSelected = ($DriveSelected -as [int]) -1
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
}
} until (($DriveSelected -le $Count -1 -or $last) -and $var)
$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"
if($DisableAutoPlay){
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
}
@@ -189,7 +207,7 @@ function New-DeploymentUSB {
WriteLog "Closing all Diskpart windows to prevent drive lock errors"
Stop-Process -Name diskpart -ErrorAction SilentlyContinue
$Selection = $Drivelist[$DriveSelected].Number
$totalSteps = 5
if($Selection -eq $last){
Read-Host -Prompt "ALL DRIVES SELECTED! WILL ERASE ALL CURRENTLY CONNECTED USB DRIVES!! Press ENTER to continue"
Build-DeploymentUSB -Drives $Drives
@@ -199,39 +217,28 @@ function New-DeploymentUSB {
}
WriteLog "Setting the registry key to re-enable autoplay for all drives"
if($DisableAutoPlay){
Writelog "Setting disable autoplay setting back to $DisableAutoPlayCurrentSetting"
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name "DisableAutoplay" -Value $DisableAutoPlayCurrentSetting -Type DWORD
Write-ProgressLog "Create Imaging Tool" "Enabling Autoplay"
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
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
$USBDrives,$USBDrivesCount = Get-RemovableDrive
WriteLog 'Setting first step for percentage progress bar'
$currentStep = 1
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
} else {
Write-Error "Unable to locate ISO file at: $($DeployISOPath.FullName)"
Write-Host "No .ISO file selected..."
read-host "Press ENTER to Exit..."
Exit
}