PowerShell for Windows Admins

Jan 17 2010   5:44AM GMT

Mapping Physical drives to logical drives part 2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

We have seen how to get physical disk information and how to get the associations between WMI classes.  We’ll now start to put this together.

001
002
003
004
005
006
007
008
009
010
Get-WmiObject -Class Win32_DiskDrive | foreach {
    "`n {0} {1}" -f $($_.Name), $($_.Model)

    $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=’" `
     + $_.DeviceID + "’} WHERE ResultClass=Win32_DiskPartition"
    
    Get-WmiObject -Query $query | 
    Format-List Name, Description, PrimaryPartition

}

We start by getting the physical disk information – this is just on the local computer for now but we can easily extend this to remote machines.

For each physical drive we write out the Name and model. The –f operating performs a .NET string formatting operation on the two inputs on the right hand side and maps them into the field definitions on the left hand side.

`n causes a new line to be thrown (actually get two due to PowerShell’s internals but a gap is what we want)

We build a WMI query  to get the associated partitions for a given physical drive. Notice that use the WHERE statement in the query to restrict the class that is returned.

The partitions that are returned are listed together with their type and if it is a primary partition.

Put a USB drive in your machine and run this to see a good test.

Next job is to add the logical drive information – see if you can work it out before my next post.

Technorati Tags: ,,

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: