gpt4 book ai didi

powershell - 如何将 powershell 脚本设置为仅显示简短的错误描述

转载 作者:行者123 更新时间:2023-12-03 20:40:00 42 4
gpt4 key购买 nike

在运行脚本时显示错误描述时,有没有办法只获取错误消息?

我有一个运行的脚本,当发生错误时,我会在不同时间收到错误消息,例如,如果我没有将参数传递给我编写的函数,我会得到:

    No Setup Location specified (i.e. \\server\share\folder)
At \\server\share\folder\script.ps1:9 char:31
+ [string] $setupHome = $(throw <<<< "No Setup Location specified (i.e. \\server\share\folder)")
+ CategoryInfo : OperationStopped: (No Setup Locati...ojects\something):String) [], RuntimeException
+ FullyQualifiedErrorId : No Setup Location specified (i.e. \\server\share\folder)

这很好,但我想要的是能够设置一个首选项(如 ErrorAction),它只会向我显示错误消息,而不是所有额外的粘性物质,这些东西很好用,但会弄乱我的控制台。所以不是上面的,我只想看到第一行:
No Setup Location specified (i.e. \\server\share\folder)

最佳答案

您可以设置 $ErrorView'CategoryView'获得不那么冗长的错误报告:

$ErrorView = 'CategoryView'
Get-Item foo

ObjectNotFound: (C:\foo:String) [Get-Item], ItemNotFoundException

默认为 'NormalView' ,它提供了更详细的输出:
$ErrorView = 'NormalView'

Get-Item foo

ObjectNotFound: (C:\foo:String) [Get-Item], ItemNotFoundException
Get-Item : Cannot find path 'C:\foo' because it does not exist.
At line:8 char:1
+ Get-Item foo
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\foo:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

关于powershell - 如何将 powershell 脚本设置为仅显示简短的错误描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798008/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com