gpt4 book ai didi

powershell - 有什么理由不在 powershell param() block 中抛出 ArgumentException?

转载 作者:行者123 更新时间:2023-12-04 14:05:52 25 4
gpt4 key购买 nike

当我发现 this blog post 时,我正在寻找一种在 powershell 中设置所需参数的方法。建议我执行以下操作:

param(
[string] $ObjectName = $(Throw "Parameter -ObjectName must be set to the name of a database object")
);

消化了一会儿我得出的结论是,抛出 ArgumentException 可能更好。与字符串相反:
param(
[string] $ObjectName = $(Throw New-Object System.ArgumentException "Parameter -ObjectName must be set to the name of a database object","ObjectNamt")
);

现在从 C# 的角度来看,后者会更好。这种做法是否有任何原因不能在 powershell 中翻译?

最佳答案

在 PowerShell 2.0 中,您可以将参数标记为 Mandatory,让 PowerShell 为您完成工作:

Param(
[Parameter(Mandatory=$true,
Position=0,
HelpMessage='ObjectName must be set to the name of a database object')]
[string]
$ObjectName
)

关于powershell - 有什么理由不在 powershell param() block 中抛出 ArgumentException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5444466/

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