从驱动器号获取磁盘序列号

时间:2017-12-01 09:37:57

标签: powershell

我想获取序列号,就像从PowerShell Get-Disk cmdlet返回的序列号一样,与Windows卷的驱动器号相关联。我正在努力寻找任何可以指明我如何做到这一点的正确方向的东西。我可以看到人们已经设法通过从vbscript和c#调用的WMI查询在网站上这样做,例如

private string GetDiskIndex(string driveLetter)
    {
        driveLetter = driveLetter.TrimEnd('\\');

        ManagementScope scope = new ManagementScope(@"\root\cimv2");
        var drives = new ManagementObjectSearcher(scope, new ObjectQuery("select * from Win32_DiskDrive")).Get();
        foreach(var drive in drives)
        {

            var partitions = new ManagementObjectSearcher(scope, new ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")).Get();
            foreach(var partition in partitions)
            {
                var logicalDisks = new ManagementObjectSearcher(scope, new ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition")).Get();
                foreach (var logicalDisk in logicalDisks)
                {
                    if (logicalDisk["DeviceId"].ToString() == driveLetter) return partition["DiskIndex"].ToString();
                }
            }

        }

        return null;
    }

在PowerShell中有一种优雅的方法吗?

4 个答案:

答案 0 :(得分:2)

WMI / CIM的东西也可以用powershell完成。但你也可以这样做:

git pull

答案 1 :(得分:1)

使用CIM cmdlet复制代码非常简单:

$DriveLetter = 'C:'

Get-CimInstance -ClassName Win32_DiskDrive |
    Get-CimAssociatedInstance -Association Win32_DiskDriveToDiskPartition |
    Get-CimAssociatedInstance -Association Win32_LogicalDiskToPartition |
    Where-Object DeviceId -eq $DriveLetter |
    Get-CimAssociatedInstance -Association Win32_LogicalDiskToPartition |
    Select-Object -Property DiskIndex

如果您只想要基本无符号整数值DiskIndex,请改用-ExpandProperty DiskIndex

如果您的Windows版本没有Get-PartitionGet-Drive,但您确实拥有PowerShell v3.0 +,则此功能非常有用。

您的代码说您想要DiskIndex,这就是上面的代码所做的。但是,您的问题表明您想要SerialNumber。这是如何实现的:

Get-CimInstance -ClassName Win32_DiskDrive |
    Get-CimAssociatedInstance -Association Win32_DiskDriveToDiskPartition |
    Get-CimAssociatedInstance -Association Win32_LogicalDiskToPartition |
    Where-Object DeviceId -eq $DriveLetter |
    Get-CimAssociatedInstance -Association Win32_LogicalDiskToPartition |
    Get-CimAssociatedInstance -Association Win32_DiskDriveToDiskPartition |
    Select-Object -Property SerialNumber

答案 2 :(得分:0)

获取所有可用驱动器的列表

$d = (Get-PSDrive).Name -match '^[a-z]$'

对于每个驱动程序,提取序列号

get-partition -DriveLetter $i | get-disk 

完整的工作代码

$d = (Get-PSDrive).Name -match '^[a-z]$'
Foreach ($i in $d)
{
  Write-Host $i". " -nonewline
  get-partition -DriveLetter $i | get-disk 
}

输出

E。 0 WDC WDS240G2XXX-00JHXX 19377B803XXX健康在线223.57 GB GPT
F.13 Seagate Backup +集线器BK NXXQWHXX健康在线7.28 TB GPT
G. 12 Seagate Backup +集线器BK NAXX58XX健康在线7.28 TB GPT

答案 3 :(得分:-1)

试试这个:

$drive_serialnumber = Get-Partition -DriveLetter C  | Get-Disk | select-object -ExpandProperty SerialNumber
$drive_serialnumber.trim()

使用trim删除空格。