有条件地排除为 null 或空的 cmdlet 参数

Conditionally excluding cmdlet arguments that are null or empty

我编写了一个调用 New-Service cmdlet 来创建 Windows 服务的脚本:

New-Service -Name $Name -BinaryPathName $ExecutablePath `
    -Credential $Credential -DisplayName $DisplayName `
    -Description $Description -StartupType $StartupType

我对描述有疑问。如果 $Description 是一个空字符串或 $null 我得到一个错误:

Cannot validate argument on parameter 'Description'. 
The argument is null or empty. Provide an argument that is not null or empty, 
and then try the command again.

如果我完全省略 -Description 参数,命令运行时不会出错:

New-Service -Name $Name -BinaryPathName $ExecutablePath `
    -Credential $Credential -DisplayName $DisplayName `
    -StartupType $StartupType

我可以通过以下方式解决这个问题:

if ($Description)
{
    New-Service -Name $Name -BinaryPathName $ExecutablePath `
        -Credential $Credential -DisplayName $DisplayName `
        -Description $Description -StartupType $StartupType
}
else
{
    New-Service -Name $Name -BinaryPathName $ExecutablePath `
        -Credential $Credential -DisplayName $DisplayName `
        -StartupType $StartupType   
}

然而,这似乎冗长而笨拙。有什么方法可以告诉 Powershell cmdlet 在调用 cmdlet 时忽略为 null 或空的参数?

大致如下:

New-Service -Name $Name -BinaryPathName $ExecutablePath `
    -Credential $Credential -DisplayName $DisplayName `
    -Description [IgnoreIfNullOrEmpty]$Description -StartupType $StartupType

参数展开,在概念性 about_Splatting 帮助主题中有记录,[1] 是这种情况下的最佳方法:

# Create a hashtable with all parameters known to have values.
# Note that the keys are the parameter names without the "-" prefix.
$htParams = @{
  Name = $Name
  BinaryPathName = $ExecutablePath
  Credential = $Credential
  DisplayName = $DisplayName
  StartupType = $StartupType
}

# Only add a -Description argument if it is nonempty.
if ($Description) { $htParams.Description = $Description }

# Use the splatting operator, @, to pass the parameters hashtable.
New-Service @htParams

[1] chribonn also recommends this blog post 了解 splatting。