当前位置 博文首页 > Powershell小技巧之用变量累积记录错误

    Powershell小技巧之用变量累积记录错误

    作者:admin 时间:2021-02-07 18:04

    当你使用GC查询文件时,当你没有足够的权限你可能会错过不少错误。你可以使用“–ErrorAction SilentlyContinue”忽略这些错误。

    有一个不错的方法来获取这些错误。

    我们在Windows目录中查询所有PS脚本文件,将它的文件保存在$PSScripts,同时将错误日志记录在变量ErrorList中:

    复制代码 代码如下:

    $PSScripts = Get-ChildItem -Path c:\windows -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue -ErrorVariable ErrorList

    $ErrorList | ForEach-Object {
      Write-Warning ('Access denied: ' + $_.CategoryInfo.TargetName)
    }

    支持所有版本

    js
    下一篇:没有了