Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Associating WMI disk classes

Reply
 
Old 01-29-2008   #1 (permalink)
Thomas Makro


 
 

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]


 
 

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


 
 

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


 
 

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


 
 

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]


 
 

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]


 
 

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]


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Associating MDB to MS Access Microsoft Office
Associating same extension to multiple programs General Discussion
Associating several email accounts to Messenger new mail notification Live Messenger
Vista not associating with "B" mode wireless access points???? Vista General


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46