Capturing Access Denied Errors

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.
Locked
User avatar
waynecierkowski
Posts: 23
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Wed May 14, 2014 11:40 am

Capturing Access Denied Errors

Post by waynecierkowski »

I am writing a script to review files on our servers that have been archived. The files in question have the Offline Attribute.

The command I am running is;

Get-Childitem \\Server\Share -recurse | Where-Object { $_.Attributes -like '*offline*' } | Select DirectoryName, Name | Export-CSV C:\Output.csv

All is good until I hit a directory that I do not have access to. I am a Domain Admin but someone must have changed security on that folder taking away Domain Admins.

When this access denied occurs I get the error in the command window.

What I would like to do is capture the directory name and output it to an error file that can be reviewed and these directories can be changed.

Any help with this would be greatly appreciated. TIA

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

Capturing Access Denied Errors

Post by SAPIEN Support Forums »

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

Thank you for posting, waynecierkowski.

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 ***

User avatar
dan.potter
Posts: 709
Joined: Wed Oct 08, 2014 6:02 am

Re: Capturing Access Denied Errors

Post by dan.potter »

try{}catch{$_ | out-file errorlog.txt}

User avatar
waynecierkowski
Posts: 23
Joined: Wed May 14, 2014 11:40 am

Re: Capturing Access Denied Errors

Post by waynecierkowski »

Hi Dan,

Well I tried that and I still get the access denied error in the command window.

Here is what I have in the program;

Try
{
Get-Childitem command
}
Catch
{
$_ | Out-File C:\Scripterrors.txt
}

Am I missing something? The Out-File is not even created.

Thanks..

jvierra
Posts: 14448
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Capturing Access Denied Errors

Post by jvierra »

You have to force the trap or the errors will just spew to screen.

User avatar
notarat
Posts: 24
Joined: Mon Aug 26, 2013 10:51 am

Re: Capturing Access Denied Errors

Post by notarat »

waynecierkowski wrote:I am writing a script to review files on our servers that have been archived. The files in question have the Offline Attribute.

The command I am running is;

Get-Childitem \\Server\Share -recurse | Where-Object { $_.Attributes -like '*offline*' } | Select DirectoryName, Name | Export-CSV C:\Output.csv

All is good until I hit a directory that I do not have access to. I am a Domain Admin but someone must have changed security on that folder taking away Domain Admins.

When this access denied occurs I get the error in the command window.

What I would like to do is capture the directory name and output it to an error file that can be reviewed and these directories can be changed.

Any help with this would be greatly appreciated. TIA
I am not a System Admin, but I do manage shared drive folders for around 1,400 sites for around 100,000 people around the U.S.

When I encounter a folder for which my access has been removed, I needed to annotate that folder so I can go back to the Server Admins and have my access restored. I created the following to record only those folders where I don't have permissions:

Code: Select all

gci C:\users -recurse -errorvariable OOPS -ErrorAction SilentlyContinue
If ($OOPS -ne $null)
    {
    add-content -value $OOPS c:\outputfiles\DENIED20150708.TXT
    }
Endif
The output of the script looks like:

Access to the path 'C:\users\John.Q.Public' is denied.

Maybe you can incorporate this code to record the permission denied entries and continue processing the rest of your script, or you can run this code before to identify and correct those permissions issues before running your script.

jvierra
Posts: 14448
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Capturing Access Denied Errors

Post by jvierra »

Just a note:

To clean up the error file we can do this:

$OOPS|%{$_.Exception.Message} | Out-file access_errors.txt

if $OOPS is empty then the file will not be created.

User avatar
waynecierkowski
Posts: 23
Joined: Wed May 14, 2014 11:40 am

Re: Capturing Access Denied Errors

Post by waynecierkowski »

Thanks Guys... Your solution worked great....

jvierra
Posts: 14448
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Capturing Access Denied Errors

Post by jvierra »

Also if you want just plain file names then do this:

$OOPS | % $_.CategoryInfo.TargetName }

This will list all file names that are denied.

Locked