I want to call an existing commandlet with a dynamic number of parameters.
So instead of doing this (taking write-host as an example), I would like to do it the smart way.
# these are the dynamic parameters which maybe get passed into my function or script
# they would be $null be default of course
$forecolor = 'Green'
$newline = $true
# now build the "dynamic" write-host...
if ($forecolor) {
if ($newline) {
write-host -fore $forecolor "Hello world"
}
else {
write-host -fore $forecolor "Hello world" -nonewline
}
}
else {
if ($newline) {
write-host "Hello world"
}
else {
write-host "Hello world" -nonewline
}
}
This of course is very ugly. Help me make it prettier!
I already tried just setting $forecolor = '-fore Green' which only outputs "-fore Green Hello world". I could think of passing a list of arguments to a function and for each argument in the list add the according parameter - I just don't know how to hold the parameters.