1

I am using Get-PhysicalDisk | Format-Table DeviceID, UniqueID to get a listing of drive number and serial number of all drives on a Windows 2016 Server. I want to search for one serial number and capture only the drive number as a variable. I'm used to awk in UNIX and I'm totally stumped on how to achieve this in PowerShell.

Get-PhysicalDisk | Format-Table DeviceID, UniqueID

DeviceID UniqueID                              
-------- --------                              
5        624A937024897B4FF488CBF800027A4B      
8        624A937024897B4FF488CBF800028A4D      
7        624A937024897B4FF488CBF800027A59      
0        {c4d394f5-509e-11e9-a834-806e6f6e6963}
1        {c4d394f6-509e-11e9-a834-806e6f6e6963}
2        {c4d394f7-509e-11e9-a834-806e6f6e6963}
3        {c4d394f8-509e-11e9-a834-806e6f6e6963}
4        {c4d394f9-509e-11e9-a834-806e6f6e6963}
6        624A937024897B4FF488CBF800027A56

I want to expand this command to find SerialNumber 624A937024897B4FF488CBF800027A56 then set a variable called $DriveNumber to the value of 6 as shown in the output.

I then plan to use this variable in Set-Disk to take the drive offline/online as a perform a volume overwrite. I don't want to hard code the drive number because upon reboot, the drive number could change.
NOTE I was using Get-Disk and piping the appropriate output to Set-Disk to perform my drive off/online. But, I have a mysterious issue of the virtual drives not displaying with Get-Disk, therefore I'm trying to find a workaround with Get-PhysicalDisk Thanks!

1 Answers1

2
$driveNumber = (
  Get-PhysicalDisk | Where-Object UniqueId -eq '{624A937024897B4FF488CBF800027A56}'
).DeviceId

Note the need to enclose the GUID string in {...}.

As all PowerShell cmdlets do, Get-PhysicalDisk outputs objects whose properties you can query.

Cmdlet Where-Object acts as a filter on the objects it receives from the pipeline and compares the value of property UniqueId to the specified literal GUID (string), which, by definition, matches (at most) one object.

(...).DeviceId returns the value of the target objects' DeviceId property and assigns it to variable $driveNumber.


A note re use of Format-* cmdlets such as Format-Table:

Only ever use Format-* cmdlets for display formatting.

If the intent is further programmatic processing:

  • either: simply access the input objects' intrinsic properties (whose availability is independent of whether they display by default or via a Format-* cmdlet call)

  • or: if you need to create simplified or transformed objects with only a subset of the original properties and/or transformed property property values (calculated properties, use Select-Object.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    @RightCoaster: You're missing the `{...}` around the GUID. What is _displayed_ is irrelevant in PowerShell - what matters is what properties the objects intrinsically have. Note that `Format-*` cmdlets should only ever be used for _display formatting_ - never if the intent is further _programmatic processing_. – mklement0 Mar 29 '19 at 22:33
  • 2
    @RightCoaster - just in case mklement0 hasn't made it _painfully_ clear ... the `Format-*` cmdlets take your objects, **_butcher them_**, and send out the remnants wrapped in _formatting code_. to see what i am talking about try this ... >> $Test = $PSVersionTable | Format-Table; $Test.Count; $Test[1]` << note the red error text about `Out-LineOutput` ... [*grin*] – Lee_Dailey Mar 29 '19 at 23:28