Octopus-Cmdlets icon indicating copy to clipboard operation
Octopus-Cmdlets copied to clipboard

Connect-OctoServer powersell connection not maintained

Open andreister opened this issue 5 years ago • 2 comments

Looks like “Connect-OctoServer” does not work if calls are done between ps1 file and psm1 module, is that expected?

Using Octopus-CmdLets 0.4.4 and Powershell 5.1

Test.psm1

function Get-EnvironmentViaModule()
{
    "getting all environments..."
    Get-OctoEnvironment
}
Test.ps1

Import-Module .\Test.psm1

$ApiKey = "API-xxxx"
$Url = "https://octopus.xxx.com" 
Connect-OctoServer -Server $Url -ApiKey $ApiKey
"connected..."
Get-EnvironmentViaModule # this fails - but a direct "Get-OctoEnvironment" works

Execution:

> .\Test.ps1
connected...
getting all environments...
Get-OctoEnvironment : Connection not established. Please connect to your Octopus Deploy instance with
Connect-OctoServer
At C:\Users\andrew\Test.psm1:4 char:5
+     Get-OctoEnvironment
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-OctoEnvironment], Exception
    + FullyQualifiedErrorId : System.Exception,Octopus_Cmdlets.GetEnvironment

andreister avatar Apr 22 '19 07:04 andreister

This is by design. Albeit a bad design. The variable set by the connect command is scoped to your terminal session and isn't available in modules. I will investigate improving this.

Swoogan avatar Apr 25 '19 14:04 Swoogan

Understood - thanks for the answer 👍

andreister avatar Apr 30 '19 11:04 andreister