how to define "config file" for my ps scripts? RRS feed

  • Question

  • Hi,

    I have several ps script files with some constants on them (service URI, user, log path, ...)

    I would like to have those parametrizations outside the script files. so I can have the exact same script file on each environment and just change the config/parametrization file.

    What is the best way to achieve this?



    Monday, April 29, 2019 7:37 PM

All replies

  • You can use a CSV file, an XML file or a clixml file.

    help Import-Csv -online
    help ConvertTo-Xml-xml -online
    help import-clixml -online


    Monday, April 29, 2019 8:14 PM
  • I am using for some little more complex projects Powershell data files and a special way to import them. I name the PS data files "BaseName" exactly the same as the according script (Script name would be PSScript01.ps1 and the data file name would be PSScript01.psd1) and use the following snippet to import the parameter:

    $Config = Import-PowerShellDataFile -Path $($MyInvocation.MyCommand.Definition).Replace('.ps1', '.psd1')
    This way I don't have to change that snippet at all and it'll work for all scripts I have to provide a config for even when there are more than one in one folder.

    The config file template looks like this:

        Config1 = @{
            Option1 = @{
                Setting1 = 'Cnfg1Optn1Sttng1'
                Setting2 = 'Cnfg1Optn1Sttng2'
            Option2 = @{
                Setting1 = 'Cnfg1Optn2Sttng1'
                Setting2 = 'Cnfg1Optn2Sttng2'
        Config2 = @{
            Option1 = @{
                Setting1 = 'Cnfg2Optn1Sttng1'
                Setting2 = 'Cnfg2Optn1Sttng2'
            Option2 = @{
                Setting1 = 'Cnfg2Optn2Sttng1'
                Setting2 = 'Cnfg2Optn2Sttng2'

    ... and in the Script you can use it for example like this:


    If you use descriptive names for your config options in your data file you can even "read" the script quite clearly.

    I hope that's been understandable. ;-)

    Live long and prosper!


    Monday, April 29, 2019 9:01 PM
  • I've been doing the same thing using XML for the config files:

    <?xml version="1.0"?>
    		<Filter>OperatingSystem -like 'Windows *Server*'</Filter>
    		<DescFilter>Failover cluster virtual network name account</DescFilter>
    		<Subject>Server Hardware Report</Subject>

    Then in the script:

    $WorkingDir = "D:\Powershell\Scripts\ServerInfo"
    $ConfigFile = "ServerHardwareInfo.xml"
    [xml]$Config = Get-Content (Get-Path -RootFolder $WorkingDir -FileName $ConfigFile)
    $Servers = Get-Servers -Filter $Config.Settings.ADOptions.Filter `
                           -SearchBase $Config.Settings.ADOptions.SearchBase `
                           -AddDescFilter $true `
                           -DescFilter $Config.Settings.ADOptions.DescFilter `
                           -OUFilter $Config.Settings.ADOptions.OUFilter `
                           -Enabled ([System.Convert]::ToBoolean($Config.Settings.ADOptions.Enabled)) `
                           -NonDomainServers $NonDomain.Settings.NonDomainServer.Server.Name
    $SMTPServer = $Config.Settings.EmailSettings.SMTPServer
    $SMTPPort = $Config.Settings.EmailSettings.SMTPPort
    $To = $Config.Settings.EmailSettings.To
    $Subject = $Config.Settings.EmailSettings.Subject

    • Proposed as answer by Simon Cutting Tuesday, April 30, 2019 4:33 PM
    Tuesday, April 30, 2019 4:31 PM