Skip to main content

PowerShell + .psd1 files - decouple environment configuration data from code


What is environment configuration data?


Well, you might have heard the term 'configuration data' in usage with PowerShell DSC. The case for using configuration data is wherein all the input arguments are abstracted from the code being written so that this configuration data can be generated on the fly and passed to the underlying scripts or framework like DSC.


For some of our solutions being deployed at the customer site, we require a lot of input parameters e.g. different network subnets for management and storage networks, AD/DNS information etc.


Adding all these parameters to our input argument collector script was an error prone and tedious task since there were far too many input arguments. So instead of having a file to specify all input arguments was the preferred method.

This also helped us while troubleshooting the deployments since a local copy of the input arguments always persisted.





.PSD1 vs .JSON ?


We started with using JSON files first but later realized below salient points of using .psd1 files:


  1. .PSD1 files are native and first class citizens on PowerShell. 
  2. Also the ability to put comments in the .psd1 files. 
  3. PowerShell ISE (native to Windows Server OS) is able to edit the .psd1 files with ease.

Below I would like to take a moment and highlight a bit more on the above points.

1) .PSD1 file support in PowerShell

There was an ugly way of using the Import-LocalizedData cmdlet to read the .psd1 files. But since WMF 5.0 a function named Import-PowerShellDataFile was added. Read this PowerShell Magazine article by Ravi here.

2) .PSD1 file supports placing inline comments

In the .psd1 file you can place inline comments to mention what that input field represents. Along with this information, we also use comments to mention if a field needs to be modified or not in our .psd1 files.

3) PowerShell ISE support to edit .PSD1 files

Since our engineered solutions are mostly run from a Windows box, there is PowerShell ISE present out of the box present which allows deployment engineers to edit the files and highlight any syntax errors.




All being said, we do support consuming .JSON files in our engineered solutions for input too, since both .psd1 and .json file formats are for data storage and consuming them is a straightforward in PowerShell e.g. our PSRemotely module supports passing configuration data in both .psd1 and .json file formats.

Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

Azure DevOps Tips & Tricks - Find private REST APIs

Original source -  Azure DevOps Tip - Find private APIs Often working with Azure DevOps, I hit a wall trying to automate some tasks but there are no REST API's made public yet. It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO. Quick research reveals that this has been requested in uservoice  (please upvote). Let's see one of the very simple ways to discover some of these APIs.