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),因为我无法获取所有文件路径。
答案 0 :(得分:0)
这对我有用
$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
}
}
在-ErrorAction变量中捕获错误,并作为第二遍发送到errors.txt。除了访问被拒绝外,可能还有其他错误,但是您会知道哪个文件夹出现问题