Powershell 传递整个哈希表以调用表达式

Powershell 传递整个哈希表以调用表达式,powershell,hashtable,Powershell,Hashtable,我想问您一个关于传递哈希表以调用表达式的问题 Iam编写简单的E2E监控,并将所提到的哈希表用作包含用于记录jira的HTTP表单的creds的主体。 它对我来说很好,但出于特定的原因,我希望根据收到的参数动态创建这个invokewebrequest。 这就是我的陷阱 所以,我不知道如何传递哈希表,其他数据类型也可以,比如字符串或int来调用表达式。 它总是像System.Collections.Hashtable一样显示 我想我错过了一些基本的东西。 我可以请教你一个建议吗 谢谢,马塞尔,让我

我想问您一个关于传递哈希表以调用表达式的问题

Iam编写简单的E2E监控,并将所提到的哈希表用作包含用于记录jira的HTTP表单的creds的主体。 它对我来说很好,但出于特定的原因,我希望根据收到的参数动态创建这个invokewebrequest。 这就是我的陷阱

所以,我不知道如何传递哈希表,其他数据类型也可以,比如字符串或int来调用表达式。 它总是像System.Collections.Hashtable一样显示

我想我错过了一些基本的东西。 我可以请教你一个建议吗


谢谢,马塞尔,让我们谈谈有什么问题

$commandFragments += " -Body $postParams"
您正在将哈希表转换为字符串。这是不可能的。所以我们能做的就是把它转换成某种东西。现在我们应该转换成什么?调用WebRequest-body

这可以通过Json实现。因此可以使用-Body$$postParams | converttoJSON

但这只是将json保存到一个字符串中,该字符串仍然不起作用,因为json需要位于Invoke WebRequest命令内的一个字符串中。因此,修复方法是用单引号将JSON括起来-正文“$$postParams |转换为Json”

为了提高效率,我们还可以做一些小的修正。就像if语句看起来一样

$PSBoundParameters.GetEnumerator() | %{
    switch($_.Key){
        "uri" { $commandFragments += " -Uri $uri" }
        "method" { $commandFragments += " -Method $method" }
        "postParams" { $commandFragments += " -Body '$($postParams | ConvertTo-Json)'" }
    }
}
最终产品是

$uri = 'https://exdom.com/login.jsp?saml_sso=false'
$method = "POST"
$postParams = @{
    "os_username" = "username";
    "os_password" = "password";
    "login" = "true"
}

$scriptBlock = {
    param(
        [Parameter(Mandatory=$true,Position=1)][string]$uri,
        [Parameter(Mandatory=$false,Position=2)][string]$method,
        [Parameter(Mandatory=$true,Position=3)][hashtable]$postParams
    )

    $commandFragments = $("Invoke-WebRequest")
    $PSBoundParameters.GetEnumerator() | %{
        switch($_.Key){
            "uri" { $commandFragments += " -Uri $uri" }
            "method" { $commandFragments += " -Method $method" }
            "postParams" { $commandFragments += " -Body '$($postParams | ConvertTo-Json)'" }
        }
    }
    (Invoke-Expression -Command $($commandFragments -join '')).Content | Out-File 'c:\tmp\response3.html'
    (Invoke-Expression -Command "Invoke-WebRequest -Uri https://exdom.com/login.jsp?saml_sso=false -Method POST -Body @(@{'os_username' = 'username@mydomain.com'; 'os_password' = 'mypassword'; 'login' = 'true'})").Content | Out-File 'c:\tmp\response4.html'
    (Invoke-WebRequest -Method $method -Uri $uri -Body $postParams|ConvertTo-Json).Content
}

Invoke-Command -ScriptBlock $scriptBlock -ArgumentList ($uri, $method, $postParams)
使用喷溅。此处不需要调用表达式。$commandFragments+=-Body$postParams
$uri = 'https://exdom.com/login.jsp?saml_sso=false'
$method = "POST"
$postParams = @{
    "os_username" = "username";
    "os_password" = "password";
    "login" = "true"
}

$scriptBlock = {
    param(
        [Parameter(Mandatory=$true,Position=1)][string]$uri,
        [Parameter(Mandatory=$false,Position=2)][string]$method,
        [Parameter(Mandatory=$true,Position=3)][hashtable]$postParams
    )

    $commandFragments = $("Invoke-WebRequest")
    $PSBoundParameters.GetEnumerator() | %{
        switch($_.Key){
            "uri" { $commandFragments += " -Uri $uri" }
            "method" { $commandFragments += " -Method $method" }
            "postParams" { $commandFragments += " -Body '$($postParams | ConvertTo-Json)'" }
        }
    }
    (Invoke-Expression -Command $($commandFragments -join '')).Content | Out-File 'c:\tmp\response3.html'
    (Invoke-Expression -Command "Invoke-WebRequest -Uri https://exdom.com/login.jsp?saml_sso=false -Method POST -Body @(@{'os_username' = 'username@mydomain.com'; 'os_password' = 'mypassword'; 'login' = 'true'})").Content | Out-File 'c:\tmp\response4.html'
    (Invoke-WebRequest -Method $method -Uri $uri -Body $postParams|ConvertTo-Json).Content
}

Invoke-Command -ScriptBlock $scriptBlock -ArgumentList ($uri, $method, $postParams)