gpt4 book ai didi

powershell - 我可以在Powershell中自定义 'not recognized as the name of a cmdlet'错误吗?

转载 作者:行者123 更新时间:2023-12-04 03:48:21 29 4
gpt4 key购买 nike

假设我在命令行上输入错误:

whih foo

Powershell返回:
whih : The term 'whih' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ whih mocha
+ ~~~~
+ CategoryInfo : ObjectNotFound: (whih:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

长消息对于脚本很有用,但是对于交互式shell使用,我想用更短的内容将其包装起来,例如:
'whih' isn't a cmdlet, function, script file, or operable program.

我可以包装错误并将其更改为更短的内容吗?

最佳答案

是的,您可以使用a CommandNotFoundException 截获CommandNotFoundAction!

$ExecutionContext.InvokeCommand.CommandNotFoundAction = {
param($Name,[System.Management.Automation.CommandLookupEventArgs]$CommandLookupArgs)

# Check if command was directly invoked by user
# For a command invoked by a running script, CommandOrigin would be `Internal`
if($CommandLookupArgs.CommandOrigin -eq 'Runspace'){
# Assign a new action scriptblock, close over $Name from this scope
$CommandLookupArgs.CommandScriptBlock = {
Write-Warning "'$Name' isn't a cmdlet, function, script file, or operable program."
}.GetNewClosure()
}
}

关于powershell - 我可以在Powershell中自定义 'not recognized as the name of a cmdlet'错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51656432/

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