Is it possible to use splatting with a nested hash table in PowerShell?

Example:

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

The goal is to splat $t.aup Get-Service.

This does not work (some for obvious reasons):

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

The only workaround I could figure out so far is:

$b = $t.a
Get-Service @b
+2
source share
1 answer

I don’t think so, no. Even if there is a way using

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

still much cleaner and what I would recommend.

An example of how to implement it:

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
+2
source

Source: https://habr.com/ru/post/1627211/


All Articles