0

I read somewhere that the param section must be the very first thing that appears in a script or function so this is what I have come up with in order to set the default values of each of the params. Yes, it is unorthodox, but it works.

Param (
    [Xml]$xmlObj = (Get-Content "Download-VBK_config.xml"),
    [String]$dlFrom = $xmlObj.Configuration.Download.From,
    [String]$dlTo = $xmlObj.Configuration.Download.To,
    [String]$exTo = $xmlObj.Configuration.Extract.To
)

However, is there a better way I could go about setting a param's default value by loading values from an XML file?

1 Answer 1

1

You can leave the parameters without default values, then look at the $PSBoundParameters variable to see what parameters were passed in, and fill the ones in that we're not passed.

Param(
    [string]$Param1,
    [string]$Param2)
[xml]$defaults =Get-Content file.xml
if(!$PSboundParameters.ContainsKey("Param1"))
{
    $Param1 = $defaults.Configuration.Defaults.Param1
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.