2014-03-27 33 views
2

实施例:是否可以在PowerShell中使用嵌套散列表进行散点图?

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

目标是图示$t.aGet-Service

这些不工作(一些显而易见的原因):

Get-Service @t.a 
Get-Service @(t.a) 
Get-Service @($t.a) 
Get-Service ([email protected]) 
Get-Service [email protected] 

我能想出迄今唯一的解决方法是:

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

这是另一种解决方法吗? '$ t.a | %{Get-Process @_}' – TessellatingHeckler

回答

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 
+0

我找不到解决方法,但如果这是唯一的选择,我认为您最好只是以$ b = @ {Name ='w *'}开头。 。先将它们全部放入哈希表中,然后将每个元素重新分配给离散变量似乎有点愚蠢。 – mjolinor

+0

我通常会同意,但我实际上可以看到使用这个。请参阅示例:) –

+1

正确,除了简单变量之外,没有办法去除任何东西。 –