4

How do I pass a hashtable as parameter to powershell.exe?

The powershell.exe is started by the IntuneManagementExtension. The behavior can be reproduce by start a powershell.exe from a running powershell console.

OutHash.ps1:

Param(
   [hashtable]$hash
)

$hash

Call:

powershell.exe -File .\OutHash.ps1 -hash @{'A'=1}

Output:

Cannot process argument transformation on parameter 'hash'. Cannot convert the "System.Collections.Hashtable" value of type "System.String" to type "System.Collections.Hashtable".

Expected output:

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
A                              1  

Inside the powershell console, this call returns the expected output:

.\OutHash.ps1 -hash @{'A'=1}
fabian
  • 63
  • 5
  • 3
    You don't. The operating system's execution environment has no concept of a "hashtable", all you can pass is strings. – Mathias R. Jessen Aug 30 '22 at 10:56
  • 1
    You will need to stringify (serialize) it, see: [Is there a way to pass serializable objects to a PowerShell script with start-process?](https://stackoverflow.com/q/34076478/1701026) – iRon Aug 30 '22 at 11:00
  • 1
    From where do you launch the `PowerShell.exe` command? (please [add that to the question](https://stackoverflow.com/posts/73541224/edit)) – iRon Aug 30 '22 at 13:50
  • @iRon depending on your serialize input, it works fine for me using a string parameter and converting to a hashtable – fabian Aug 31 '22 at 14:37
  • @MathiasR.Jessen This comment untied a knot in my brain, thank you so much. I've been trying to pass a hash table to `pwsh -c { my_command_that_uses_hashtable_here }`, but now I understand why nothing I'm trying to do works. I guess I have to pass each individual value as a named parameter instead. Thank you for clarifying. – Adrian Wiik Nov 10 '22 at 11:26

3 Answers3

1

My "userfriendly" serialized solution:

Param(

    [string]$String
)

$Hash = $String -replace ",","`n" | ConvertFrom-StringData
$Hash

Call

powershell -file .\OutHash.ps1 -String "A=1,B=1"
fabian
  • 63
  • 5
0

As commented by @Mathias:

You don't. The operating system's execution environment has no concept of a "hashtable", all you can pass is strings.

From the error message, I presume that you launching the PowerShell.exe command from PowerShell itself, as a alternative supporting complex hash tables, you might encode your command including the hashtable and invoke that:

$Command        = { .\OutHash.ps1 -hash @{ 'e-mail' = 'doe,john@fabrikam.com' } }
$Bytes          = [System.Text.Encoding]::Unicode.GetBytes($Command.ToString())
$EncodedCommand = [Convert]::ToBase64String($Bytes)

PowerShell.exe -EncodedCommand $EncodedCommand
iRon
  • 20,463
  • 10
  • 53
  • 79
0

The default -Command works from cmd. It should work from Intune. You may have to put the full path to the script. "-command" and "-hash" are optional.

powershell.exe -command c:\temp\outhash.ps1 -hash @{A=1}

Name                           Value
----                           -----
A                              1
js2010
  • 23,033
  • 6
  • 64
  • 66
  • that is probably from the cmd prompt. I think the OP needs to add (to the question) from where he is launching the command... – iRon Aug 30 '22 at 13:48