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...
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.
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"
}
During Scriptember, our calendar provides the best possible overview.
Sep 4, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 4, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 4, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
--------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.
--------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.