Cataster
Cataster

Reputation: 3541

How to store 1 output from one script to another?

Suppose I have script1.ps1 with the following code:

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

This gives me back a response like this:

{
    "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"
}

Now consider in a Script2.ps1, I call script1

$second_response = & ".\Script1.ps1"

of course, $second_response will store the 2 responses above as output.

How can i store JUST the second response as keys/values say in a table in Script2? i.e. this part:

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

$HashTable = @{ }
$HashTable.Add($second_response.key, $second_response.value)

in other words, somehow the $second_response variable should only store this output:

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

Note: that second response is considerably dynamic. meaning there could be different values on different environments. therefore, i want to be able to DYNAMICALLY store whatever is in this response, not hardcode the values

Also, I need the 2 responses in script 1 because i use script1 for other purposes, such as say i want to only view the vault content. script2 will have operations on the response from script1, so i have them separated for this convenience and flexibility

UPDATE: Following @kuzimoto suggestion, i removed the output and converting the response back from JSON, i get this output from Script2:

abc:  1234
def:  897
klm:  something12

Upvotes: 0

Views: 69

Answers (1)

kuzimoto
kuzimoto

Reputation: 184

I can't comment yet, but here's a couple possibilities:

  1. Edit script1.ps1

You don't mention if you need the first set of results. If you don't, just simply delete or comment out this line $response| ConvertTo-Json -depth 100 from the Renew_Token function.

  1. Parse the output of script1.ps1 as json

You're passing the output of script1 to script2 as plain text. Simply change $second_response = & ".\Script1.ps1" to $second_response = & ".\Script1.ps1" | ConvertFrom-Json. Then when you want to access the second response, use $second_response[1] as both sets of JSON get added to a custom PS Object and can be accessed individually like you would an array.

Upvotes: 1

Related Questions