2014-03-06 7 views
0

Ive는 파일/폴더 크기를 계산하는 기능이있는이 사이트에서 유용한 powershell 스크립트를 발견했습니다.Powershell - 기능을 사용하여 오류를 표시하지 않음 (Out-Null) 작동 방법을 알아 봅니다.

대용량 파일/폴더의 메모리 사용 속도가 빠르고 빠르기 때문에이 방법을 사용하고 있습니다.

문제가 발생하면 폴더에 액세스 할 수 없어 액세스가 거부되었다는 콘솔에 출력됩니다. 그러나 난 그냥 여러 시도에도 불구하고,이 작업을 수행하는 곳이나 방법을 알아낼 수 없습니다, 오류를 억제하는 아웃 널을, 그러나 아직도 스크립트 일이 |

Exception calling "GetFiles" with "0" argument(s): "Access to the path 'c:\users\administrator\AppData\Local\Applicati\ 
n Data' is denied." 
At line:4 char:37 
+   foreach ($f in $dir.GetFiles <<<<()) 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodException 

나는 알고있다, 아니면 사용할 필요가 있다고 생각합니다.

아무도 아이디어가 있다면 스크립트를 heres 하시겠습니까? 당신이 어떤 PowerShell cmdlet을에서 오류를 얻는 경우

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] { 
      # Do something here if you need to 
     } 
    } 
    $statistics = New-Object PsObject -Property @{Count = 0; Size = [long]0 } 
    go (new-object IO.DirectoryInfo $directory) $statistics 
    $statistics 
} 

, 당신은에 -ErrorAction SilentlyContinue을 사용할 수 있습니다 : 당신이 시도 사용할 필요가 있으므로

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 

답변

1

당신은 캐치 /의의 DirectoryInfo 개체에서 예외를 받고있어 cmdlet은 화면에 인쇄 오류를 방지합니다.

+0

멋진! "시도"가 해결되었습니다! 그게 어떻게 작동하는지 보여 주셔서 고마워요 ... 내 powershell 도약과 경계에 올 것이라고 확신합니다. 나는 Erroraction을 시도했지만 아무런 성공도하지 못했습니다. 그래서 나는 함수 내에서 어떻게 든 침묵 할 필요가 있다고 생각했습니다. 감사!! – Will