Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Associating WMI disk classes

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-29-2008   #1 (permalink)
Thomas Makro
Guest


 

Associating WMI disk classes

Hi, I'm trying to create a PS script, that enumerates physical disks,
and for each, lists logical volumes. So, the output should be
something like:
\\.\PHYSICALDRIVE0
C: D: E:
\\.\PHYSICALDRIVE1
F: G:

...etc...

For this, I've used two WMI classes. The first one gets the physical
disks, the second one the logical volumes (see below). However, how
does these two get linked together?
I've tried using Associators of, but with no luck.

$strComputer = "."
$colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
"root\CIMV2" ´
-computername $strComputer
$colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
namespace "root\CIMV2" ´
-computername $strComputer


My System SpecsSystem Spec
Old 01-29-2008   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Associating WMI disk classes

Quote:

> $strComputer = "."
> $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> "root\CIMV2" ´
> -computername $strComputer
> $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> namespace "root\CIMV2" ´
> -computername $strComputer
>
How about with just on WMI call:

PSH>gwmi Win32_LogicalDisk|select-object __PATH

__PATH
------
\\xxx\root\cimv2:Win32_LogicalDisk.DeviceID="C:"
\\xxx\root\cimv2:Win32_LogicalDisk.DeviceID="D:"
\\xxx\root\cimv2:Win32_LogicalDisk.DeviceID="E:"

I don't have a server to test whether it will handle properly multiple
logical disks per physical disk.

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 01-29-2008   #3 (permalink)
urkec
Guest


 

RE: Associating WMI disk classes

"Thomas Makro" wrote:
Quote:

> Hi, I'm trying to create a PS script, that enumerates physical disks,
> and for each, lists logical volumes. So, the output should be
> something like:
> \\.\PHYSICALDRIVE0
> C: D: E:
> \\.\PHYSICALDRIVE1
> F: G:
>
> ...etc...
>
> For this, I've used two WMI classes. The first one gets the physical
> disks, the second one the logical volumes (see below). However, how
> does these two get linked together?
> I've tried using Associators of, but with no luck.
>
> $strComputer = "."
> $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> "root\CIMV2" ´
> -computername $strComputer
> $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> namespace "root\CIMV2" ´
> -computername $strComputer
>
>
You can't associate them directly. You first need to associate
Win32_DiskDrive with Win32_DiskPartition :

