Skip to the main content.

ScriptRunner Blog

Tip #4: Utilizing external config data in PowerShell (2/4)

Table of contents

Post Featured Image

Tobias & Aleksandar's tip #4:

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. That worked great, but Import‑PowerShellDataFile always returns a hash table, so the script would always need to address the hash table first.

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 used in the previous tip (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 

# output entire hash table
$dataHashtable | Out-GridView -Title 'Review of all imported data from the psd1 file'

# utilize settings from hash table
$logLevel = $dataHashtable.datalogLevel
"Current Log Level = $logLevel"

$inPath = $dataHashtable.dataInPath
foreach ($item in $inPath)
{
   "Checking $item"


 

Let’s now add a simple filter function that takes (any) hash table, and turns each key into a variable. Here is such a simple filter:


filter Convert-HashtableToVariable
{
   foreach ($key in $_.Keys) { Set-Variable -Name $key -Value $_[$key] -Scope script }


 

With this filter, the script above can be greatly simplified: simply by adding Convert‑HashtableToVariable, the hash table that was read from the *.psd1 file is auto-converted into script-global variables that can be immediately used. No need anymore for picking the required information from inside a hash table each time you need it:


filter Convert-HashtableToVariable
{
   foreach ($key in $_.Keys) { Set-Variable -Name $key -Value $_[$key] -Scope script }
}

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

# convert hash table keys into variables
Import-PowerShellDataFile -Path $dataPath | Convert-HashtableToVariable




# script can now use all hash table keys like variables
# utilize settings from hash table
"Current Log Level = $datalogLevel"

foreach ($item in $dataInPath)
{
   "Checking $item"


 

 

 

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

5 min read

Tip #3: Utilizing external config data in PowerShell (1/4)

Tobias & Aleksandar's tip #3:

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

4 min read

Tip #4: Utilizing external config data in PowerShell (2/4)

Tobias & Aleksandar's tip #4:

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

4 min read

Tip #5: Utilizing external config data in PowerShell (3/4)

Tobias & Aleksandar's tip #5:

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

About the author: