This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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 MSHPowerShell 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 : Monad msh PowerShell