Using global variables in a ps1
Asked Answered
D

2

1

I can't seem to find good enough solution to my problem. Is there a good way of grouping variables in some kind of file so that multiple scripts could access them?

I've been doing some work with Desired State Configuration but the work that needs to be done cannot be efficiently implemented that way. The point is to install Azure Build Agent on a server and then to configure it. There are some variables that really should not be inside a script file just copypasted like Personal Access Token. I just want to be able to easily change it without the need to go inside every script that would be using it. In DSC you can just make a .psd1 file and access the variables like for example AllNodes.NodeName. The config file invocation and parameters look like this:

.\config.cmd --unattended --url $myUrl --auth PAT --token $myToken --pool default --agent "$env:COMPUTERNAME" --acceptTeeEula --work $workDir'

I want to make the variable $myToken accessible from outside file for better security and having a centralized place from where I can change values. $myUrl is also important to have access to due to it changing with new update to Build Agent.

Thank you in advance for your effort. If anything is not clear please let me know.

Davie answered 13/2, 2019 at 11:22 Comment(2)
Put all the functions in the same module, and then define it as a script scoped variable (ie. $script:module) from one of the module functions or in the .psm1 file. This way all your module functions can access it, but you can't directly access it from the global scopeHorsetail
Is it possible to create a module with just the variables and then use Import-Module to access them? Would it work without putting everything into one file?Davie
D
0

Thank you for the help. Those are very useful solutions in some cases, but I dug a bit deeper and found solution that suits my purpose. Basically if you have a psd1 file suited for DSC use you can also access its content via normal ps1 file. For example:

NonNodeData = 
@{
    Pat = 'somePAT'
}

Let's say this section of a psd1 file called ENV.psd1 is on your local machine in C:/Configuration

To access the content of this file you have to make a variable inside your script and use Import-PowerShellDataFile like so:

$configData = Import-PowerShellDataFile -Path "C:\Configuration\ENV.psd1"

And now you are free to use anything stored inside ENV.psd1. For example if I want to extract my PAT from config file to be able to store it in a variable in the script:

$myPat = $configData.NonNodeData.Pat

Thanks to that I can just pass $myPat as a parameter when invoking config.cmd like so:

.\config.cmd --unattended --auth PAT --token $myPat

Keeping my code cleaner and easier for any future updates.

Davie answered 15/2, 2019 at 9:57 Comment(0)
M
0

I have two very different answers to your question, although either one of them may miss your point.

First, it's possible to define veriables inside your profile script. Most people only use the profile script to define a library of functions or classes. But a variable can be made global the same way.

I have a variable named $myps that identifies the folder where I keep my PS scripts (in subfolders).

When I start a session I generally switch to this directory (oops, I called it a folder above.

The second way involde storing values of variables in a CSV file, while the names are stored in the CSV header.i then have a quickie little comandlet that steps through a CSV file, record by record, generating different expansions of a template each time through.

These values are not quite global, but they can be used in more than one context.

Mueller answered 13/2, 2019 at 11:55 Comment(0)
D
0

Thank you for the help. Those are very useful solutions in some cases, but I dug a bit deeper and found solution that suits my purpose. Basically if you have a psd1 file suited for DSC use you can also access its content via normal ps1 file. For example:

NonNodeData = 
@{
    Pat = 'somePAT'
}

Let's say this section of a psd1 file called ENV.psd1 is on your local machine in C:/Configuration

To access the content of this file you have to make a variable inside your script and use Import-PowerShellDataFile like so:

$configData = Import-PowerShellDataFile -Path "C:\Configuration\ENV.psd1"

And now you are free to use anything stored inside ENV.psd1. For example if I want to extract my PAT from config file to be able to store it in a variable in the script:

$myPat = $configData.NonNodeData.Pat

Thanks to that I can just pass $myPat as a parameter when invoking config.cmd like so:

.\config.cmd --unattended --auth PAT --token $myPat

Keeping my code cleaner and easier for any future updates.

Davie answered 15/2, 2019 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.