Merge pull request #74 from w0/main

Added support for relative paths
This commit is contained in:
rbalsleyMSFT
2024-09-12 12:32:22 -07:00
committed by GitHub
+32 -18
View File
@@ -1,25 +1,22 @@
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, Position = 0)]
$DeployISOPath,
[io.fileinfo] $DeployISOPath,
[Switch]$DisableAutoPlay
)
$Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator'
if($DeployISOPath){
$DevelopmentPath = $DeployISOPath | Split-Path
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 Get-USBDrive {
function Get-USBDrive {
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
If ($USBDrives -and ($null -eq $USBDrives.count)) {
$USBDrivesCount = 1
}
else {
} else {
$USBDrivesCount = $USBDrives.Count
}
WriteLog "Found $USBDrivesCount USB drives"
@@ -31,7 +28,8 @@ Function Get-USBDrive {
}
return $USBDrives, $USBDrivesCount
}
Function Build-DeploymentUSB{
function Build-DeploymentUSB {
param(
[Array]$Drives
)
@@ -138,7 +136,7 @@ Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null
Writelog "Drive creation jobs completed..."
}
Function New-DeploymentUSB {
function New-DeploymentUSB {
param(
[Array]$Drives,
[int]$Count,
@@ -169,8 +167,10 @@ Function New-DeploymentUSB {
$DriveSelected = ($DriveSelected -as [int]) - 1
if ($Last) {
writelog "All drives selected"
}else{
writelog "Drive $DriveSelected selected"}
}
else {
writelog "Drive $DriveSelected selected"
}
}
catch {
Write-Host 'Input was not in correct format. Please enter a valid FFU number'
@@ -189,7 +189,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
@@ -204,20 +204,34 @@ Function New-DeploymentUSB {
}
Writelog "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
New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount
read-host -Prompt "USB drive creation complete. Press ENTER to exit"
$USBDrives, $USBDrivesCount = Get-USBDrive
New-DeploymentUSB -Drives $USBDrives -Count $USBDrivesCount
Read-Host -Prompt "USB drive creation complete. Press ENTER to exit"
Exit
} else {
Write-Host "No .ISO file selected..."
read-host "Press ENTER to Exit..."
Exit
Write-Error "Unable to locate ISO file at: $($DeployISOPath.FullName)"
}