Error handling

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5691

    by notarat at 2013-03-20 00:25:47

    I'm trying to work on my script to scan my hard drive for old files, and it's working fine so far. However, now and then I'll encounter a folder that my user account does not have ntfs permissions to see into. Because of this, I'm trying to implement error handling to capture the names of those folders.
    I created a pair of folders on my c: and removed all groups but "SYSTEM" and I'm trying to log the errors I get when attempting to recurse through them

    Try {
    Get-ChildItem c:\ -recurse -errorvariable strOOPS -errorAction stop | Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-2190))} |export-csv C:\outputfiles\old.csv
    } catch {
    write-host $_
    "$_" | Out-File C:\error.txt -append
    }

    When I run my script and use the Try/Catch method with -ev strOOPS and -ea stop it writes the error information to the error.txt file just fine, but stops execution of the script when the first error condition is encountered.

    When I use -ea continue(or inquire), it writes nothing at all to the log file, but displays the three errors I expect to see.
    [code2=plain]Get-ChildItem : Access to the path 'C:\testfolder2icreated03202013\' is denied.
    At line:2 char:1
    + Get-ChildItem c:\ -recurse -errorvariable +strOOPS | Where-Object { $_.LastWrite ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\testfolder2icreated03202013:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

    Get-ChildItem : Access to the path 'C:\testfoldericreated03192013\' is denied.
    At line:2 char:1
    + Get-ChildItem c:\ -recurse -errorvariable +strOOPS | Where-Object { $_.LastWrite ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\testfoldericreated03192013:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

    Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup\' is denied.
    At line:2 char:1
    + Get-ChildItem c:\ -recurse -errorvariable +strOOPS | Where-Object { $_.LastWrite ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand[/code2]

    I'm actually at a bit of a loss because, well, it's 2:30am where I live and I'm beat...and I'm having a difficult time wrapping my head around write-error/write-warning.

    At this point, If I drank, I'd drink.

    Could use some assistance removing my head from my backside so if you have a way to capture these permissions errors I'd be greatly appreciative.

    by mjolinor at 2013-03-20 01:32:49

    Does this do what you're after?

    Get-ChildItem c:\ -recurse -errorvariable strOOPS -errorAction continue |
    Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-2190))} |
    export-csv C:\outputfiles\old.csv

    $strOOPS | Out-File C:\error.txt -append

    by notarat at 2013-03-20 07:17:04

    [quote="mjolinor"]Get-ChildItem c:\ -recurse -errorvariable strOOPS -errorAction continue | Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-2190))} | export-csv C:\outputfiles\old.csv $strOOPS | Out-File C:\error.txt -append[/quote]

    mjolinor,

    Thanks for the response. I am receiving the following error when executing your code:

    [code2=plain]Export-Csv : Cannot convert 'System.Collections.ArrayList' to the type 'System.Char' required by parameter 'Delimit
    er'. Specified method is not supported.
    At line:1 char:157
    + Get-ChildItem c:\ -recurse -errorvariable strOOPS -errorAction continue | Where-Object { $_.LastWriteTime -lt ([D
    ateTime]::Now.AddDays(-2190))} | Export-CSV < <<< C:\outputfiles\old.txt $strOOPS | Out-File C:\outputfiles\error.t
    xt -append
    + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ExportCsvCommand[/code2]

    I changed the Export-CSV to Out-File, and when I encountered the same type error, I then changed it to

    [code2=plain].......w.AddDays(-2190))} >C]

    and I still receive the same type error. I'm Googling to see if I can find a solution.

    by MasterOfTheHat at 2013-03-20 07:55:23

    It runs fine when I copy/paste the code into a console, (changing paths and number of days):
    PS > Get-ChildItem c:\temp -recurse -errorvariable strOOPS -errorAction continue |
    >> Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-20))} |
    >> export-csv C:\temp\old.csv
    >>
    PS F:\Storage\Scripts\Windows\Exchange> cat C:\temp\old.csv

    by notarat at 2013-03-20 09:27:57

    [quote="MasterOfTheHat"]It runs fine when I copy/paste the code into a console, (changing paths and number of days):
    PS > Get-ChildItem c:\temp -recurse -errorvariable strOOPS -errorAction continue |
    >> Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-20))} |
    >> export-csv C:\temp\old.csv
    >>
    PS F:\Storage\Scripts\Windows\Exchange> cat C:\temp\old.csv
    [/quote]

    Thanks for the response. When I run the code below:
    Get-ChildItem c:\ -recurse -errorvariable strOOPS -errorAction continue |
    Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-2190))} |
    export-csv C:\outputfiles\oldfiles.csv -notype

    It does runs fine now, but the errors I encounter are not written to a file.

    Your code that follows:
    PS F:\Storage\Scripts\Windows\Exchange> cat C:\temp\old.csv

    I'm not familiar enough with cat'ing the outputfile to understand fully what you're doing with the last line. I'm assuming you're reading it in, via Get-Content, to do a search for a string like "Permission Denied"?

    I changed the code a little, to:
    Get-ChildItem c:\ -recurse -errorvariable strOOPS -errorAction continue |
    Where-Object { $_.LastWriteTime -lt ([DateTime]::Now.AddDays(-2190))} |
    export-csv C:\outputfiles\oldfiles.csv -notype
    $strOOPS | export-csv c:\outputfiles\errors.csv

    and it seems to be working great now.

    Thanks for the assistance!

    I'll work on slimming down the errors.csv file entries by maybe(?) using a (select-object $strOOPS).TargetObject for the export or trying to do some search & Replace (since those errors are things I can take my time resolving)

    by notarat at 2013-03-20 09:29:14

    Is there a way to select both Charles' and mjolinor's answers as the answer?

    by MasterOfTheHat at 2013-03-20 10:31:51

    Sorry, the "cat C:\temp\old.csv" line wasn't actually supposed to be included in the post, but all it is doing is writing the contents of the file to the console. In posh, it's actually an alias for Get-Content:
    PS > Get-Alias cat

    CommandType Name ModuleName
    ----------- ---- ----------
    Alias cat -> Get-Content

    And yeah, the errors aren't written to a file because I didn't include the "$strOOPS | Export-Csv C:\outputfiles\errors.csv" line in what I was testing, since the exception you were getting referred to the first line of the script.

    Glad it's working!

You must be logged in to reply to this topic.