# /\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")$PreviousItems = (" PowerShell : Getting Subdirectories using WMI part... "," PowerShell : Getting Subdirectories using WMI "," PowerShell basic parameter checking of a function "," PowerShell Celsius Fahrenheit converting "," PowerShell : How Can I Rename Files Using File Nam... "," PowerShell make a drive of an UNC path "," Some Powershell News "," PowerShell Import and Export a DirectoryTree and S... "," PowerShell How Can I Query a Text File and Retriev... "," PowerShell Import Shares and Security info From CS... "," ")

## PowerShell : Getting Subdirectories using WMI part 3

The shortcut to filtering subdirectories, I made in : PowerShell : Getting Subdirectories using WMI part 2 was not special character proof, e.g. "mow's dir" went wrong,

So here 2 other solutions using WMI relations :

# Get Only SubDirectories

(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated($null,"Win32_SubDirectory",$null,$null,$null,"GroupComponent",$false,$null)

# or

(new-object Management.ManagementObjectSearcher('associators of {Win32_Directory.Name="c:\\foobar"} where assocclass = Win32_SubDirectory role = GroupComponent')).get()

But how did I come to those ?

As I did say before I showed the wrong workaround in the first, the Management.RelatedObjectQuery helps with this task so following and using the 2 former posts :

Lets Query WMI from MSH

PowerShell out-DataGrid (MSH view-Object)

but as working with relations is an advanced WMI topic, and the former Service example used another kind of relation, I will show how I came to the query in this case.

I came to this using the commandline like this :

first get some info using the getrelationships() method :

# Kind of RelationShips

MowPS>(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelationships() | fl __class

__class : Win32_SubDirectory

__class : Win32_SubDirectory

__class : Win32_SubDirectory

__class : Win32_SecuritySettingOfLogicalFile

# Roles :

MowPS>(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelationShips('Win32_SubDirectory') | fl [a-z]*

GroupComponent : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\foobar"
PartComponent  : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\foobar\\bar"

GroupComponent : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\foobar"
PartComponent  : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\foobar\\Foo"

GroupComponent : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\"
PartComponent  : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\foobar"

we already used the simple overloads using no filter or only the related class,
but the next step is that big overload :

# get available overloads :

MowPS>(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated.OverloadDefinitions
System.Management.ManagementObjectCollection GetRelated()
System.Management.ManagementObjectCollection GetRelated(String relatedClass)
System.Management.ManagementObjectCollection GetRelated(String relatedClass, String relationshipClass, String relationshipQualifier, String
relatedQualifier, String relatedRole, String thisRole, Boolean classDefinitionsOnly, EnumerationOptions options)

how to fill that big overload, we use the data we have from getrelationships :

we want only Win32_SubDirectory and we want only the ones where we are the GroupComponent.

so we use a RelatedObjectQuery to see how to do that :

MowPS>$roq = new-object System.Management.RelatedObjectQuery MowPS>$roq

IsSchemaQuery         : False
SourceObject          :
RelatedClass          :
RelationshipClass     :
RelatedQualifier      :
RelationshipQualifier :
RelatedRole           :
ThisRole              :
ClassDefinitionsOnly  : False
QueryLanguage         : WQL
QueryString           :

# fill in the parts we know now :

$roq = new-object System.Management.RelatedObjectQuery$roq.SourceObject = 'Win32_Directory.Name="c:\\foobar"'
$roq.RelationshipClass = 'Win32_SubDirectory'$roq.ThisRole = 'GroupComponent'

# or use ProertyGrid to Edit :

$roq = new-object System.Management.RelatedObjectQuery$roq | out-propertygrid

# Result

MowPS>$roq IsSchemaQuery : False SourceObject : Win32_Directory.Name="c:\\foobar" RelatedClass : RelationshipClass : Win32_SubDirectory RelatedQualifier : RelationshipQualifier : RelatedRole : ThisRole : GroupComponent ClassDefinitionsOnly : False QueryLanguage : WQL QueryString : associators of {Win32_Directory.Name="c:\\foobar"where assocclass = Win32_SubDirectory role = GroupComponent # Use with ManagementObjectSearcher # Use Query Object :$mos = new-object system.management.ManagementObjectSearcher($roq)$mos.get()

# Or Query

(new-object Management.ManagementObjectSearcher('associators of {Win32_Directory.Name="c:\\foobar"} where assocclass = Win32_SubDirectory role = GroupComponent')).get()

now that you can see parameters like this the overload in not that hard do,
just fill in what you know.

You see using the .NET system.management helper classes and out-propertyGrid can make this task a whole bit easier.
just copy the properties of $roq or just use it. Enjoy, Greetings /\/\o\/\/ Tags : Comments: Post a Comment Links to this post: Create a Link << Home ## Archives October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.

 Web mow001.blogspot.com