是否可以在PowerShell中使用带嵌套哈希表的splatting?

时间:2014-03-27 15:22:48

标签: powershell

示例:

Get-Service -Name w*
$t = @{a = @{Name = 'w*'} }

目标是将$t.a展开到Get-Service

这些不起作用(有些原因显而易见):

Get-Service @t.a
Get-Service @(t.a)
Get-Service @($t.a)
Get-Service ($t.@a)
Get-Service $t.@a

到目前为止我唯一能解决的解决方法是:

$b = $t.a
Get-Service @b

1 个答案:

答案 0 :(得分:2)

我不这么认为,不。即使有办法,也可以使用

$b = $t.a
Get-Service @b

仍然更清洁,我会推荐。

如何实施它的示例:

function disableservices ($type) { 
    $types = @{
        web = @{name = '*iis*'}
        db = @{name = '*sqlserv*'}
        windows = @{name = 'win*' }
    }

    if($types.ContainsKey($type)) {
        $b = $types[$type]
        Get-Service @b | Stop-Service
    } else {
        Write-Error "The type '$type' is not supported"
    }
}

disableservices windows