【发布时间】:2019-05-14 05:31:12
【问题描述】:
假设我有script1.ps1,代码如下:
Function Renew_Token($token) {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-Vault-Token", $token)
$response = Invoke-RestMethod -method POST -uri "https://vault.com:8243/v1/auth/token/renew-self" -ContentType 'application/json' -headers $headers
$response| ConvertTo-Json -depth 100
}
Function getValues($token) {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-Vault-Token", $token)
$response = Invoke-RestMethod -method GET -uri "https://vault.com:8243/v1/secret/vault/development" -ContentType 'application/json' -headers $headers
$response.data| ConvertTo-Json -depth 100
}
Renew_Token $token
write-host "token renewed!"
write-host "Vault Values:"
getValues $token
这给了我这样的回应:
{
"request_id": "ghgdf5-yuhgt886-gfd76trfd",
"lease_id": "",
"renewable": false,
"lease_duration": 0,
"data": null,
"wrap_info": null,
"warnings": null,
"auth": {
"client_token": "i657ih4rbg68934576y",
"accessor": "t543qyt54y64y654y",
"policies": [
"default",
"vault"
],
"token_policies": [
"default",
"vault"
],
"metadata": null,
"lease_duration": 2000,
"renewable": true,
"entity_id": ""
}
}
token renewed!
Vault Values:
{
"abc": "1234",
"def": "897",
"klm": "something12"
}
现在考虑一个Script2.ps1,我叫script1
$second_response = & ".\Script1.ps1"
当然,$second_response 会将上面的 2 个响应存储为输出。
如何将第二个响应作为键/值存储在 Script2 的表中?即这部分:
{
"abc": "1234",
"def": "897",
"klm": "something12"
}
$HashTable = @{ }
$HashTable.Add($second_response.key, $second_response.value)
换句话说,$second_response 变量应该只存储这个输出:
{
"abc": "1234",
"def": "897",
"klm": "something12"
}
注意:第二个响应是相当动态的。这意味着在不同的环境中可能有不同的值。因此,我希望能够动态存储此响应中的任何内容,而不是对值进行硬编码
另外,我需要脚本 1 中的 2 个响应,因为我将 script1 用于其他目的,例如说我只想查看保管库内容。 script2 将对来自 script1 的响应进行操作,因此为了方便和灵活,我将它们分开
更新:根据@kuzimoto 的建议,我删除了输出并将响应从 JSON 转换回,我从 Script2 获得了这个输出:
abc: 1234
def: 897
klm: something12
【问题讨论】:
标签: powershell