Reputation: 11
I have a script that maps files in order to run VHDs. But the drive letter can change when you run it from one machine to another. How can I prompt the user which drive letter has the folder? or determine which drive letter has \Program Files\Microsoft Learning\20414\Drives\?
Actual Script Below:
Set-VHD -Path "D:\Program Files\Microsoft Learning\20414\Drives\20414B-LON-DC1\Virtual Hard Disks\20414B-LON-DC1.vhd” -ParentPath "D:\Program Files\Microsoft Learning\Base\Drives\MT12-WS12-LON-DC1-TMP.vhd”
Upvotes: 1
Views: 2198
Reputation: 294177
This is what I use to get the drive of a mounted VHD:
Write-Output "Mount-VHD $targetVhdx..."
$mountVhd = Mount-VHD -Path $targetVhdx -Passthru
Write-Output "Select mounted DriveLetter..."
$mountDrive = $mountVhd | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.FileSystemLabel -ne 'System Reserved'} | Select DriveLetter
Upvotes: 0
Reputation: 200193
I'd use a slightly different approach from what vonPryz suggested, because Get-PSDrive
will enumerate more than just disk/network drives. Using WMI should provide slightly better performance:
$subfolder = "Program Files\Microsoft Learning\20414\Drives"
$drivesPath = gwmi Win32_LogicalDisk -Filter 'DriveType=3 OR DriveType=4' | % {
Join-Path $_.DeviceID $subfolder
} | ? { Test-Path -LiteralPath $_ }
Upvotes: 1
Reputation: 24071
Use Read-Host
to prompt for user input. Like so,
$vhdLocation = read-host "Enter path for VHD file"
Instead of prompting user, you could list all the drives and check if the directory exists with Get-PSDrive
, Test-Path
and Join-Path
. Like so,
get-psdrive | ? {
$_.root -match "[c-z]:\\" -and (test-path $(join-path $_.root "Program Files\Microsoft Learning\20414\Drives\"))
}
$_.root -match "[c-z]:\\"
will match drive letters C: to Z:.
$(join-path $_.root "Program Files\Microsoft Learning\20414\Drives\")
will create a valid syntax for path. That is, it will manage delimiters automatically.
test-path
will return true if the path does exist.
Upvotes: 3