From c68ea840ba9b79d9c89499b358c7fbfbf5e4065c Mon Sep 17 00:00:00 2001 From: Ingvarr100th Date: Fri, 30 Aug 2024 00:42:12 +0200 Subject: [PATCH 1/6] BitLocker support and ability to use alredy mounted drives --- Update-VMGpuPartitionDriver.ps1 | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index 157b22b..2e87c48 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -1,4 +1,4 @@ -<# +<# If you are opening this file in Powershell ISE you should modify the params section like so... Remember: GPU Name must match the name of the GPU you assigned when creating the VM... @@ -13,30 +13,45 @@ Param ( Param ( [string]$VMName, [string]$GPUName, +[string]$BitLockerKey, [string]$Hostname = $ENV:Computername ) +$ErrorActionPreference = "Stop" + Import-Module $PSSCriptRoot\Add-VMGpuPartitionAdapterFiles.psm1 $VM = Get-VM -VMName $VMName $VHD = Get-VHD -VMId $VM.VMId If ($VM.state -eq "Running") { - [bool]$state_was_running = $true - } + [bool]$state_was_running = $True +} if ($VM.state -ne "Off"){ "Attemping to shutdown VM..." Stop-VM -Name $VMName -Force - } +} While ($VM.State -ne "Off") { Start-Sleep -s 3 "Waiting for VM to shutdown - make sure there are no unsaved documents..." - } +} -"Mounting Drive..." -$DriveLetter = (Mount-VHD -Path $VHD.Path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) +if ($VHD.Attached -eq $False) { + "Mounting Drive..." + $DriveLetter = (Mount-VHD -Path $VHD.Path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) +} else { + $DriveLetter = ($VHD | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) +} + +$BitLockerStatus = Get-BitLockerVolume $DriveLetter +if($BitLockerStatus.LockStatus) { + if([string]::IsNullOrWhiteSpace($BitLockerKey)){ + $BitLockerKey = Read-Host "Enter BitLockerKey for drive $DriveLetter" + } + Unlock-BitLocker -MountPoint $DriveLetter -RecoveryPassword $BitLockerKey +} "Copying GPU Files - this could take a while..." Add-VMGPUPartitionAdapterFiles -hostname $Hostname -DriveLetter $DriveLetter -GPUName $GPUName @@ -44,9 +59,9 @@ Add-VMGPUPartitionAdapterFiles -hostname $Hostname -DriveLetter $DriveLetter -GP "Dismounting Drive..." Dismount-VHD -Path $VHD.Path -If ($state_was_running){ +If ($state_was_running) { "Previous State was running so starting VM..." Start-VM $VMName - } +} "Done..." \ No newline at end of file From 4203a04d210ab51239bfbeee75423f193a21381e Mon Sep 17 00:00:00 2001 From: Ingvarr100th Date: Fri, 30 Aug 2024 01:06:33 +0200 Subject: [PATCH 2/6] Formating fixes and more informative outputs --- Update-VMGpuPartitionDriver.ps1 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index 2e87c48..27ed8e1 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -11,10 +11,10 @@ Param ( #> Param ( -[string]$VMName, -[string]$GPUName, -[string]$BitLockerKey, -[string]$Hostname = $ENV:Computername + [string]$VMName, + [string]$GPUName, + [string]$BitLockerKey, + [string]$Hostname = $ENV:Computername ) $ErrorActionPreference = "Stop" @@ -28,7 +28,7 @@ If ($VM.state -eq "Running") { [bool]$state_was_running = $True } -if ($VM.state -ne "Off"){ +if ($VM.state -ne "Off") { "Attemping to shutdown VM..." Stop-VM -Name $VMName -Force } @@ -40,15 +40,17 @@ While ($VM.State -ne "Off") { if ($VHD.Attached -eq $False) { "Mounting Drive..." - $DriveLetter = (Mount-VHD -Path $VHD.Path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) -} else { - $DriveLetter = ($VHD | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) + $DriveLetter = (Mount-VHD -Path $VHD.Path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object { $_.DriveLetter } | ForEach-Object DriveLetter) +} +else { + $DriveLetter = ($VHD | Get-Disk | Get-Partition | Get-Volume | Where-Object { $_.DriveLetter } | ForEach-Object DriveLetter) + "Using already mounted drive ${DriveLetter}:" } $BitLockerStatus = Get-BitLockerVolume $DriveLetter -if($BitLockerStatus.LockStatus) { - if([string]::IsNullOrWhiteSpace($BitLockerKey)){ - $BitLockerKey = Read-Host "Enter BitLockerKey for drive $DriveLetter" +if ($BitLockerStatus.LockStatus) { + if ([string]::IsNullOrWhiteSpace($BitLockerKey)) { + $BitLockerKey = Read-Host "Enter BitLocker Key for drive ${DriveLetter}:" } Unlock-BitLocker -MountPoint $DriveLetter -RecoveryPassword $BitLockerKey } From 036ba8753d0e82b6acf1d6f308578485df62fdc3 Mon Sep 17 00:00:00 2001 From: Ingvarr100th Date: Fri, 30 Aug 2024 01:10:56 +0200 Subject: [PATCH 3/6] Param example --- Update-VMGpuPartitionDriver.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index 27ed8e1..e3ae0ff 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -5,6 +5,7 @@ Remember: GPU Name must match the name of the GPU you assigned when creating the Param ( [string]$VMName = "NameofyourVM", [string]$GPUName = "NameofyourGPU", +[string]$BitLockerKey = "XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX", [string]$Hostname = $ENV:Computername ) From 1b7f01935c1122dcb232ca05ae761b7b22230170 Mon Sep 17 00:00:00 2001 From: Ingvarr100th Date: Wed, 23 Oct 2024 17:23:52 +0200 Subject: [PATCH 4/6] Get the VHD state after shutting down the VM --- Update-VMGpuPartitionDriver.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index e3ae0ff..f66d248 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -32,6 +32,7 @@ If ($VM.state -eq "Running") { if ($VM.state -ne "Off") { "Attemping to shutdown VM..." Stop-VM -Name $VMName -Force + $VHD = Get-VHD -VMId $VM.VMId } While ($VM.State -ne "Off") { From c27020906081cb789cd903979216fcbb7e109567 Mon Sep 17 00:00:00 2001 From: Ingvarr100th Date: Wed, 23 Oct 2024 17:25:44 +0200 Subject: [PATCH 5/6] Removal of unnecessary colons --- Update-VMGpuPartitionDriver.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index f66d248..76184aa 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -46,13 +46,13 @@ if ($VHD.Attached -eq $False) { } else { $DriveLetter = ($VHD | Get-Disk | Get-Partition | Get-Volume | Where-Object { $_.DriveLetter } | ForEach-Object DriveLetter) - "Using already mounted drive ${DriveLetter}:" + "Using already mounted drive ${DriveLetter}" } $BitLockerStatus = Get-BitLockerVolume $DriveLetter if ($BitLockerStatus.LockStatus) { if ([string]::IsNullOrWhiteSpace($BitLockerKey)) { - $BitLockerKey = Read-Host "Enter BitLocker Key for drive ${DriveLetter}:" + $BitLockerKey = Read-Host "Enter BitLocker Key for drive ${DriveLetter}" } Unlock-BitLocker -MountPoint $DriveLetter -RecoveryPassword $BitLockerKey } From 4c7f27ee42118bff7318be529ba7b35942e10dd4 Mon Sep 17 00:00:00 2001 From: Ingvarr100th Date: Tue, 20 May 2025 13:03:48 +0200 Subject: [PATCH 6/6] add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6766e3d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.* \ No newline at end of file