简体   繁体   English

Powershell脚本-GCI

[英]Powershell script - gci

I'm trying to divide output into two files with following script: 我正在尝试使用以下脚本将输出分为两个文件:

try {
    gci C:\Windows\System32 -r -Force | % {
        if (!$_.PsIsContainer) {
            $_.FullName;
            $file = $_.FullName
        }
    } > paths.txt
} catch [System.UnauthorizedAccessException] {
    $file > errors.txt
}

I'm aware that you can't catch non-terminating scripts with catch [System.UnauthorizedAccessException] , but I don't want to use -ErrorAction Stop with catch [System.Management.Automation.ActionPreferenceStopException] either (like here ), because I won't get all file paths. 我知道,你不能赶上非终止脚本与catch [System.UnauthorizedAccessException] ,但我不希望使用-ErrorAction Stopcatch [System.Management.Automation.ActionPreferenceStopException]任(如这里 )因为我不会获取所有文件路径。

This worked for me 这对我有用

$epath2 = $null

Get-ChildItem -Path C:\Windows\System32 -Recurse -Force -Directory -ErrorVariable epath2 |
foreach {
     if ($?) {
       Out-File -InputObject $_.FullName -FilePath c:\test\paths.txt -Append
    } 
}

if ($epath2) {
  $epath2 | 
  foreach {
     ($_.Exception -split "'")[1] | Out-File -FilePath c:\test\errors.txt -Append
  }
}

catch the errors in the -ErrorAction variable and send to errors.txt as a second pass. 在-ErrorAction变量中捕获错误,然后第二次发送到errors.txt。 There may be other errors than access denied but you'll know which folders were showing problems 除了拒绝访问外,可能还有其他错误,但是您会知道哪个文件夹出现问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM