Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, June 10, 2019 11:46 AM
Hi
I am new to PowerShell and just started to learn it. Your help is much appreciated on this.
In our environment, we frequently expand the server's disks. For that we need to get disk's unique ID and drive letter of servers (VM, VM with RDMs, Physical server). I am able to get this on the server by running the following PowerShell cmdlet and referring the number in Disk management (diskmgmt.msc).
get-disk | Select number, uniqueid
However, I find this a bit of manual work and want to obtain both drive letter and unique ID from a central server (say ex: Jumpbox) via PowerShell.
I find one script and that able to fetches the drive letter and unique ID. However, I find the script does not work if the VM contains RDMs or if it is a physical server.
$strComputer = Read-host -prompt 'Enter the server name:'
$colDiskDrives = get-wmiobject -query "Select * From Win32_DiskDrive" -computer $strComputer
$allDrive = @()
Foreach ($drive in $colDiskDrives)
{
$o_drive = New-Object PSObject
$a = $drive.DeviceID.Replace("\", "\\")
if($drive.serialnumber -ne $null)
{
$o_drive | Add-Member -type NoteProperty -Name UUID -value $drive.SerialNumber
$colPartitions = get-wmiobject -query "Associators of {Win32_DiskDrive.DeviceID=""$a""} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -computer $strComputer
Foreach ($Partition in $colPartitions)
{
$b = $Partition.DeviceID
$colLogicalDisk = get-wmiobject -query "Associators of {Win32_DiskPartition.DeviceID=""$b""} WHERE AssocClass = Win32_LogicalDiskToPartition" -computer $strComputer
If ($colLogicalDisk.Caption -ne $null)
{
$o_drive | Add-Member -type NoteProperty -Name DriveLetter -value $colLogicalDisk.Caption.ToString()
}
Else
{
#No letter assigned.
}
}
}
else
{
#No UUID found.
}
$allDrive += $o_drive
}
$allDrive
there is also one more thing in Stackoverflow on similar topic. However, this does not contain disk unique id:
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -PipelineVariable partition |
Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk |
Select-Object @{n='Disk';e={$disk.deviceid}},
@{n='DiskSize';e={$disk.size}},
@{n='DiskModel';e={$disk.model}},
@{n='Partition';e={$partition.name}},
@{n='RawSize';e={$partition.size}},
@{n='DriveLetter';e={$_.DeviceID}},
VolumeName,Size,FreeSpace
Please let me know how to get drive letter and disk UID for VM, VM with RDMs, Physical server etc. Your help is much appreciated. Thanks in advance.
All replies (5)
Monday, June 10, 2019 12:12 PM
CIM and WMI are equivalent. To do what you ask will require that you use remoting to obtain this information. You can also use the MS namespace which is what "Get-Disk" does. You can also use Get-Disk remotely like this:
Get-Disk -CimSession $computername | Selectnumber,uniqueid
I strongly recommend learning how to use PowerShell CmdLet help to understand how to use the CmdLets in all modes.
\(ツ)_/
Tuesday, June 11, 2019 5:24 AM
Thank you but the above cmdlet does not bring the drive letter. Any idea how to integrate ?
Tuesday, June 11, 2019 5:42 AM
Your original code does not return a drive letter. It can only give you the disk number. "Drives" are not disks. Only volumes have letters.
\(ツ)_/
Wednesday, July 10, 2019 4:03 PM
The below script (http://blog.tenera.no/?p=220) shows the drive letter and UID. However, this does not work if the system has RDMs. It would be great if there is code for that.
Ex output:
UUID DriveLetter
JD1008DM1X7K4V F:
$strComputer = "."
$colDiskDrives = get-wmiobject -query "Select * From Win32_DiskDrive" -computer $strComputer
$allDrive = @()
Foreach ($drive in $colDiskDrives)
{
$o_drive = New-Object PSObject
$a = $drive.DeviceID.Replace("\", "\\")
if($drive.serialnumber -ne $null)
{
$o_drive | Add-Member -type NoteProperty -Name UUID -value $drive.SerialNumber
$colPartitions = get-wmiobject -query "Associators of {Win32_DiskDrive.DeviceID=""$a""} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -computer $strComputer
Foreach ($Partition in $colPartitions)
{
$b = $Partition.DeviceID
$colLogicalDisk = get-wmiobject -query "Associators of {Win32_DiskPartition.DeviceID=""$b""} WHERE AssocClass = Win32_LogicalDiskToPartition" -computer $strComputer
If ($colLogicalDisk.Caption -ne $null)
{
$o_drive | Add-Member -type NoteProperty -Name DriveLetter -value $colLogicalDisk.Caption.ToString()
}
Else
{
#No letter assigned.
}
}
}
else
{
#No UUID found.
}
$allDrive += $o_drive
}
$allDrive
Wednesday, July 10, 2019 4:38 PM
How about:
get-volume | select driveletter,uniqueid
driveletter uniqueid
D \?\Volume{11111111-ffbd-11e7-beee-806e6f6e6963}\
C \?\Volume{22222222-0000-0000-007e-000000000000}\