我需要在Powershell脚本中公开调试或详细数据。如何定义允许用户使用get-object
或get-object -verbose
运行的函数?
答案 0 :(得分:3)
将CmdletBinding
属性应用于您的函数:
function Get-Object
{
[CmdletBinding()]
PARAM()
Write-Verbose "You'll only see this with -Verbose"
}
答案 1 :(得分:3)
要不,请使用-verbose
,但要使用开关:
function get-object{
param (
[switch]$gassy
)
"switch gassy is {0}" -f $gassy
}
> get-object
switch gassy is False
> get-object -gassy
switch gassy is True
请参见Get-Help about_commonparameters
-详细[:{$ true | $ false}] 别名:vb
Displays detailed information about the operation performed by the
command. This information resembles the information in a trace or in
a transaction log. This parameter works only when the command generates
a verbose message. For example, this parameter works when a command
contains the Write-Verbose cmdlet.
The Verbose parameter overrides the value of the $VerbosePreference
variable for the current command. Because the default value of the
$VerbosePreference variable is SilentlyContinue, verbose messages
are not displayed by default.
Valid values:
$true (-Verbose:$true) has the same effect as -Verbose.
$false (-Verbose:$false) suppresses the display of verbose
messages. Use this parameter when the value of $VerbosePreference
is not SilentlyContinue (the default).
答案 2 :(得分:0)
您可以调用param
函数。在这种情况下,我定义了一个名为$obj
的变量,它是一个字符串,并绑定到一个名为“ Get-Object”的命令行参数。
param (
[string]$obj = Get-Object
)
If($obj)
{
"Do Something"
}
else
{
"Do Something Else"
}