Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $driveLetter = Read-Host "Enter Drive Letter:"
- Write-Host " "
- $DynAssembly = New-Object System.Reflection.AssemblyName('SysUtils')
- $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
- $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SysUtils', $False)
- # Define [Kernel32]::QueryDosDevice method
- $TypeBuilder = $ModuleBuilder.DefineType('Kernel32', 'Public, Class')
- $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)
- $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
- $SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError')
- $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('kernel32.dll'), [Reflection.FieldInfo[]]@($SetLastError), @($true))
- $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
- $Kernel32 = $TypeBuilder.CreateType()
- $Max = 65536
- $StringBuilder = New-Object System.Text.StringBuilder($Max)
- $ReturnLength = $Kernel32::QueryDosDevice($driveLetter, $StringBuilder, $Max)
- if ($ReturnLength)
- {
- Write-Host "Device Path: "$StringBuilder.ToString()
- }
- else
- {
- Write-Host "Device Path: not found"
- }
- Write-Host " "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement