Export unreachable folder names into a text file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Sam
Posts: 2
Last visit: Mon Feb 09, 2015 3:17 pm

Export unreachable folder names into a text file

Post by Sam »

Hi team.

Am tying to check permissions issues on user folders . I am an admin and would like to run a script and check if i will be able to access a particlar users folder in the dfs. If i am denied permission ( meaning permissions on that particular folder are skewy), I would like to output that folder name into a text file, which i will then analyse and find out what the problem is at a later stage.

Please HELPPP!!!!!

Sam
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Export unreachable folder names into a text file

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, Sam.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export unreachable folder names into a text file

Post by jvierra »

Hi Sam

Can you post a copy of your script please?
User avatar
notarat
Posts: 24
Last visit: Mon Feb 12, 2018 6:56 am

Re: Export unreachable folder names into a text file

Post by notarat »

Sam wrote:Hi team.

Am tying to check permissions issues on user folders . I am an admin and would like to run a script and check if i will be able to access a particlar users folder in the dfs. If i am denied permission ( meaning permissions on that particular folder are skewy), I would like to output that folder name into a text file, which i will then analyse and find out what the problem is at a later stage.

Please HELPPP!!!!!

Sam
I'll walk you through my script so you can see why the commands do what they do.

Code: Select all

$OOPS.clear()
gci C:\users -recurse -errorvariable OOPS
If ($OOPS -ne $null)
    {
    $PermDenied=$OOPS
    add-content -value $PermDenied c:\outputfiles\DENIED.TXT
    }
Endif
$OOPS.clear() creates the variable and clears its value

GCI is the abbreviation for Get-Childitem
You can change the path (C:\users) to whatever path you need.

-Recurse tells gci to look through every subfolder, not just c:\users

-errorvariable tells Powershell to use the variable named "OOPS" to store any errors the script encounters (i.e. the names of the directories you can't access)

The If/Endif statement is where Powershell checks to see if OOPS is blank or not. (whether it contains the names of folders you can't access)

If OOPS is not blank, that information is assigned to the variable "PERMDENIED".,

Then, add-content statement tells Powershell to write the value of the variable "PermDenied" to the file located in C:\outputfiles called DENIED.txt

Add-Content will create the file if it doesn't already exist, and will append information to it if it does already exist...so make sure you create a separate file for each check you're performing.

This is probably a lot slower and less efficient method to search a large network drive than others use, but it works for me.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export unreachable folder names into a text file

Post by jvierra »

You are trying too hard. The custom error variable is deleted and created automatically. We do not manage it. Just name it and use it.

Like this:
PowerShell Code
Double-click the code block to select all.
gci C:\users -recurse -errorvariable OOPS
$OOPS | select TargetObject
User avatar
notarat
Posts: 24
Last visit: Mon Feb 12, 2018 6:56 am

Re: Export unreachable folder names into a text file

Post by notarat »

jvierra wrote:You are trying too hard. The custom error variable is deleted and created automatically. We do not manage it. Just name it and use it.

Like this:
PowerShell Code
Double-click the code block to select all.
gci C:\users -recurse -errorvariable OOPS
$OOPS | select TargetObject
jvierra,

I kind of got the feeling the original poster didn't have any experience using power shell, so I "Rube Goldberg'd" some code together which would explain how to do what he/she wants, and provide an opportunity to also explain some additional things, like how you declare a variable, use abbreviations, use If/Endif statements, etc.

The code I use for this task is actually quite a bit different. It also tends to find and record the sub-folders contained within folders where my permissions are insufficient.
PowerShell Code
Double-click the code block to select all.
$PCode=Read-Host "Enter Person for which this report is being run: "
$target=Read-Host "Enter Drive letter or UNC Path Name: (Ex: C:\ or \\Server\Dept\Loc  "
$null = Get-ChildItem $target –Force –Recurse –ErrorAction SilentlyContinue –ErrorVariable AccessDenied
($AccessDenied.Exception)
forgot to add that if you needed it in a text file you can use something like

Code: Select all

 >c:\outputfiles\$pcode"_Denied.txt" 
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export unreachable folder names into a text file

Post by jvierra »

notarat - yes, that should be helpful with one caveat. I used the "TargetObject" because it contains the name of the folder where access is denied. This avoids having to look at and save the whole exception.

The format of an output would be derived from processing all of this in a pipeline into either a set of CSV files or into a human readable report of some kind.

Note that the OP made some non-serious mistakes that I have noticed even many experienced scripters make. Too often non-programmers try to declare everything and do things like clearing a new variable. While this doesn't hurt it does make the code harder to read and understand.
This topic is 9 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked