Skip to the main content.

PowerShell Tips

Utilizing external config data in PowerShell (3/4)

Table of contents

Post Featured Image

Tobias & Aleksandar's tip #5:

The two very well-known PowerShell experts have teamed up to share their best and most helpful PowerShell tips.

We will be publishing their scripts over the course of Scriptember in 13 blog posts. Don't miss their insights! Be sure to follow all Scriptember events, listed in our calendar here

 

All four parts of 'utilizing external config data'
from our Scriptember blog series  

 

Utilizing external config data

In the previous tip we used external configuration data stored in a *.psd1 file and imported it into a script using Import‑PowerShellDataFile. Access to *.psd1 data was simplified by turning all hash table keys into easily accessible script-global variables.

There is another great alternative that we tackle in this tip: converting a hash table to an object. This way, you have all external information contained in one variable, and IntelliSense helps you pick the information you may need.
Let’s try and improve this. Here is the *.psd1 file again:


@{
   # paths to important folders
   dataInPath = 'c:\data1', '\\server2\public\data'
   dataOutPath = '\\server99\public\results'
   dataLogPath = 'f:\local\log'

   # AD groups 
   dataGroups = 'Technicians', 'Testers', 'Auditors'

   # miscellaneaous settings
   dataTimeoutSeconds = 5400
   dataLogLevel = 4


And this is the script that reads in the *.psd1 file as a hash table (make sure both files are stored in the same folder):


# data.psd1 must be located in the same folder
$dataPath = Join-Path -Path $PSScriptRoot -ChildPath data.psd1

# safely read psd1 file (caveat: unsafe reads can execute code within psd1 file)
$dataHashtable = Import-PowerShellDataFile -Path $dataPath 


Hash tables can easily be converted to objects in PowerShell, so this gets you an object instead of a hash table:


# data.psd1 must be located in the same folder
$dataPath = Join-Path -Path $PSScriptRoot -ChildPath data.psd1

# convert hash table keys into variables
$hashObject = [PSCustomObject](Import-PowerShellDataFile -Path $dataPath)
$hashObject | Out-GridView -Title 'Hash table as Object'
$hashObject.dataLogLevel 


As object, all *.psd1 data is now contained in one single object, and exposed as different object properties. This simplifies coding a lot because now you get IntelliSense once you type "hashObject.", and get a list of all external properties that are present in the *.psd1 file.

 

Good2know


Scriptember! Stay tuned:

During Scriptember, our calendar provides the best possible overview.  

Scriptember - hier ist die Übersicht

 

  • Follow us on social media, look for and use the hashtags #Scriptember and #PowerShell.
  • Join our live sessions, overview soon here.
  • Participate in challenges and contests. 
  • Share your knowledge.

 

Find all events here!

 

 

 

Related links 

Related posts

7 min read

Hidden configuration variables in PowerShell

Tobias & Aleksandar's tip #13:

The two very well-known PowerShell experts have teamed up to share their best and most...

4 min read

Hiding confirmations in PowerShell

Tobias & Aleksandar's tip #12:

The two PowerShell experts have teamed up to share their best and most helpful...

4 min read

URL Encoding in PowerShell

Tobias & Aleksandar's tip #11:

The two very well-known PowerShell experts have teamed up to share their best and most...

About the author: