OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Powershell splatting a nested hash table

  • Thread starter Thread starter Gordon
  • Start date Start date
G

Gordon

Guest
I have a function that returns a complex nested hash table data structure, where part of it forms the arguments for a further function call, and part of it is strings for reporting errors that result in the arguments not being populated. Ideally I would like to splat just the arguments hash table, but I am starting to think that can't be done. So, an example of the basic problem looks like this...

Code:
function Test {
    param (
        [String]$A,
        [String]$B,
        [String]$C
    )
    Write-Host "A: $A"
    Write-Host "B: $B"
    Write-Host "C: $C"
}

$data = @{
    arguments = @{
        A = 'A string'
        B = 'Another string'
        C = 'The last string'
    }
    kruft = 'A string that doesn not need to get passed'
}

Ideally I want to splat $data.arguments, so something like this... Test @data.arguments But that doesn't work, resulting in the error

Code:
The splatting operator '@' cannot be used to reference variables in an expression. '@data' can be used only as an argument to a command. To 
reference variables in an expression use '$data'.

So I tried... Test @(data.arguments) Which results in the error

Code:
data.arguments : The term 'data.arguments' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.

I also tried... Test @($data.arguments) Which results in the whole hash table being passed as a single argument and the output is

Code:
A: System.Collections.Hashtable
B: 
C:

What DOES work is...

Code:
$arguments = $data.arguments
Test @arguments

Which has me thinking you really cannot splat anything but a simple variable that is an appropriate hash table. But, I am hoping someone can verify that is indeed true, or point out the solution I haven't come up with yet. The actual code requires 5 arguments, with somewhat verbose names because I prefer descriptive names, so splatting is very much an appropriate solution. Needing to make a new variable with just the hash table to be passed isn't an issue, just wondering if it really is the only option.
<p>I have a function that returns a complex nested hash table data structure, where part of it forms the arguments for a further function call, and part of it is strings for reporting errors that result in the arguments not being populated.
Ideally I would like to splat just the arguments hash table, but I am starting to think that can't be done.
So, an example of the basic problem looks like this...</p>
<pre><code>function Test {
param (
[String]$A,
[String]$B,
[String]$C
)
Write-Host "A: $A"
Write-Host "B: $B"
Write-Host "C: $C"
}

$data = @{
arguments = @{
A = 'A string'
B = 'Another string'
C = 'The last string'
}
kruft = 'A string that doesn not need to get passed'
}
</code></pre>
<p>Ideally I want to splat <code>$data.arguments</code>, so something like this...
<code>Test @data.arguments</code>
But that doesn't work, resulting in the error</p>
<pre><code>The splatting operator '@' cannot be used to reference variables in an expression. '@data' can be used only as an argument to a command. To
reference variables in an expression use '$data'.
</code></pre>
<p>So I tried...
<code>Test @(data.arguments)</code>
Which results in the error</p>
<pre><code>data.arguments : The term 'data.arguments' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
</code></pre>
<p>I also tried...
<code>Test @($data.arguments)</code>
Which results in the whole hash table being passed as a single argument and the output is</p>
<pre><code>A: System.Collections.Hashtable
B:
C:
</code></pre>
<p>What DOES work is...</p>
<pre><code>$arguments = $data.arguments
Test @arguments
</code></pre>
<p>Which has me thinking you really cannot splat anything but a simple variable that is an appropriate hash table. But, I am hoping someone can verify that is indeed true, or point out the solution I haven't come up with yet.
The actual code requires 5 arguments, with somewhat verbose names because I prefer descriptive names, so splatting is very much an appropriate solution. Needing to make a new variable with just the hash table to be passed isn't an issue, just wondering if it really is the only option.</p>
Continue reading...
 

Latest posts

Top