Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use PSFramework to read a config file? #657

Open
danielniccoli opened this issue Feb 18, 2025 · 1 comment
Open

Can I use PSFramework to read a config file? #657

danielniccoli opened this issue Feb 18, 2025 · 1 comment

Comments

@danielniccoli
Copy link

danielniccoli commented Feb 18, 2025

Can PSF be used to read settings from e.g., a config.json file ? The use-case is that I want to provide a configuration file to my automation script. I need a module that simplifies reading the config file, checking the config value, validating it, throwing an error if it is invalid, or provide a default value if the value is missing. It looks like PSF can do all of that, but I have not found a simple example of how I read a value in from a file.

@FriedrichWeinmann
Copy link
Member

Hi @danielniccoli ,
Yeah, I really need to make my docs more discoverable. Also need to add some videos for it.
So, to read a configuration file, there are different "schemata" - definitions, on how a config file is supposed to look like. If the builtin ones don't work for you, you can define your own schema (not for the faint of heart, but possible).

Here's the docs for the one you'll find most useful, given your description. It understands both json and psd1 files, whatever works better for you :)

Import-PSFConfig -Schema MetaJson -Path .\config.json

Note: The path can also be a weblink or blank json string.

Now, to validate & default-value settings, you need to define these settings via Set-PSFConfig. While config initialization was implemented with modules in mind, that is in no way a requirement.
Here's an example file from PSFramework itself.

A minimal example would look like this:

Set-PSFConfig -FullName MyScript.EnableFeature1 -Value $true -Initialize -Validation 'bool'
  • FullName: Name of the configuration setting. Use Get-PSFConfigValue -FullName MyScript.EnableFeature1 to later retrieve it.
  • Value: The value to apply to the setting. In combination with -Initialize this becomes the default value.
  • Initialize: A somewhat complex parameter, that basically means "This call defines the behavior of this setting"
  • Validation: What validation rule to apply to the setting. Will type-convert input if technically legal but in the wrong datatype. If the builtin validation rules are not enough, you can define your own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants