Last active
March 23, 2025 16:05
-
-
Save fatihbahceci/8e4a207fe96d73d5a04361d7eb9f6206 to your computer and use it in GitHub Desktop.
Mount .vhdx file to a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Diagnostics | |
$EventSource = "Mount-Bin" | |
$EventId = 1; | |
Set-Location -Path $PSScriptRoot | |
$VhdPath = "$PSScriptRoot\Bin.vhdx" | |
$AccessPath = "D:\Bin" | |
if (-not ([EventLog]::SourceExists($EventSource))) { | |
New-EventLog -LogName "Application" -Source $EventSource | |
} | |
function Trace { | |
#Level: Error 1, Warning 2, Information 4, SuccessAudit 8, FailureAudit 16 | |
param ([string]$line,[EventLogEntryType]$Level = [EventLogEntryType]::Information) | |
# Write to Event Log | |
Write-EventLog -LogName Application -Source $EventSource -EntryType $Level -Message $line -EventId $EventId | |
$script:EventId = $EventId + 1 | |
if ($script:EventId -gt 65535) { | |
$script:EventId = 1 | |
} | |
# Write to Host | |
Write-Host $line | |
} | |
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
Trace "Script not run as administrator, restarting..." | |
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs | |
exit | |
} | |
Trace "Checking disk status for $VhdPath ..." | |
$Disk = Get-DiskImage -ImagePath $VhdPath -ErrorAction SilentlyContinue | |
if ($Disk) { | |
if ($Disk.Attached) { | |
Trace "VHD is already mounted. Proceeding with partition access path..." -Level Warning | |
} else { | |
Trace "(1) Attaching disk image $VhdPath..." -Level Warning | |
$Disk = Mount-DiskImage -ImagePath $VhdPath -NoDriveLetter -PassThru | |
} | |
} else { | |
Trace "(2) Attaching disk image $VhdPath..." -Level Warning | |
$Disk = Mount-DiskImage -ImagePath $VhdPath -NoDriveLetter -PassThru | |
} | |
Start-Sleep -Seconds 5 | |
Trace $Disk | |
if (-not $Disk) { | |
Trace "Still there is not mounted disk. Exiting" -Level Error | |
Exit 1 | |
} | |
$DiskNumber = ($Disk | Get-Disk).Number | |
# $DiskNumber = (Get-Disk | Where-Object { $_.Location -eq $VhdPath }).Number | |
$Partition = Get-Partition -DiskNumber $DiskNumber -ErrorAction SilentlyContinue | |
if (-not $Partition) { | |
Trace "Partition not found. Exiting..." -Level Error | |
Exit 2 | |
# Trace "Partition not found. Creating a new partition..." -Level Warning | |
# Initialize-Disk -Number $DiskNumber -PartitionStyle MBR | |
# $Partition = New-Partition -DiskNumber $DiskNumber -UseMaximumSize | |
# Format-Volume -Partition $Partition -FileSystem NTFS -Force | |
} | |
Trace "Checking $AccessPath is exists..." | |
if (!(Test-Path $AccessPath)) { | |
Trace "$AccessPath is not exists. Creating..." | |
New-Item -ItemType Directory -Path $AccessPath -Force | |
}else { | |
Trace "Access path already exists. $AccessPath" | |
} | |
$ExistingPaths = Get-Partition | Where-Object -Property AccessPaths -Contains "d:\Bin\" | |
#PS C:\WINDOWS\system32> Get-Partition | Where-Object -Property AccessPaths -Contains "d:\Bin\" | |
# | |
# | |
# DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000001#{53f56307-b6bf-11d0-94f2-00a0c91efb8b} | |
# | |
#PartitionNumber DriveLetter Offset Size Type | |
#--------------- ----------- ------ ---- ---- | |
#2 16777216 59.98 GB Basic | |
if ($ExistingPaths) { | |
Trace "Access path already assigned: $($ExistingPaths.PartitionNumber)" | |
} else { | |
Trace "Adding access path..." | |
#$Partition | Add-PartitionAccessPath -AccessPath $AccessPath | |
try { | |
$Success = $Partition | Add-PartitionAccessPath -AccessPath $AccessPath -ErrorAction Stop | |
if ($Success) { | |
Trace "Add-PArtitionAccessPath is success"; | |
} else { | |
Trace "Add-PArtitionAccessPath is failed" -Level Error; | |
Exit 3 | |
} | |
} catch { | |
Trace "Error adding partition access path: $_" | |
# Optionally, try removing and re-adding | |
Exit 4 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment