Reputation: 2110
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?
Upvotes: 0
Views: 570
Reputation: 2442
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
}
Upvotes: 1