Var shared between functions, is it the way to do it, or?

I got this psydo code
switch ([System.Environment]::OSVersion.Platform) {
'Unix' {
  } default {
    $BaseUrl='http://<company server>:8282/masking/api'

$Header = @{

Function New-Login {
$Authorization=Invoke-WebRequest-Uri “$($BaseURL)/login”-Method POST -Headers $Header-Body $Jason
$Header.Add(“Authorization”, ($Authorization|Select-Object-ExpandProperty Content |ConvertFrom-Json).Authorization)
Write-Output ($Authorization|Select-Object-ExpandProperty Content |ConvertFrom-Json).Authorization

Function Get-Environments {
Param (
[parameter(Mandatory = $False,

Begin {}

process {
$res=Invoke-RestMethod-Uri $url-Method GET -Headers $header

End {}


Function Get-FileRuleSet {
[parameter(Mandatory = $True,

Begin {}

Process {
Invoke-RestMethod-Uri $url-Method GET -Headers $Header|ForEach-Object {
$.responseList | ForEach-Object {
FileRulesetId= $
rulesetName= $.rulesetName
fileConnectorId= $
$Obj=New-Object-TypeName PSObject -Property $properties

End {}


if (New-Login) {

I have some var there is shared between the functions (and the next 15-20 functions),
is there a smarter or more beautiful way to do it?
Or do I do it the right and only way?
You can say, that normally a function has some input, and some output (an object),
nothing comes into the function from the sides, but you can say, in this case it does (its not defined as input parameter)
If i save this as a module, then i will have a module with some functions, and some global variables,
would you do it the same way?


Variable scope is a fully documented and heavily discussed topic, for example:

Get-help -Name about_Scopes -Examples
Get-help -Name about_Scopes -Full
Get-help -Name about_Scopes -Online

When you create a variable, it is available in that scope and any child scopes.

The example below shows how you can define a variable in one scope and access it in a child scope (by calling a function t1) and how it is available in all child scopes (by calling a function t2 which then calls a t1):

function t1 {“X = $x”}
X =
X = test
function t2 { t1 }
X = test

If you are making this a Module, which I would highly recommend, you can also setup private module variables. In your .PSD1 file, you can setup

PrivateData = @{
    DelphixAuthorizationToken = $null;
    DelphixHrmHostName = $null;
    PSData = @{

Then you can set this private variable:

$MyInvocation.MyCommand.Module.PrivateData['DelphixAuthorizationToken'] = $

I normally have a wrapper script for Invoke-RestMethod where I set global headers, allow self-signed certs, force TLS 1.2, etc. that all of my functions call. Additionally, I build the URL here and just pass the relative URL (i.e. /file-rulesets?environment_id=123) to this function. I set the AuthToken as a Parameter like so:

           [Parameter(Mandatory = $False,
                       ValueFromPipelineByPropertyName = $True,
                       HelpMessage = 'Authorization Token for session returned from Invoke-DelphixRestMethod')]
            [string]$AuthorizationToken = $MyInvocation.MyCommand.Module.PrivateData['DelphixAuthorizationToken'],