PowerShell中$ ErrorView的用途是什么?
$Errorview变量确定PowerShell中错误消息的显示格式。在PowerShell7之前,主要有两个视图,
普通视图(默认视图)
分类视图
在PowerShell版本7中,包括了一个新的其他错误视图类别,现在版本7有3个$ErrorView类别。
简洁视图(默认)
普通视图
分类视图
我们将一一理解每个视图。
A)普通视图
它是PowerShell版本7之前的默认视图,并且会产生详细的多行错误和位噪声。它包括异常名称,类别,错误的行号等。
$ErrorView = 'NormalView' Get-ChildItem C:\NoDirectory
输出结果
Get-ChildItem : Cannot find path 'C:\NoDirectory' because it does not exist. At line:1 char:1 + Get-ChildItem C:\NoDirectory + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\NoDirectory:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
B)类别视图
专为生产环境设计的单衬板和结构化视图。其格式如下。
{Category}: ({TargetName}:{TargetType}):[{Activity}], {Reason}例如
$ErrorView = 'CategoryView' Get-ChildItem C:\NoDirectory
输出结果
ObjectNotFound: (C:\NoDirectory:String) [Get-ChildItem], ItemNotFoundException
C)简洁视图
PowerShell版本7中的默认视图。它提供了简洁的错误消息。如果错误来自命令行,则为单行错误消息。
例如
$ErrorView = 'ConciseView' Get-ChildItem C:\NoDirectory
输出结果
Get-ChildItem: Cannot find path 'C:\NoDirectory' because it does not exist.
如果错误来自脚本,则它是多行错误消息,其中包含错误消息和错误的行号。
$ErrorView = 'ConciseView' PS C:\> C:\Temp\TestPS1.ps1
输出结果
Error message in Concise view Get-ChildItem: C:\Temp\TestPS1.ps1:2 Line | 2 | Get-ChildItem c:\nonDirectory | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot find path 'C:\nonDirectory' because it does not exist.
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短