Powershell - Suppress errors witihn a function (Out-Null) how to get it working -
ive found useful powershell script on site function counting files/folder sizes.
im using because fast , low memory use large files/folders.
the problems when encounters folder doesn't have access output console saying access denied.
exception calling "getfiles" "0" argument(s): "access path 'c:\users\administrator\appdata\local\applicati\ n data' denied." @ line:4 char:37 + foreach ($f in $dir.getfiles <<<< ()) + categoryinfo : notspecified: (:) [], methodinvocationexception + fullyqualifiederrorid : dotnetmethodexception
i know, or think need use |out-null suppress errors, still have script work, cannot figure out or how this, despite multiple attempts.
so heres script if has ideas?
function get-hugedirstats ($directory) { function go($dir, $stats) { foreach ($f in $dir.getfiles()) { $stats.count++ $stats.size += $f.length } foreach ($d in $dir.getdirectories()) { go $d $stats } } $statistics = new-object psobject -property @{count = 0; size = [long]0 } go (new-object io.directoryinfo $directory) $statistics $statistics } $stats = get-hugedirstats c:\users
you're getting exception directoryinfo object, need use try / catch:
function get-hugedirstats ($directory) { function go($dir, $stats) { try { foreach ($f in $dir.getfiles()) { $stats.count++ $stats.size += $f.length } foreach ($d in $dir.getdirectories()) { go $d $stats } } catch [exception] { # here if need } } $statistics = new-object psobject -property @{count = 0; size = [long]0 } go (new-object io.directoryinfo $directory) $statistics $statistics }
if you're getting errors powershell cmdlets, can use -erroraction silentlycontinue
on cmdlet prevent errors printing screen.
Comments
Post a Comment