/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")$PreviousItems = (" 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... "," An other PowerShell Blog is born "," Windows PowerShell: TFM "," ")

PowerShell : Getting Subdirectories using WMI

In the new Microsoft.Public.Windows.PowerShell NewsGroup (we where shareing m.p.w.server.scripting),
In the Thread : gwmi to get folder and security info :

(the newsgroup is still so new that I can not find it in Google yes, so no link you have to fire up your newsreader and if you not have done yet subscribe to the NG (when done, keep hanging there you can learn a lot from the questions and aswers there .)
or use this link (http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.windows.powershell&cat=en_US_3750E87B-4971-4A5C-A537-45F5D7ABBECC&lang=en&cr=US ) for the Webbased version.

the question was why this command (changed to use the foobar example dir )took to long :

$folders = gwmi Win32_Directory Where {$_.Name -eq "C:\foobar"}

reason is first ALL the directorys on the computer (on all disks) get collected
and only then is the directory filtered out.

the answer is using a filter on the WMI query directly :

(gwmi Win32_directory -filter "name = 'c:\\foobar'")

but how did I come to this getting subdirectories as in the rest of the post was stated that Pete was working on a WMI translation of my PowerShell Import and Export a DirectoryTree and Security info example.

working from this former WMIsample.

PowerShell Import Shares and Security info From CSV

A good reason for this is that you want to do it remote, another reason he rewrote this was that he did not like use SDDL (no reason for using WMI as it would be also possible using the directorytree example ), but lets focus on the WMI and get subdirectory part

in the PowerShell Import and Export a DirectoryTree and Security info example I just use ls - recurse to enumerate to directories, in Share example we have a path to get the directory but how to do this in WMI ?

look at this example :

MowPS>(gwmi Win32_directory -filter "name = '$name'").getrelated() Hidden : False Archive : False EightDotThreeFileName : c:\foobar\bar FileSize : Name : c:\foobar\bar Compressed : False Encrypted : False Readable : True Hidden : False Archive : False EightDotThreeFileName : c:\foobar\foo FileSize : Name : c:\foobar\foo Compressed : False Encrypted : False Readable : True Hidden : Archive : EightDotThreeFileName : FileSize : Name : c:\ Compressed : Encrypted : Readable : True Caption : Security settings of c:\foobar ControlFlags : 44052 Description : Security settings of c:\foobar OwnerPermissions : True Path : c:\foobar SettingID : __GENUS : 2 __CLASS : Win32_LogicalFileSecuritySetting __SUPERCLASS : Win32_SecuritySetting __DYNASTY : CIM_Setting __RELPATH : Win32_LogicalFileSecuritySetting.Path="c:\\foobar" __PROPERTY_COUNT : 6 __DERIVATION : {Win32_SecuritySetting, CIM_Setting} __SERVER : Computer __NAMESPACE : root\cimv2 __PATH : \\Computer\root\cimv2:Win32_LogicalFileSecuritySetting.Path="c:\\foobar" the (gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated() Method will get all the related classes, you can see that this includes all the directories that are related and also the Win32_LogicalFileSecuritySetting class (very hande as we needed that also. to get all the relations as a WMI class use the GetRelationships Method (gwmi Win32_directory -filter "name = '$name'").GetRelationships()

but there are 2 problems left first we need to split the security and the subdirectory info that is easy :

(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated('Win32_LogicalFileSecuritySetting')
(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated('Win32_directory')

but we also get the parent directory,
a diffence is that in this case the find directory is the GroupComponent part,

GroupComponent : \\CP340339-A\root\cimv2:Win32_Directory.Name="c:\\"
we can filter on the GroupComponent but its hard to type
(with all the escaping)
But we can make use of some helperclasses for making the combined query.

it's from october last year so using Monad V2 but this is not changed much.
if you need to make more difficult WMI queries I think it still worth reading.

only to make the query in a GUI, I made an updatedversion that also takes pipelineinfo :

PowerShell out-DataGrid (MSH view-Object)

so to edit the query in a GUI you can do this :

new-object System.Management.RelatedObjectQuery | out-PropertyGrid

as I did a more detailed explainatin in that post I will not go into that,

but I will use a simpler workaround in Monad here
:
I just pipeline it to a where and check if the Path of the directories found contains the name of the current directory, if not it must be the root.

(gwmi Win32_directory -filter "name = '$name'").getrelated('Win32_directory') |? {$_.name -match $name} so you can see that the WMI relations are very handy in this case to get from a directory to the directorysecurity and also gives a way to iterate subdirectories using WMI. I hope this commandline examples make clear how this could help makeing the directoryscript using WMI. Enjoy, Greetings /\/\o\/\/ Tags : Comments: Pete Gomersall MOW, First of all a great bit of insight into wmi methodology - keep up the good work. I have got the code for security for a sigle folder done over the weekend; I was just about to ask about recursion - but you beat me to it. So another question sometimes you use: MowPS>(gwmi Win32_directory -filter "name = '$name'").getrelated()
or sometimes:
(gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated
What is your strategy for using the variable method to pass in the full path of the folder name, baring in mind that that paths using wmi need double-backslashes; I'm particularly thinking of when one needs to iterate through the subfolders?
Pete

Pete Gomersall
MOW,
The only problem is that the stategies you list in this post give only info on the first level of subfolders. It does not recurse deeper. I think we may need to use classic wmi method of interating through deep folder trees, don't you think?
Pete

/\/\o\/\/
Hello Pete,

"

Lol, as I answered about filtering in WMI for 1 folder, I did feel it comming ;-)

gr /\/\o\/\/

Pete Gomersall
What about the difference between "name = '$name'" and "name = 'C:\\FooBar'" i.e. how do you make sure you have correct path when you use '$name' in your scripts?
Pete

/\/\o\/\/
?
there is no difference.

$name = "c:\\foobar" "name = '$name'"

"name = 'C:\\FooBar'"

gr /\/\o\/\/

<< 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")