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
+156 -142
View File
@@ -1,25 +1,22 @@
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $True, Position = 0)] [Parameter(Mandatory = $True, Position = 0)]
$DeployISOPath, [io.fileinfo] $DeployISOPath,
[Switch]$DisableAutoPlay [Switch]$DisableAutoPlay
) )
$Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator'
if($DeployISOPath){
$DevelopmentPath = $DeployISOPath | Split-Path
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 Get-USBDrive {
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'") $USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
If ($USBDrives -and ($null -eq $USBDrives.count)) { If ($USBDrives -and ($null -eq $USBDrives.count)) {
$USBDrivesCount = 1 $USBDrivesCount = 1
} } else {
else {
$USBDrivesCount = $USBDrives.Count $USBDrivesCount = $USBDrives.Count
} }
WriteLog "Found $USBDrivesCount USB drives" WriteLog "Found $USBDrivesCount USB drives"
@@ -31,26 +28,27 @@ Function Get-USBDrive {
} }
return $USBDrives, $USBDrivesCount return $USBDrives, $USBDrivesCount
} }
Function Build-DeploymentUSB{
param( function Build-DeploymentUSB {
[Array]$Drives param(
) [Array]$Drives
writelog "Checking if ffu files are present in the ffu folder" )
$Images = Get-ChildItem -Path $FFUPath -Filter "*.ffu" -File -Recurse writelog "Checking if ffu files are present in the ffu folder"
writelog "Checking if drivers are present in the drivers folder" $Images = Get-ChildItem -Path $FFUPath -Filter "*.ffu" -File -Recurse
$Drivers = Get-ChildItem -Path $DriversPath -Recurse writelog "Checking if drivers are present in the drivers folder"
$DrivesCount = $Drives.Count $Drivers = Get-ChildItem -Path $DriversPath -Recurse
Writelog "Creating partitions..." $DrivesCount = $Drives.Count
foreach ($USBDrive in $Drives) { Writelog "Creating partitions..."
$DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "") foreach ($USBDrive in $Drives) {
$Model = $USBDrive.model $DriveNumber = $USBDrive.DeviceID.Replace("\\.\PHYSICALDRIVE", "")
$ScriptBlock = { $Model = $USBDrive.model
$ScriptBlock = {
param($DriveNumber) param($DriveNumber)
Clear-Disk -Number $DriveNumber -RemoveData -RemoveOEM -Confirm:$false Clear-Disk -Number $DriveNumber -RemoveData -RemoveOEM -Confirm:$false
$Disk = Get-Disk -Number $DriveNumber $Disk = Get-Disk -Number $DriveNumber
$PartitionStyle = $Disk.PartitionStyle $PartitionStyle = $Disk.PartitionStyle
if($PartitionStyle -ne 'MBR'){ if ($PartitionStyle -ne 'MBR') {
$Disk | Set-Disk -PartitionStyle MBR $Disk | Set-Disk -PartitionStyle MBR
} }
$BootPartition = New-Partition -DiskNumber $DriveNumber -Size 2GB -IsActive -AssignDriveLetter $BootPartition = New-Partition -DiskNumber $DriveNumber -Size 2GB -IsActive -AssignDriveLetter
$DeployPartition = New-Partition -DiskNumber $DriveNumber -UseMaximumSize -AssignDriveLetter $DeployPartition = New-Partition -DiskNumber $DriveNumber -UseMaximumSize -AssignDriveLetter
@@ -62,83 +60,83 @@ Function Build-DeploymentUSB{
} }
writelog "Wait for partitioning jobs to complete" writelog "Wait for partitioning jobs to complete"
Get-Job | Wait-Job | Out-Null Get-Job | Wait-Job | Out-Null
if($DrivesCount -gt 1){ if ($DrivesCount -gt 1) {
writelog "Get file system information for all drives" writelog "Get file system information for all drives"
$Partitions = Get-Partition | Get-Volume $Partitions = Get-Partition | Get-Volume
}else{ } else {
writelog "Get file system information for drive number $DiskNumber" writelog "Get file system information for drive number $DiskNumber"
$Partitions = Get-Partition -DiskNumber $DriveNumber | Get-Volume $Partitions = Get-Partition -DiskNumber $DriveNumber | Get-Volume
} }
writelog "Get drive letter for all volumes labeled:BOOT" writelog "Get drive letter for all volumes labeled:BOOT"
$BootDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "BOOT"}).DriveLetter $BootDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "BOOT" }).DriveLetter
writelog "Get drive letter for all volumes labeled:Deploy" writelog "Get drive letter for all volumes labeled:Deploy"
$DeployDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "Deploy"}).DriveLetter $DeployDrives = ($Partitions | Where-Object { $_.FileSystemLabel -eq "Deploy" }).DriveLetter
writelog "Mount Deployment .iso image" writelog "Mount Deployment .iso image"
$ISOMountPoint = (Mount-DiskImage -ImagePath "$DeployISOPath" -PassThru | Get-Volume).DriveLetter + ":\" $ISOMountPoint = (Mount-DiskImage -ImagePath "$DeployISOPath" -PassThru | Get-Volume).DriveLetter + ":\"
writelog "Copying boot files to all drives labeled BOOT concurrently" writelog "Copying boot files to all drives labeled BOOT concurrently"
foreach ($Drive in $BootDrives) { foreach ($Drive in $BootDrives) {
$Destination = $Drive + ":\" $Destination = $Drive + ":\"
$jobScriptBlock = { $jobScriptBlock = {
param ( param (
[string]$SFolder, [string]$SFolder,
[string]$DFolder [string]$DFolder
) )
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 boot files to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $ISOMountPoint, $Destination | Out-Null
}
if($Images){
writelog "Copying FFU image files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\"
$jobScriptBlock = {
param (
[string]$SFolder,
[string]$DFolder
)
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
}
}
if($Drivers){
writelog "Copying driver files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\Drivers"
$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 drivers to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $DriversPath, $Destination | Out-Null
}
}
if(!($Drivers)){
foreach ($Drive in $DeployDrives) {
WriteLog "Create drivers directory"
$drivepath = $Drive + ":\"
New-Item -Path "$drivepath" -Name Drivers -ItemType Directory -Force -Confirm: $false | Out-Null
} }
} WriteLog "Start job to copy all boot files to $Destination"
if($DrivesCount -gt 1){ Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $ISOMountPoint, $Destination | Out-Null
Writelog "Building $DrivesCount drives concurrently...Please be patient..." }
}else{ if ($Images) {
Writelog "Building the imaging tool on $model...Please be patient..." writelog "Copying FFU image files to all drives labeled deploy concurrently"
} foreach ($Drive in $DeployDrives) {
Get-Job | Wait-Job | Out-Null $Destination = $Drive + ":\"
$jobScriptBlock = {
param (
[string]$SFolder,
[string]$DFolder
)
Robocopy $SFolder $DFolder /E /COPYALL /R:5 /W:5 /J
}
Dismount-DiskImage -ImagePath $DeployISOPath | Out-Null WriteLog "Start job to copy all FFU files to $Destination"
Writelog "Drive creation jobs completed..." Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $FFUPath, $Destination | Out-Null
}
}
if ($Drivers) {
writelog "Copying driver files to all drives labeled deploy concurrently"
foreach ($Drive in $DeployDrives) {
$Destination = $Drive + ":\Drivers"
$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 drivers to $Destination"
Start-Job -ScriptBlock $jobScriptBlock -ArgumentList $DriversPath, $Destination | Out-Null
}
}
if (!($Drivers)) {
foreach ($Drive in $DeployDrives) {
WriteLog "Create drivers directory"
$drivepath = $Drive + ":\"
New-Item -Path "$drivepath" -Name Drivers -ItemType Directory -Force -Confirm: $false | Out-Null
}
}
if ($DrivesCount -gt 1) {
Writelog "Building $DrivesCount drives concurrently...Please be patient..."
} else {
Writelog "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..."
} }
Function New-DeploymentUSB { function New-DeploymentUSB {
param( param(
[Array]$Drives, [Array]$Drives,
[int]$Count, [int]$Count,
@@ -149,75 +147,91 @@ Function New-DeploymentUSB {
$Drivelist = @() $Drivelist = @()
writelog "Creating a USB drive selection list" writelog "Creating a USB drive selection list"
for($i=0;$i -le $Count -1;$i++){ for ($i = 0; $i -le $Count - 1; $i++) {
$DriveModel = $Drives[$i].Model $DriveModel = $Drives[$i].Model
$DriveSize = [math]::round($Drives[$i].size/1GB, 2) $DriveSize = [math]::round($Drives[$i].size / 1GB, 2)
$DiskNumber = $Drives[$i].DeviceID.Replace("\\.\PHYSICALDRIVE", "") $DiskNumber = $Drives[$i].DeviceID.Replace("\\.\PHYSICALDRIVE", "")
$Properties = [ordered]@{Number = $i + 1 ; DriveNumber = $DiskNumber ; DriveModel = $driveModel ; 'Size (GB)' = $DriveSize} $Properties = [ordered]@{Number = $i + 1 ; DriveNumber = $DiskNumber ; DriveModel = $driveModel ; 'Size (GB)' = $DriveSize }
$Drivelist += New-Object PSObject -Property $Properties $Drivelist += New-Object PSObject -Property $Properties
} }
if($Count -gt 1){ if ($Count -gt 1) {
$Last = $Count+1 $Last = $Count + 1
$Drivelist += New-Object -TypeName PSObject -Property @{ Number = "$last"; DriveModel = "Select this option to use all ($count) inserted USB Drives" } $Drivelist += New-Object -TypeName PSObject -Property @{ Number = "$last"; DriveModel = "Select this option to use all ($count) inserted USB Drives" }
} }
$Drivelist | Format-Table -AutoSize -Property Number, DriveModel , 'Size (GB)' $Drivelist | Format-Table -AutoSize -Property Number, DriveModel , 'Size (GB)'
do { do {
try { try {
$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){ if ($Last) {
writelog "All drives selected" writelog "All drives selected"
}else{
writelog "Drive $DriveSelected selected"}
} }
catch { else {
writelog "Drive $DriveSelected selected"
}
}
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)
$DisableAutoPlayCurrentSetting = (Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name DisableAutoplay).DisableAutoplay $DisableAutoPlayCurrentSetting = (Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers" -Name DisableAutoplay).DisableAutoplay
if($DisableAutoPlay -and $DisableAutoPlayCurrentSetting -ne 1){ if ($DisableAutoPlay -and $DisableAutoPlayCurrentSetting -ne 1) {
writelog "Disable autoPlay current setting is $DisableAutoPlayCurrentSetting" 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
} }
WriteLog "Closing all MMC windows to prevent drive lock errors" WriteLog "Closing all MMC windows to prevent drive lock errors"
Stop-Process -Name mmc -ErrorAction SilentlyContinue Stop-Process -Name mmc -ErrorAction SilentlyContinue
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
}else{ } else {
Read-Host -Prompt "Drive number $Selection was selected. Press ENTER to continue" Read-Host -Prompt "Drive number $Selection was selected. Press ENTER to continue"
Build-DeploymentUSB -Drives $Drives[$DriveSelected] Build-DeploymentUSB -Drives $Drives[$DriveSelected]
} }
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" Writelog "Setting disable autoplay setting back to $DisableAutoPlayCurrentSetting"
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 $DisableAutoPlayCurrentSetting -Type DWORD
} }
Writelog "Completed!" Writelog "Completed!"
} }
#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"
Exit $Host.UI.RawUI.WindowTitle = 'USB Imaging Tool Creator'
}else{
Write-Host "No .ISO file selected..." # Check if path is relative
read-host "Press ENTER to Exit..." if (-not [System.IO.Path]::IsPathRooted($DeployISOPath)) {
Exit # 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"
Exit
} else {
Write-Error "Unable to locate ISO file at: $($DeployISOPath.FullName)"
} }