SHARE
TWEET

getpath from drive

jmsfer Feb 27th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  $driveLetter = Read-Host "Enter Drive Letter:"
  2.     Write-Host " "
  3.     $DynAssembly = New-Object System.Reflection.AssemblyName('SysUtils')
  4.     $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
  5.     $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SysUtils', $False)
  6.  
  7.     # Define [Kernel32]::QueryDosDevice method
  8.     $TypeBuilder = $ModuleBuilder.DefineType('Kernel32', 'Public, Class')
  9.     $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)
  10.     $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
  11.     $SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError')
  12.     $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('kernel32.dll'), [Reflection.FieldInfo[]]@($SetLastError), @($true))
  13.     $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
  14.     $Kernel32 = $TypeBuilder.CreateType()
  15.  
  16.     $Max = 65536
  17.     $StringBuilder = New-Object System.Text.StringBuilder($Max)
  18.     $ReturnLength = $Kernel32::QueryDosDevice($driveLetter, $StringBuilder, $Max)
  19.  
  20.      if ($ReturnLength)
  21.      {
  22.          Write-Host "Device Path: "$StringBuilder.ToString()
  23.       }
  24.       else
  25.       {
  26.           Write-Host "Device Path: not found"
  27.       }
  28.     Write-Host " "
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top