Using global variables in a ps1

非 Y 不嫁゛ 提交于 2021-01-28 18:43:41

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/54669010/using-global-variables-in-a-ps1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!