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

Welcome Forums General PowerShell Q&A Var shared between functions, is it the way to do it, or?

This topic contains 2 replies, has 3 voices, and was last updated by

8 months, 2 weeks ago.

  • Author
  • #127476

    Topics: 2
    Replies: 0
    Points: 43
    Rank: Member
    I got this psydo code
    switch ([System.Environment]::OSVersion.Platform) {
    'Unix' {
      } default {
    $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= $_.fileRulesetId
          rulesetName= $_.rulesetName
          fileConnectorId= $_.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?


  • #127488

    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    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

    Controlling the Scope of Variables

    First rule of PowerShell scoping rules…

    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
  • #127491

    Topics: 8
    Replies: 1191
    Points: 646
    Helping Hand
    Rank: Major Contributor

    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'],

The topic ‘Var shared between functions, is it the way to do it, or?’ is closed to new replies.