PoshCode Logo PowerShell Code Repository

Get-DevicePath by Matt Graeber 32 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/4768"></script>download | new post

Returns the corresponding device path for each drive letter

  1. function Get-DevicePath
  2. {
  3. <#
  4. .SYNOPSIS
  5.  
  6.     Returns the device paths for each volume.
  7.  
  8.     Author: Matthew Graeber (@mattifestation)
  9.     License: BSD 3-Clause
  10.  
  11. .DESCRIPTION
  12.  
  13.     Get-DevicePath returns the corresponding device path for each drive letter. This is useful for converting device paths to drive letters.
  14.  
  15. .EXAMPLE
  16.  
  17.     Get-DevicePath
  18.  
  19.     DevicePath              DriveLetter
  20.     ----------              -----------
  21.     \Device\HarddiskVolume2 D:
  22.     \Device\HarddiskVolume4 C:
  23.  
  24. .OUTPUTS
  25.  
  26.     PSObject[]
  27.  
  28.     For each mount point, a PSObject is returned representing the drive letter and device path.
  29. #>
  30.  
  31.     # Utilize P/Invoke in order to call QueryDosDevice. I prefer using
  32.     # reflection over Add-Type since it doesn't require compiling C# code.
  33.     $DynAssembly = New-Object System.Reflection.AssemblyName('SysUtils')
  34.     $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
  35.     $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SysUtils', $False)
  36.  
  37.     # Define [Kernel32]::QueryDosDevice method
  38.     $TypeBuilder = $ModuleBuilder.DefineType('Kernel32', 'Public, Class')
  39.     $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod('QueryDosDevice', 'kernel32.dll', ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [UInt32], [Type[]]@([String], [Text.StringBuilder], [UInt32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
  40.     $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
  41.     $SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError')
  42.     $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('kernel32.dll'), [Reflection.FieldInfo[]]@($SetLastError), @($true))
  43.     $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
  44.     $Kernel32 = $TypeBuilder.CreateType()
  45.  
  46.     $Max = 65536
  47.     $StringBuilder = New-Object System.Text.StringBuilder($Max)
  48.  
  49.     Get-WmiObject Win32_Volume | ? { $_.DriveLetter } | % {
  50.         $ReturnLength = $Kernel32::QueryDosDevice($_.DriveLetter, $StringBuilder, $Max)
  51.  
  52.         if ($ReturnLength)
  53.         {
  54.             $DriveMapping = @{
  55.                 DriveLetter = $_.DriveLetter
  56.                 DevicePath = $StringBuilder.ToString()
  57.             }
  58.  
  59.             New-Object PSObject -Property $DriveMapping
  60.         }
  61.     }
  62. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me