Global variable in PowerShell module

Welcome Forums General PowerShell Q&A Global variable in PowerShell module

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

 
Participant
1 year ago.

  • Author
    Posts
  • #83660

    Participant
    Points: 0
    Rank: Member

    Hi, I wrote more than 20 advanced functions to automate installation/configuration process of document management platform (OpenText Documentum xCP). One of them is "input" function for many others since it returns an object containing 3 crucial properties (docbroker host name, docbase name and global registry password). Can I call this "main" function only once, store returned object into global variable and then just use property of that variable in other functions?

    i.e. $global:dfc = Get-DFCParameter -xCP1ComputerName VMContentServer
    from some other function:
    $Docbroker = $dfc.docbrokerhostname
    $Docbase = $dfc.docbasename
    $GlobalRegistryPassword = $dfc.globalregistrypassword

    Note: I posted similar question last year but no one answered. More luck this time I hope.

  • #83672

    Participant
    Points: 0
    Rank: Member

    One answer would be to create your own Registry hive and set keys in there.

  • #83674

    Participant
    Points: 0
    Rank: Member

    Then I found this on the web: Use PowerShell to Persist Environment Variables

    Use PowerShell to Persist Environment Variables


    cut& paste from there below:

    ### Use the variable syntax to create a new environment variable
    $env:FirstName = 'Trevor'
    ### Use the built-in env: PSDrive
    New-Item -Path env:\LastName -Value Sullivan
    ### Set an environment variable using the System.Environment .NET class
    [System.Environment]::SetEnvironmentVariable('DOCKER_HOST', 'docker.artofshell.com')
    

The topic ‘Global variable in PowerShell module’ is closed to new replies.