Setting powershell -SessionVariable in octopus

Hello all,

I am trying to interact with an API through Octopus deploy.

In step one I set: -SessionVariable session

Then I store this inside an Octopus variable to be able to reference it in the following steps.

The $session variable which is created during the “login” process is a “WebRequestSession”

As far as I know Octopus converts all variables set to string.

When I try to use the $session variable in following steps I get errors from powershell saying “cannot convert string to WebRequestSession”

Has anybody else experienced this and does anyone have any idea on how to store the variable exactly as it is

Thanks

Hi @supafly270

Thanks for getting in touch with Octopus and your question!

You’re correct that Octopus variables (normal ones and output ones too) are stored as strings. Therefore if you create a WebRequestSession object in one step, you would need to be able to pass the data to the next step as a string.

Simple objects can usually be passed between steps so long as they can be serialized into a string form, e.g. a JSON string representation.

I did some brief research online, and it looks like it’s quite difficult, if not impossible, to take the WebRequestSession variable as-is between PowerShell sessions (steps).

I think that probably only leaves two choices.

  1. Perform the necessary steps in a single step or
  2. Attempt to deconstruct the WebRequestSession variable and store any necessary details in one or many output variables (e.g. cookie values, credentials, certificates etc). Then you would need to re-create any necessary details on every subsequent step where its needed.

I found the following article about retrieving cookie details from a PS WebRequestSession object.

In addition, I found this reddit post which talks about creating a new WebRequestSession object and passing in values to recreate the session variable e.g.

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie 
$cookie.Name = "MYSESSION-ID"
$cookie.Value = $cookievalue
$cookie.Domain = $siteDomain 
$session.Cookies.add($cookie)

Unfortunately, there’s no guarantee this will work with something like this, so there is likely some trial and error required to see if you can get it working.

I hope that helps!

Best,

Hi @mark.harrison

Thank you for the response, I will try to recreate a new object with the required variables from the existing session.

Thanks again

Hi @supafly270

You’re very welcome!

Best,

Hi @mark.harrison,

Just tested this by creating a new $session variable using the new-object cmdlet and it seems to have worked.

Shall continue with this.

Thank you again

Much appreciated!

Hi @supafly270

Thank you for letting me know, I really appreciate the update!

Hope you have a great rest of your day!

Best,