[英]How to reuse param on Powershell?
假設我具有以下功能:
function Get-DBStatus
{
<# .. removed help section for brevity .. #>
[CmdletBinding()]
[OutputType([System.Object])]
param
(
[Parameter(Mandatory = $true)]
[String]$ServerName,
[Parameter(Mandatory = $true)]
[String]$ServerUser,
[Parameter(Mandatory = $true)]
[String]$ServerPassword,
[Parameter(Mandatory = $true)]
[String]$DatabaseName,
)
try
{
$params = @{ ... } # <<< It's possible to avoid this duplication ?
$dbStatus = Invoke-SqlConnection @params
}
catch
{
Write-Error -Message ('An error has occured while ...')
}
...
我想避免在我的參數已經聲明和設置@params
聲明@params
。 有可能用Powershell做到嗎?
傳入的參數保存在自動變量$PSBoundParameters
。 然后,可以通過在命令中使用@PSBoundParameters
此變量來使用@PSBoundParameters
。
function Get-DBStatus {
<# .. removed help section for brevity .. #>
[CmdletBinding()]
[OutputType([System.Object])]
param (
[Parameter(Mandatory = $true)]
[String]$ServerName,
[Parameter(Mandatory = $true)]
[String]$ServerUser,
[Parameter(Mandatory = $true)]
[String]$ServerPassword,
[Parameter(Mandatory = $true)]
[String]$DatabaseName,
)
try {
$dbStatus = Invoke-SqlConnection @PSBoundParameters
}
catch {
Write-Error -Message ('An error has occured while ...')
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.