1

I want to wrap several Invoke-RestMethod calls by two functions and I have to pass the WebSession variable between them. Goal:

Login -SessionVariable MySession
# do some work while using $MySession
Logout -WebSession $MySession

The Login and Logout functions will probably look like that:

function Login {

    Param(
        [String]SessionVariable
    )
    Invoke-RestMethod ... -SessionVariable $SessionVariable
    Invoke-RestMethod ... -WebSession ???
    Invoke-RestMethod ... -WebSession ???
}

function Logout {

    Param(
        WebSession
    )
    Invoke-RestMethod ... -WebSession $WebSession
}

But how can I use the name of the session variable in Login to pass it to the -WebSession parameter? Is it even possible to reimplement this SessionVariable/WebSession concept of Invoke-RestMethod? I think it has something to do with a call-by-reference by [ref], but how do I build the bridge between a variable name (a string) and an actual variable?

stackprotector
  • 10,498
  • 4
  • 35
  • 64

1 Answers1

0

Specify a global scope modifier in the string value for your session variable, for example:

Login -SessionVariable "global:MySession"

See this link for more details on PowerShell Scope Modifiers

NiMux
  • 826
  • 7
  • 16
user429622
  • 11
  • 3