Solved

Limit depth of -recurse when using -get-childitem

Posted on 2007-03-29
4
3,349 Views
Last Modified: 2008-01-09
Hello - I'm trying to dump out directories and owners for my helpdesk.
I found this script:
PS C:\> get-childitem \\server\c$\work  –recurse | get-acl |
select-object path,owner,accesstostring,group | export-csv c:\ACL.csv

from this site:http://www.microsoft.com/technet/scriptcenter/csc/tips/ps/acl.mspx

That's great, but I don't really need it to recurse the entire structure. All they want is the top two folder layers. Is there something equivalent to linux's --max-depth?  I also only need directories, not all objects, and really just want the folder owners, not the whole acl. If someone can assist with those additional items that would be great. I've only just looked at powershell for the first time today, so this is a learning process!

I'm running this on XP SP2, but the target will be Win2K and 2003 file servers.

thanks

0
Comment
Question by:JammyPak
  • 3
4 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18820498

It's all possible, but it would have to be a custom script, there's nothing built into limit recursion as far as I know.

I'm still a bit slow writing in PowerShell, but if you give me a day or so I'm sure I can come up with something.

Chris
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 18835048

Sorry for the wait. Basically you just want the Owner and the Path in a csv file?

I'm sure there's going to be a better / neater / easier way to do this.

Copy this to a .ps1 file and modify the $Start bit:


function do-recurse($Item, $i, $j)
{
      $Owner = $Item | Get-ACL | Select-Object owner

      Write-Output "$Item,$Owner"

      if ((get-item $Item).PSIsContainer -eq "True")
      {
            $i++
            if ($i -le $j)
            {
            foreach ($ChildItem in (get-childitem $Item))
                  {
                        $Path = $Item + "\" + $ChildItem
                        if ((get-item $Path).PSIsContainer -eq "True")
                        {
                              do-recurse $Path $i $j
                        }
                  }
            }
      }
}

$Start = "<Path>"
do-recurse $Start 0 3


Once you've done that you must change the Execution Policy. I haven't signed the code because I'm not going to buy a digital signature so you must turn down the execution policy. I suggest setting it to Unrestricted so you can test this with:

Set-ExecutionPolicy Unrestricted

You can always turn it back up to the default, Restricted, after we're done.

After that you can execute the script above with:

./<scriptname>

And if you want the output in a file, do:

./<scriptname> | Out-File <FileName>

I'm afraid Export-CSV doesn't work for this at the moment, still trying to understand out to properly control output.

In the script you configure the maximum depth by modifying this line:

do-recurse $Start 0 3

Where 3 is the maximum depth. As I mentioned above I'm sure there are better ways to do all this, but it's tricky to find them.

HTH

Chris
0
 
LVL 16

Author Comment

by:JammyPak
ID: 18839247
Chris, this seems to be working well. Thanks for putting the time in, much appreciated.
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18841652

You're welcome, happy to help.

Chris
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now