How to use a global variables file in powershell

Mohammed Abdul-Basith 1 Reputation point
2022-04-26T16:20:35.583+00:00

Hi,
I've been setting up vcheck reports and noticed that all the variables are stored in the global variables.ps1 file, what I'd like to understand is how are these variables referenced in the plugin scripts and other vcheck scripts?
This isn't a vcheck only question though, I'm trying to understand how powershell allows calling variables from an external file, this is something that I dont know how to do and would help me with scripting in general.

Thanks in advance!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,459 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 103.9K Reputation points MVP
    2022-04-26T17:21:23.52+00:00

    Hi @Mohammed Abdul-Basith ,

    maybe this helps to get an idea how global variables are working: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2

    And an example how to reference to a global variable: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2#example-5-using-a-local-variable-in-a-remote-command

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 45,831 Reputation points
    2022-04-26T18:22:48.167+00:00

    Those are probably "dot-sourced" in the other scripts.

    Something like this:

    . .\variables.ps1        # contains a variable declaration "$x = 1"
    
    $y = $x + 2
    write-host $y           # value of $y should be 3
    
    0 comments No comments