【发布时间】:2019-03-16 15:43:15
【问题描述】:
我想问你一个关于将哈希表传递给 Invoke-Expression 的问题。
我正在编写简单的 E2E 监控,并提到哈希表用作包含 HTTP 表单凭据的主体以记录 jira。 它对我来说很好,但出于特定原因,我想根据收到的参数动态创建这个 Invoke-Webrequest。 这就是我的收获。
因此,我不知道如何将哈希表(其他数据类型也可以,如字符串或 int)传递给 Invoke Expression。
它总是像System.Collections.Hashtable
$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 = @()
$commandFragments += "Invoke-WebRequest"
if ( $PSBoundParameters.ContainsKey('uri')){
$commandFragments += " -Uri $uri"
}
if ( $PSBoundParameters.ContainsKey('method')){
$commandFragments += " -Method $method"
}
if ( $PSBoundParameters.ContainsKey('postParams')){
$commandFragments += " -Body $postParams"
}
$commandFromFragments = $commandFragments -join ''
(Invoke-Expression -Command $commandFromFragments).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).Content | Out-File 'c:\tmp\response5.html'
}
Invoke-Command -ScriptBlock $scriptBlock -ArgumentList ($uri, $method, $postParams)
我想我缺少一些基本的东西。 可以请教一下吗?
谢谢,马塞尔
【问题讨论】:
-
使用喷溅。
Invoke-Expression这里不需要。 -
$commandFragments += " -Body $postParams"splatting and the&operator instead
标签: powershell hashtable