Skip to content

Instantly share code, notes, and snippets.

@psitem
Last active October 9, 2023 16:43
Show Gist options
  • Save psitem/1440de1b50748f61d6d280de60b72111 to your computer and use it in GitHub Desktop.
Save psitem/1440de1b50748f61d6d280de60b72111 to your computer and use it in GitHub Desktop.
Modifies the output of Get-PhysicalDisk to have enclosure information. Works On My Machine™ certified with an LSI SAS 9201-16e and Promise SAS enclosures.
<#
Wrote this to scratch an itch for tracking where my disks are physically located within and across
storage enclosures. Before writing this I was using a spreadsheet, which was tedious to maintain.
For this script to be useful, you'll need a storage enclosure that Windows recognizes and exposes
through WMI, ie: one that provides SCSI Enclosure Services (SES).
Check the output of Get-StorageEnclosure.
On my systems with multiple Promise J610S/J630S enclosures, Get-PhysicalDisk provides "Slot #" in
the PhysicalLocation property but the EnclosureNumber isn't populated. This script adds two
Properties to the objects returned by Get-PhysicalDisk:
EnclosureSN = Enclosure SerialNumber as provided by Get-StorageEnclosure
SizeString = Disk size coverted from Bytes to KB/MB/GB/TB/PB units.
I do not know how broadly applicable this is to other brands of enclosure, the only other model I
own does not provide SES.
#>
# Format-Bytes borrowed and modified from https://theposhwolf.com/howtos/Format-Bytes/
Function Format-Bytes {
Param
(
[Parameter(
ValueFromPipeline = $true
)]
[ValidateNotNullOrEmpty()]
$number
)
Begin{
$sizes = 'KB','MB','GB','TB','PB'
}
Process {
# New for loop
for($x = 0;$x -lt $sizes.count; $x++){
if ($number -lt "1$($sizes[$x])"){
if ($x -eq 0){
return "$number B"
} else {
$num = $number / "1$($sizes[$x-1])"
$num = "{0:N2}" -f $num
return "$num $($sizes[$x-1])"
}
}
}
}
End{}
}
$disks = Get-PhysicalDisk | ForEach-Object {
$_ | `
Add-Member `
-MemberType NoteProperty `
-Name EnclosureSN `
-Value ( ( $_ | Get-StorageEnclosure ).SerialNumber ) `
-PassThru | `
Add-Member `
-MemberType NoteProperty `
-Name SizeString `
-Value ( Format-Bytes $_.Size ) `
-PassThru `
} | Sort-Object EnclosureSN, SlotNumber, { [int] $_.DeviceId }
return $disks
# $disks | Format-Table DeviceId,FriendlyName,SerialNumber,MediaType,EnclosureSN,SlotNumber,SizeString -AutoSize
@psitem
Copy link
Author

psitem commented Oct 9, 2023

Sample output from my Plex server:

PS C:\Users\Administrator> .\Get-DisksWithEnclosureInfo.ps1 | Format-Table DeviceId,FriendlyName,MediaType,EnclosureSN,SlotNumber,SizeString -AutoSize

DeviceId FriendlyName                   MediaType   EnclosureSN  SlotNumber SizeString
-------- ------------                   ---------   -----------  ---------- ----------
0        Samsung SSD 970 EVO Plus 500GB SSD                                 465.76 GB
1        Samsung SSD 970 EVO Plus 500GB SSD                                 465.76 GB
2        Samsung SSD 970 EVO Plus 2TB   SSD                                 1.82 TB
3        COVECUBE CoveFsDisk_____       Unspecified                         2.00 TB
35       COVECUBE CoveFsDisk_____       Unspecified                         2.00 TB
7        ATA WDC WD40EFRX-68N           HDD         R80095037394 1          3.64 TB
13       ATA WDC WD40EFRX-68N           HDD         R80095037394 2          3.64 TB
26       ATA WDC WD40EZRZ-00G           HDD         R80095037394 3          3.64 TB
20       ATA WDC WD40EZRZ-00G           HDD         R80095037394 4          3.64 TB
18       HGST HUH721010AL4200           HDD         R80095037394 5          9.10 TB
16       IBM-D050 HUH721010AL4200       HDD         R80095037394 6          9.03 TB
24       HGST HUH721010AL4200           HDD         R80095037394 7          9.10 TB
30       HGST HUH721010AL42C0           HDD         R80095037394 8          9.10 TB
17       HGST HUH721010AL4200           HDD         R80095037394 9          9.10 TB
27       ATA WDC WD120EMAZ-11           HDD         R80095037394 10         10.91 TB
23       HGST HUH721010AL4200           HDD         R80095037394 11         9.10 TB
29       HGST HUH721010AL4200           HDD         R80095037394 12         9.10 TB
25       ATA WDC WD120EMAZ-11           HDD         R80095037394 13         10.91 TB
22       IBM-D050 HUH721010AL4200       HDD         R80095037394 14         9.03 TB
28       HGST HUH721010AL42C0           HDD         R80095037394 15         9.10 TB
8        ATA WDC WD120EMAZ-11           HDD         R800A4042605 1          10.91 TB
9        ATA WDC WD120EMAZ-11           HDD         R800A4042605 2          10.91 TB
10       HGST H7210A520SUN010T          HDD         R800A4042605 3          8.91 TB
5        HGST HUH721010AL4200           HDD         R800A4042605 4          9.10 TB
19       ATA WDC WD120EMFZ-11           HDD         R800A4042605 5          10.91 TB
33       ATA HGST HUH721010AL           HDD         R800A4042605 6          9.10 TB
32       ATA HGST HUH721010AL           HDD         R800A4042605 7          9.10 TB
31       ATA HGST HUH721010AL           HDD         R800A4042605 8          9.10 TB
14       HGST HUH721010AL4200           HDD         R800A4042605 9          9.10 TB
15       ATA WDC WD120EMAZ-11           HDD         R800A4042605 10         10.91 TB
12       HGST HUH721010AL4200           HDD         R800A4042605 11         9.10 TB
21       ATA WDC WD120EMAZ-11           HDD         R800A4042605 12         10.91 TB
6        HGST HUH721010AL4200           HDD         R800A4042605 13         9.10 TB
4        HGST HUH721010AL4200           HDD         R800A4042605 14         9.10 TB
11       HGST HUH721010AL4200           HDD         R800A4042605 15         9.10 TB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment