I wrote a simple function to create a hashtable out of an xml-file which will hold params that should be passed to a cmdlet.
My XML-File looks like this:
<params>
<Parameter>
<Name>After</Name>
<Value>(get-date).adddays(-7)</Value>
</Parameter>
<Parameter>
<Name>Log</Name>
<Value>System</Value>
</Parameter>
</params>
My function looks like this:
function Create-ParamTable {
param ([string]$ConfigFile,[string]$Root = "params", [string]$Child = "Parameter")
$hash = @{}
[xml]$config = get-content $ConfigFile
foreach ($param in $config.$root.$child) {
$hash.add($param.name,$param.value)
}
return $hash
}
I’m using that returned hashtable with the splat-operator:
PS > $h = create-paramtable -configfile c:\tmp\params.xml ; get-eventlog @h
I want to be able to pass scriptblocks as parameter-value in order to use other cmdlets like get-date to calculate a few values.
For example: I want to store params for get-eventlog in a xml-config-file but I always want to have the logs from the past 7 days.
How do I have to store the value in order to get it executed when passing it to a cmdlet via splatting?
You need to evaluate the parameter values before sticking them in the hashtable. Something like this.