/\/\o\/\/ PowerShelled

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

Monday, May 22, 2006

 


PowerShell : Getting Subdirectories using WMI part 2



In the last post PowerShell : Getting Subdirectories using WMI ,
I did use the WMI relation to get a folders subfolder.

Pete did ask 3 questions in the comments of the last post.
I think they were interesing enough to make a new post,

first about using $name and just the string of the directoryname,

as I made commandline examples I did just type in the string, and did $name = 'c:\foo' as you would do that in the Share example (it would be the path of the share there, i give that example in the NG I think.)
I just mixed it up in the examples by acident sorry if that was confusing.

but for the second question how to handle the escapingm if you get back a path without the escaping you can use the replace method.

$_.name.replace('\','\\')

you can see how I used it in the recursive sample below

and the last question, about recursion,
we can make a recursive function in PowerShell to get all the subdirectories,
in the sample below the function does just call itself again for each subdirectory found. so that it wil recurse the complete subtree.


# recursive directory iterating using wmi
Function get-DirTree ($name) {
  (gwmi Win32_directory -filter "name = '$name'").getrelated('Win32_directory') |
    where {$_.name -match $name} |% {
      $_.name
      get-SubDirs $_.name.replace('\','\\')
    }
}


# Example 

MowPS>get-SubDirs c:\\foobar
c:\foobar\bar
c:\foobar\foo
c:\foobar\foo\bar


Greetings /\/\o\/\/
Tags :


Comments:
Blogger /\/\o\/\/
hi Applewc,

Oops,

I did change my mind about the name while testing, but gorgot to edit.

gr /\/\o\/\/
 
Anonymous Anonymous
MOW,
See "more on wmi folder security" in the NG.
Pete
 
Anonymous Anonymous
MOW,
So this type of directory check bombs out when there are special characters in the folder name e.g. "Pete's Folder". I looked at the post on your site related to System.Management.RelatedObjectQuery but was not sure how to implement it in this case.
Pete
 
Blogger /\/\o\/\/
Pete :

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

gr /\/\o\/\/
 
Anonymous Anonymous
MOW:
So using the following:
Function get-SubFolder ($name) {
$name=$name.replace('\','\\')
(gwmi Win32_directory -filter "name = '$name'").getrelated($null,"Win32_SubDirectory",$null,$null,$null,"GroupComponent",$false,$null) |% {
$_.name
get-SubFolder $_.name
}
}
Function will get subfolders with or without special characters; but errors out when the folder path with special character is passed back to function or when Get-SubFolder C:\test's is used.
This version didn't work either:
(new-object Management.ManagementObjectSearcher('associators of {Win32_Directory.Name="c:\\foobar"} where assocclass = Win32_SubDirectory role = GroupComponent')).get()
Is it me?
Pete
 
Blogger /\/\o\/\/
this happens before :
we need to escape here also:

$name=$name.replace('\','\\').replace("'","\'")

nope its not you,
escaping is difficult to handle

gr /\/\o\/\/
 
Anonymous Anonymous
MOW,
so using another replace line:
$name=$name.replace("'","\'")
It will take account of ', but is there a generic approach to cover all situations that need escaping in the path?
Pete
 
Anonymous Anonymous
MOW, I did some tests and works with all English keyboard characters and a variety of Unicode as well.
Maybe the escape for \ and ' is all we need.
Pete
 
Post a Comment



<< 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.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?