4 min read
Utilizing external config data in PowerShell (4/4)
Tobias & Aleksandar's tip #6:
The two very well-known PowerShell experts have teamed up to share their best and most...
Unlocking the Power of PowerShell: Tips for Success
Tired of digging through hash tables? Discover a trick that lets you access config settings directly as variables and make your scripts a breeze to read.
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"
}
Each tip progressively builds on creating a more manageable, efficient, and secure way to handle configuration data in PowerShell scripts, moving from basic hash tables to more advanced type-safe objects.
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.
The poster is available for download and in paper form.
Sep 30, 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 30, 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.