【发布时间】:2018-10-22 08:45:50
【问题描述】:
我正在尝试使用以下脚本将输出分成两个文件:
try {
gci C:\Windows\System32 -r -Force | % {
if (!$_.PsIsContainer) {
$_.FullName;
$file = $_.FullName
}
} > paths.txt
} catch [System.UnauthorizedAccessException] {
$file > errors.txt
}
我知道您无法使用catch [System.UnauthorizedAccessException] 捕获非终止脚本,但我也不想将-ErrorAction Stop 与catch [System.Management.Automation.ActionPreferenceStopException] 一起使用(例如here),因为我不会获取所有文件路径。
【问题讨论】:
-
作为一个附带问题,尽量避免在脚本中使用别名(
gci、%),因为这会使它们更难维护。此外,如果您使用的是 PowerShell v3 或更高版本,则不需要使用$_.PsisContainer,但可以使用-Directory/-File开关Get-Childitem:FileSystem Provider -
@boxdog 感谢您的建议!
标签: windows powershell exception get-childitem unauthorizedaccessexcepti