Powershell 如何将1个输出从一个脚本存储到另一个脚本?

Powershell 如何将1个输出从一个脚本存储到另一个脚本?,powershell,Powershell,假设我有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.co

假设我有
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"
}

现在在 Script Pt2.PS1/<代码>中,我调用Script PT1

$second_response = & ".\Script1.ps1"
当然,
$second\u response
将上述两个响应存储为输出

如何将第二个响应作为键/值存储在Script2的表中?i、 e.本部分:

{
    "abc":  "1234",
    "def":  "897",
    "klm":  "something12"
}

$HashTable = @{ }
$HashTable.Add($second_response.key, $second_response.value)
换句话说,
$second\u response
变量应该只存储这个输出:

{
    "abc":  "1234",
    "def":  "897",
    "klm":  "something12"
}
注意:第二个响应相当动态。这意味着在不同的环境中可能会有不同的值。因此,我希望能够动态存储此响应中的任何内容,而不是硬编码这些值

另外,我需要脚本1中的2个响应,因为我将脚本1用于其他目的,例如,我只想查看vault内容。script2将对来自script1的响应进行操作,因此为了方便和灵活,我将它们分开

更新:根据@kuzimoto的建议,我删除了输出,并将响应从JSON转换回来,我从Script2获得了以下输出:

abc:  1234
def:  897
klm:  something12

我还不能发表评论,但这里有几个可能性:

  • 编辑script1.ps1
  • 你没有提到你是否需要第一组结果。如果不需要,只需从Renew_Token函数中删除或注释掉这一行
    $response | ConvertTo Json-depth 100

  • 将script1.ps1的输出解析为json

  • 您正在将script1的输出作为纯文本传递给script2。只需将
    $second\u response=&“\Script1.ps1”
    更改为
    $second\u response=&“\Script1.ps1”|从Json转换而来。然后,当您想要访问第二个响应时,请使用
    $second\u response[1]
    ,因为这两组JSON都被添加到自定义PS对象中,并且可以像访问数组一样单独访问。

    如果我在script1中有一些writeHost,例如:token renewed!JSON会被单独识别吗?如果我这样使用:$second_response=&“\Script1.ps1”| convertfromjson?换句话说,是否只存储JSON片段?另外,我之所以将它们作为两个单独的脚本,是因为我想运行script1只是为了查看。script2将有一些操作我将应用于script1的响应,这就是为什么我将它们分开,这就是为什么我没有注释掉响应1的输出,以便该脚本可以方便地用于script2之外的其他事情不幸的是,它会尝试将script1的所有输出转换为JSON,所以它可能只是出错了。yep:/刚刚尝试了它,它出错了:ConvertFrom Json:Invalid Json primitive:Renewing。除了注释掉我所有的其他输出之外,没有其他方法可以解决这个问题吗?我真的在一些输出解释上下了很大的功夫,哈哈,如果我不得不把它们注释掉,那就太可惜了:(在这一点上,你可以试着把你的函数单独放到一个文件中,然后把它们导入到Script1和Script2中,这样你就可以调用你需要的函数,而不需要所有额外的东西。