这里是使用内核的另一种选择。 Microsoft 建议不要这样做,但事实证明它非常有效地捕获跨区卷、挂载点和其他卷类型信息以帮助关联磁盘和卷。
$scriptBlockInvokeCommandKernel32 = {
param([array]$arrayOfVolumeSerial)
$cSharpCodeKernel32GetDisk = @"
using System;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Runtime.InteropServices;
public class GetDisk
{
private const uint IoctlVolumeGetVolumeDiskExtents = 0x560000;
private const uint HASMOREDATA = 234;
// 4 rounded to 8 + 8 + 8= 24
[StructLayout(LayoutKind.Sequential)]
public struct DiskExtent
{
public int DiskNumber;
public Int64 StartingOffset;
public Int64 ExtentLength;
}
//size 4 rounded to 8 + number of extends * 24
[StructLayout(LayoutKind.Sequential)]
public struct DiskExtents
{
public int numberOfExtents;
public DiskExtent extents;
}
//4
[StructLayout(LayoutKind.Sequential)]
public struct DiskExtentsBeforeArray
{
public int numberOfExtents;
}
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern SafeFileHandle CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile
);
[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint IoControlCode,
[MarshalAs(UnmanagedType.AsAny)] [In] object InBuffer,
uint nInBufferSize,
ref DiskExtents OutBuffer,
int nOutBufferSize,
ref uint pBytesReturned,
IntPtr Overlapped
);
[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint IoControlCode,
[MarshalAs(UnmanagedType.AsAny)] [In] object InBuffer,
uint nInBufferSize,
IntPtr outBuffer,
int nOutBufferSize,
ref uint pBytesReturned,
IntPtr Overlapped
);
[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
private static extern int GetLastError();
public static DiskExtent[] GetPhysicalDriveString(string path)
{
DiskExtent[] diskExtent = null;
path = path.TrimEnd('\\');
if (!path.StartsWith(@"\\.\"))
path = @"\\.\" + path;
SafeFileHandle safeFileHandle = CreateFile(path, FileAccess.Read, FileShare.Read | FileShare.Write, IntPtr.Zero, FileMode.Open, 0,
IntPtr.Zero);
if (safeFileHandle.IsInvalid)
{
Exception e = Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
}
uint bytesReturned = new uint();
DiskExtents VOLUME_DISK_EXTENTS = new DiskExtents();
bool result = DeviceIoControl(
safeFileHandle,
IoctlVolumeGetVolumeDiskExtents,
IntPtr.Zero,
0,
ref VOLUME_DISK_EXTENTS,
Marshal.SizeOf(VOLUME_DISK_EXTENTS),
ref bytesReturned,
IntPtr.Zero);
if (result)
{
diskExtent = new DiskExtent[1];
diskExtent[0] = VOLUME_DISK_EXTENTS.extents;
}
else
{
int numberOfExtents = VOLUME_DISK_EXTENTS.numberOfExtents;
int lastError = GetLastError();
if (lastError == HASMOREDATA)
{
int size = 8 + 24 * numberOfExtents;
uint bytesReturned2 = new uint();
IntPtr blob = default(IntPtr);
blob = Marshal.AllocHGlobal(size);
bool resultMutipleDiskExtent = DeviceIoControl(
safeFileHandle,
IoctlVolumeGetVolumeDiskExtents,
IntPtr.Zero,
0,
blob,
(int)size,
ref bytesReturned2,
IntPtr.Zero
);
if (resultMutipleDiskExtent)
{
DiskExtentsBeforeArray lie = default(DiskExtentsBeforeArray);
lie = (DiskExtentsBeforeArray)Marshal.PtrToStructure(blob, typeof(DiskExtentsBeforeArray));
diskExtent = new DiskExtent[lie.numberOfExtents];
for (int i = 0; i <= lie.numberOfExtents - 1; i++)
{
IntPtr offset = new IntPtr(blob.ToInt64() + 8 + 24 * i);
diskExtent[i] = (DiskExtent)Marshal.PtrToStructure(offset, typeof(DiskExtent));
}
}
}
else{
throw new System.ComponentModel.Win32Exception();
}
}
safeFileHandle.Close();
return diskExtent;
}
}
"@
$resultOfOperation =@{}
$volumeKernelInfo =@{}
$type = Add-Type -TypeDefinition $cSharpCodeKernel32GetDisk -ErrorAction Continue -ErrorVariable errorAddType -PassThru
if($errorAddType){
$resultOfOperation["error"]= ("error at CsharpBuild" + $errorAddType)
}
foreach($volumeSerial in $arrayOfVolumeSerial){
$volumeString = "Volume{" + $volumeSerial + "}"
$volumeKernelInfo[$volumeSerial] = [GetDisk]::GetPhysicalDriveString($volumeString)
}
$resultOfOperation["volumeKernelInfo"]=$volumeKernelInfo
return $resultOfOperation
}
你可以调用它:
$resultVolumeMappingFromKernel = Invoke-Command -ScriptBlock $scriptBlockInvokeCommandKernel32 -ComputerName $hostName -Credential $credential -ArgumentList (,$arrayOfVolumeSerial)
它需要在您想要使用管理员帐户的信息的远程机器上运行。