< Back

Import-ConfigFile

Mon Jan 13, 2020 4:21 am

NAME Import-ConfigFile



SYNOPSIS

Sets script parameters and variables using the values in a config file.





SYNTAX

Import-ConfigFile [-ConfigFilePath <String>] [-DefaultParameters <Hashtable>] [<CommonParameters>]



Import-ConfigFile [-ConfigFilePath <String>] [-Format <String>] [-DefaultParameters <Hashtable>]

[<CommonParameters>]



Import-ConfigFile [-ConfigFilePath <String>] [-Json] [-DefaultParameters <Hashtable>] [<CommonParameters>]



Import-ConfigFile [-ConfigFilePath <String>] [-Ini] [-DefaultParameters <Hashtable>] [<CommonParameters>]





DESCRIPTION

Use this function to load script settings from a config file.



The function will iterate through elements of the config file and, if the value

is set, it will use it to set the script parameter or variable with the same

name.



For the sample of the config file and the explanation of the file structure,

see online help.





PARAMETERS

-ConfigFilePath <String>

Path to the config file. If not specified, the config file is considered

optional (if the default config file is not found, no error will be returned).

The default config file is named after the calling script with the appended

extension reflecting the format, e.g. for the script with the name 'MyScript.ps1',

the default JSON config file will be 'MyScript.ps1.json' located in the same

folder.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Format <String>

Either Json or Ini.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Json [<SwitchParameter>]

Shortcut for '-Format Json'.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Ini [<SwitchParameter>]

Shortcut for '-Format Ini'.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-DefaultParameters <Hashtable>

Holds a hashtable with parameters that will not be imported. To ignore parameters

initialized via command line pass the $PSBoundParameters variable. When passing

a custom hashtable, you only need to specify the keys.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Import-ConfigFile



Checks if the default JSON config file exists, and if so, loads settings from

the file into the script variables.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Import-ConfigFile -Ini



Checks if the default INI config file exists, and if so, loads settings from

the file into the script variables.









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Import-ConfigFile -ConfigFilePath "C:\\Scripts\\MyScript.ps1.DEBUG.json"



Loads settings from the specified JSON config file into the script variables.









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Import-ConfigFile -ConfigFilePath "C:\\Scripts\\MyScript.ps1.DEBUG.ini"



Loads settings from the specified INI config file into the script variables.









-------------------------- EXAMPLE 5 --------------------------



PS C:\\>Import-ConfigFile -DefaultParameters $PSBoundParameters



Loads settings from the default config file into the script variables

ignoring parameters explicitly passed via command line.











RELATED LINKS

https://github.com/alekdavis/ConfigFile