ASSOCIATORS OF {Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE0"} WHERE
AssocClass = Win32_DiskDriveToDiskPartition

and then associate results of that query with Win32_LogicalDisk:

ASSOCIATORS OF {Win32_DiskPartition.DeviceID="Disk #0, Partition #0"} WHERE
AssocClass = Win32_LogicalDiskToPartition

You need to execute the second query for each object returned by the first
query.I don't have time to test this now, so I'm not sure about double or
single quotes.

Hope this helps.

--
urkec
My System SpecsSystem Spec
Old 01-29-2008   #4 (permalink)
Shay Levi
Guest


 

Re: Associating WMI disk classes


I was able to run this on XP successfully but it fails on Win2003:

$DiskDrive = Get-WmiObject Win32_DiskDrive
$DiskDrive | foreach {$_.DeviceID}
$DiskDrive | foreach { $_.GetRelated("Win32_DiskPartition")} | foreach {$_.GetRelated("Win32_LogicalDisk")}
| foreach {$_.DeviceID}

\\.\PHYSICALDRIVE0
C:
D:


Building up on urkec worked on both versions:

$DiskDrive = Get-WmiObject Win32_DiskDrive
$DiskDrive | foreach {
$_.DeviceID
$d = gwmi -q "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'}
WHERE AssocClass = Win32_DiskDriveToDiskPartition" | foreach {$_.name}
$d | foreach { gwmi -q "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$_'}
WHERE AssocClass = Win32_LogicalDiskToPartition"} | foreach {$_.DeviceID}
}

\\.\PHYSICALDRIVE0
C:
D:

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi, I'm trying to create a PS script, that enumerates physical disks,
> and for each, lists logical volumes. So, the output should be
> something like:
> \\.\PHYSICALDRIVE0
> C: D: E:
> \\.\PHYSICALDRIVE1
> F: G:
> ...etc...
>
> For this, I've used two WMI classes. The first one gets the physical
> disks, the second one the logical volumes (see below). However, how
> does these two get linked together?
> I've tried using Associators of, but with no luck.
> $strComputer = "."
> $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> "root\CIMV2" ´
> -computername $strComputer
> $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> namespace "root\CIMV2" ´
> -computername $strComputer

My System SpecsSystem Spec
Old 01-29-2008   #5 (permalink)
alexandair
Guest


 

Re: Associating WMI disk classes

On Jan 29, 10:05*pm, urkec <ur...@xxxxxx> wrote:
Quote:

> "Thomas Makro" wrote:
Quote:

> > Hi, I'm trying to create a PS script, that enumerates physical disks,
> > and for each, lists logical volumes. So, the output should be
> > something like:
> > \\.\PHYSICALDRIVE0
> > * * C: D: E:
> > \\.\PHYSICALDRIVE1
> > * * F: G:
>
Quote:

> > ...etc...
>
Quote:

> > For this, I've used two WMI classes. The first one gets the physical
> > disks, the second one the logical volumes (see below). However, how
> > does these two get linked together?
> > I've tried using Associators of, but with no luck.
>
Quote:

> > $strComputer = "."
> > $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> > "root\CIMV2" *´
> > * * -computername $strComputer
> > $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> > namespace "root\CIMV2" ´
> > * * -computername $strComputer
>
> You can't associate them directly. You first need to associate
> Win32_DiskDrive with Win32_DiskPartition :
>
> ASSOCIATORS OF {Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE0"} WHERE
> AssocClass = Win32_DiskDriveToDiskPartition
>
> and then associate results of that query with Win32_LogicalDisk:
>
> ASSOCIATORS OF {Win32_DiskPartition.DeviceID="Disk #0, Partition #0"} WHERE
> AssocClass = Win32_LogicalDiskToPartition
>
> You need to execute the second query for each object returned by the first
> query.I don't have time to test this now, so I'm not sure about double or
> single quotes.
>
> Hope this helps.
>
> --
> urkec- Hide quoted text -
>
> - Show quoted text -
This should help:

$strComputer = "."
$colDiskDrives = Get-WmiObject Win32_DiskDrive -ComputerName
$strComputer

$colDiskDrives | ForEach-Object {
"Physical Disk:" + $_.caption + " -- " + $_.deviceid
$strDriveDeviceID = $_.deviceid
$query1 = "Associators of
{Win32_DiskDrive.DeviceID='$strDriveDeviceID'} Where
AssocClass=Win32_DiskDriveToDiskPartition"
$colPartitions = get-wmiobject -query $query1
$colPartitions | ForEach-Object {
"Disk Partition: " + $_.DeviceID
$strPartitionDeviceID = $_.DeviceID
$query2 = "Associators of
{Win32_DiskPartition.DeviceID='$strPartitionDeviceID'} Where
AssocClass=Win32_LogicalDiskToPartition"
$colLogicalDisks = Get-WmiObject -query $query2
$colLogicalDisks | ForEach-Object {
"Logical Disk: " + $_.deviceid
}
""
}
""
}

Output looks like this:

Physical Disk:SAMSUNG HD080HJ/P -- \\.\PHYSICALDRIVE0
Disk Partition: Disk #0, Partition #0
Logical Disk:

Disk Partition: Disk #0, Partition #1
Logical Disk: C:

Disk Partition: Disk #0, Partition #2
Logical Disk: D:

-aleksandar
http://powershellers.blogspot.com

PS Thank you /\/\o\/\/.
My System SpecsSystem Spec
Old 01-29-2008   #6 (permalink)
/\/\o\/\/ [MVP]
Guest


 

Re: Associating WMI disk classes

After chatting with Alexandair (his version works for me ctp2 / xp )
I came up woth this version :

$drives = Get-WmiObject Win32_DiskDrive -ComputerName $strComputer

$s = New-Object System.Management.ManagementObjectSearcher
$s2 = New-Object System.Management.ManagementObjectSearcher

$qPartition = new System.Management.RelatedObjectQuery
$qPartition.RelationshipClass = 'Win32_DiskDriveToDiskPartition'

$qLogicalDisk = new System.Management.RelatedObjectQuery
$qLogicalDisk.RelationshipClass = 'Win32_LogicalDiskToPartition'

$drives |% {
$_.Caption
$qPartition.SourceObject = $_
$s.Query = $qPartition
$s.get() |% {
$_.DeviceID
$qLogicalDisk.SourceObject = $_
$qLogicalDisk.Query = $q2
$s.get()
}
}

more about this way of working ou can also find here :
http://mow001.blogspot.com/2006/10/p...c2-series.html

might make a entry on my new blog also later

Greetings /\/\o\/\/

"Shay Levi" wrote:
Quote:

>
> I was able to run this on XP successfully but it fails on Win2003:
>
> $DiskDrive = Get-WmiObject Win32_DiskDrive
> $DiskDrive | foreach {$_.DeviceID}
> $DiskDrive | foreach { $_.GetRelated("Win32_DiskPartition")} | foreach {$_.GetRelated("Win32_LogicalDisk")}
> | foreach {$_.DeviceID}
>
> \\.\PHYSICALDRIVE0
> C:
> D:
>
>
> Building up on urkec worked on both versions:
>
> $DiskDrive = Get-WmiObject Win32_DiskDrive
> $DiskDrive | foreach {
> $_.DeviceID
> $d = gwmi -q "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'}
> WHERE AssocClass = Win32_DiskDriveToDiskPartition" | foreach {$_.name}
> $d | foreach { gwmi -q "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$_'}
> WHERE AssocClass = Win32_LogicalDiskToPartition"} | foreach {$_.DeviceID}
> }
>
> \\.\PHYSICALDRIVE0
> C:
> D:
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > Hi, I'm trying to create a PS script, that enumerates physical disks,
> > and for each, lists logical volumes. So, the output should be
> > something like:
> > \\.\PHYSICALDRIVE0
> > C: D: E:
> > \\.\PHYSICALDRIVE1
> > F: G:
> > ...etc...
> >
> > For this, I've used two WMI classes. The first one gets the physical
> > disks, the second one the logical volumes (see below). However, how
> > does these two get linked together?
> > I've tried using Associators of, but with no luck.
> > $strComputer = "."
> > $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> > "root\CIMV2" ´
> > -computername $strComputer
> > $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> > namespace "root\CIMV2" ´
> > -computername $strComputer
>
>
>
My System SpecsSystem Spec
Old 01-29-2008   #7 (permalink)
/\/\o\/\/ [MVP]
Guest


 

Re: Associating WMI disk classes

Sorry wrong versoin
Correrion on script :

$drives = Get-WmiObject Win32_DiskDrive -ComputerName $strComputer

$s = New-Object System.Management.ManagementObjectSearcher
$s2 = New-Object System.Management.ManagementObjectSearcher

$qPartition = new-object System.Management.RelatedObjectQuery
$qPartition.RelationshipClass = 'Win32_DiskDriveToDiskPartition'

$qLogicalDisk = new-object System.Management.RelatedObjectQuery
$qLogicalDisk.RelationshipClass = 'Win32_LogicalDiskToPartition'

$drives |% {
$_.Caption
$qPartition.SourceObject = $_
$s.Query = $qPartition
$s.get() |% {
$_.DeviceID
$qLogicalDisk.SourceObject = $_
$s2.query = $qLogicalDisk
$s2.get()
}
}

enjoy,

Greetings /\/\o\/\/

"/\/\o\/\/ [MVP]" wrote:
Quote:

> After chatting with Alexandair (his version works for me ctp2 / xp )
> I came up woth this version :
>
> $drives = Get-WmiObject Win32_DiskDrive -ComputerName $strComputer
>
> $s = New-Object System.Management.ManagementObjectSearcher
> $s2 = New-Object System.Management.ManagementObjectSearcher
>
> $qPartition = new System.Management.RelatedObjectQuery
> $qPartition.RelationshipClass = 'Win32_DiskDriveToDiskPartition'
>
> $qLogicalDisk = new System.Management.RelatedObjectQuery
> $qLogicalDisk.RelationshipClass = 'Win32_LogicalDiskToPartition'
>
> $drives |% {
> $_.Caption
> $qPartition.SourceObject = $_
> $s.Query = $qPartition
> $s.get() |% {
> $_.DeviceID
> $qLogicalDisk.SourceObject = $_
> $qLogicalDisk.Query = $q2
> $s.get()
> }
> }
>
> more about this way of working ou can also find here :
> http://mow001.blogspot.com/2006/10/p...c2-series.html
>
> might make a entry on my new blog also later
>
> Greetings /\/\o\/\/
>
> "Shay Levi" wrote:
>
Quote:

> >
> > I was able to run this on XP successfully but it fails on Win2003:
> >
> > $DiskDrive = Get-WmiObject Win32_DiskDrive
> > $DiskDrive | foreach {$_.DeviceID}
> > $DiskDrive | foreach { $_.GetRelated("Win32_DiskPartition")} | foreach {$_.GetRelated("Win32_LogicalDisk")}
> > | foreach {$_.DeviceID}
> >
> > \\.\PHYSICALDRIVE0
> > C:
> > D:
> >
> >
> > Building up on urkec worked on both versions:
> >
> > $DiskDrive = Get-WmiObject Win32_DiskDrive
> > $DiskDrive | foreach {
> > $_.DeviceID
> > $d = gwmi -q "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'}
> > WHERE AssocClass = Win32_DiskDriveToDiskPartition" | foreach {$_.name}
> > $d | foreach { gwmi -q "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$_'}
> > WHERE AssocClass = Win32_LogicalDiskToPartition"} | foreach {$_.DeviceID}
> > }
> >
> > \\.\PHYSICALDRIVE0
> > C:
> > D:
> >
> > -----
> > Shay Levi
> > $cript Fanatic
> > http://scriptolog.blogspot.com
> >
Quote:

> > > Hi, I'm trying to create a PS script, that enumerates physical disks,
> > > and for each, lists logical volumes. So, the output should be
> > > something like:
> > > \\.\PHYSICALDRIVE0
> > > C: D: E:
> > > \\.\PHYSICALDRIVE1
> > > F: G:
> > > ...etc...
> > >
> > > For this, I've used two WMI classes. The first one gets the physical
> > > disks, the second one the logical volumes (see below). However, how
> > > does these two get linked together?
> > > I've tried using Associators of, but with no luck.
> > > $strComputer = "."
> > > $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> > > "root\CIMV2" ´
> > > -computername $strComputer
> > > $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> > > namespace "root\CIMV2" ´
> > > -computername $strComputer
> >
> >
> >
My System SpecsSystem Spec
Old 01-29-2008   #8 (permalink)
/\/\o\/\/ [MVP]
Guest


 

Re: Associating WMI disk classes

With Identing ;-)
http://thepowershellguy.com/blogs/po...owershell.aspx

"/\/\o\/\/ [MVP]" wrote:
Quote:

> Sorry wrong versoin
> Correrion on script :
>
> $drives = Get-WmiObject Win32_DiskDrive -ComputerName $strComputer
>
> $s = New-Object System.Management.ManagementObjectSearcher
> $s2 = New-Object System.Management.ManagementObjectSearcher
>
> $qPartition = new-object System.Management.RelatedObjectQuery
> $qPartition.RelationshipClass = 'Win32_DiskDriveToDiskPartition'
>
> $qLogicalDisk = new-object System.Management.RelatedObjectQuery
> $qLogicalDisk.RelationshipClass = 'Win32_LogicalDiskToPartition'
>
> $drives |% {
> $_.Caption
> $qPartition.SourceObject = $_
> $s.Query = $qPartition
> $s.get() |% {
> $_.DeviceID
> $qLogicalDisk.SourceObject = $_
> $s2.query = $qLogicalDisk
> $s2.get()
> }
> }
>
> enjoy,
>
> Greetings /\/\o\/\/
>
> "/\/\o\/\/ [MVP]" wrote:
>
Quote:

> > After chatting with Alexandair (his version works for me ctp2 / xp )
> > I came up woth this version :
> >
> > $drives = Get-WmiObject Win32_DiskDrive -ComputerName $strComputer
> >
> > $s = New-Object System.Management.ManagementObjectSearcher
> > $s2 = New-Object System.Management.ManagementObjectSearcher
> >
> > $qPartition = new System.Management.RelatedObjectQuery
> > $qPartition.RelationshipClass = 'Win32_DiskDriveToDiskPartition'
> >
> > $qLogicalDisk = new System.Management.RelatedObjectQuery
> > $qLogicalDisk.RelationshipClass = 'Win32_LogicalDiskToPartition'
> >
> > $drives |% {
> > $_.Caption
> > $qPartition.SourceObject = $_
> > $s.Query = $qPartition
> > $s.get() |% {
> > $_.DeviceID
> > $qLogicalDisk.SourceObject = $_
> > $qLogicalDisk.Query = $q2
> > $s.get()
> > }
> > }
> >
> > more about this way of working ou can also find here :
> > http://mow001.blogspot.com/2006/10/p...c2-series.html
> >
> > might make a entry on my new blog also later
> >
> > Greetings /\/\o\/\/
> >
> > "Shay Levi" wrote:
> >
Quote:

> > >
> > > I was able to run this on XP successfully but it fails on Win2003:
> > >
> > > $DiskDrive = Get-WmiObject Win32_DiskDrive
> > > $DiskDrive | foreach {$_.DeviceID}
> > > $DiskDrive | foreach { $_.GetRelated("Win32_DiskPartition")} | foreach {$_.GetRelated("Win32_LogicalDisk")}
> > > | foreach {$_.DeviceID}
> > >
> > > \\.\PHYSICALDRIVE0
> > > C:
> > > D:
> > >
> > >
> > > Building up on urkec worked on both versions:
> > >
> > > $DiskDrive = Get-WmiObject Win32_DiskDrive
> > > $DiskDrive | foreach {
> > > $_.DeviceID
> > > $d = gwmi -q "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'}
> > > WHERE AssocClass = Win32_DiskDriveToDiskPartition" | foreach {$_.name}
> > > $d | foreach { gwmi -q "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$_'}
> > > WHERE AssocClass = Win32_LogicalDiskToPartition"} | foreach {$_.DeviceID}
> > > }
> > >
> > > \\.\PHYSICALDRIVE0
> > > C:
> > > D:
> > >
> > > -----
> > > Shay Levi
> > > $cript Fanatic
> > > http://scriptolog.blogspot.com
> > >
> > > > Hi, I'm trying to create a PS script, that enumerates physical disks,
> > > > and for each, lists logical volumes. So, the output should be
> > > > something like:
> > > > \\.\PHYSICALDRIVE0
> > > > C: D: E:
> > > > \\.\PHYSICALDRIVE1
> > > > F: G:
> > > > ...etc...
> > > >
> > > > For this, I've used two WMI classes. The first one gets the physical
> > > > disks, the second one the logical volumes (see below). However, how
> > > > does these two get linked together?
> > > > I've tried using Associators of, but with no luck.
> > > > $strComputer = "."
> > > > $colWMIDiskDrives = get-wmiobject -class "Win32_DiskDrive" -namespace
> > > > "root\CIMV2" ´
> > > > -computername $strComputer
> > > > $colWMILogicalDisks = get-wmiobject -class "Win32_LogicalDisk" -
> > > > namespace "root\CIMV2" ´
> > > > -computername $strComputer
> > >
> > >
> > >
My System SpecsSystem Spec
Old 01-30-2008   #9 (permalink)
Thomas Makro
Guest


 

Re: Associating WMI disk classes

On Jan 30, 12:07 am, /\/\o\/\/ [MVP] <o...@xxxxxx>
wrote: Thanks everybody, that's just what I needed.
Keep up the good work.
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Associating several email accounts to Messenger new mail notification mjs Live Messenger 1 08-31-2008 09:20 PM
Vista not associating with "B" mode wireless access points???? Steve in Abbotsford Vista General 1 03-27-2007 08:18 PM
how to load .net classes IT Staff PowerShell 3 01-22-2007 11:42 AM
Associating an Animation::Completed event with an object John Dunn Avalon 8 12-08-2006 12:45 PM
RC1 and WMI - mising classes? DewBoy3d Vista performance & maintenance 1 09-18-2006 06:05 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008