将哈希表作为参数传递给powershell.exe



如何将哈希表作为参数传递给powershell.exe?

powershell.exe由IntuneManagementExtension启动。可以通过从运行的powershell控制台启动powershell.exe来重现该行为。

OutHash.ps1:

Param(
[hashtable]$hash
)
$hash

呼叫:

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

输出:

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

预期输出:

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

在powershell控制台中,此调用返回预期的输出:

.OutHash.ps1 -hash @{'A'=1}

我的"用户友好型";串行化解决方案:

Param(
[string]$String
)
$Hash = $String -replace ",","`n" | ConvertFrom-StringData
$Hash

呼叫

powershell -file .OutHash.ps1 -String "A=1,B=1"

@Mathias所评论:

你没有操作系统的执行环境没有";hashtable";,您所能传递的只是字符串

根据错误消息,我认为您从PowerShell本身启动PowerShell.exe命令,作为支持复杂哈希表的替代方案,您可能会对包括哈希表在内的命令进行编码,并调用该命令:

$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

默认-命令从cmd开始工作。它应该在Intune上工作。您可能需要放置脚本的完整路径&quot-命令";以及"-hash";是可选的。

powershell.exe -command c:tempouthash.ps1 -hash @{A=1}
Name                           Value
----                           -----
A                              1

最新更新