user9850983
user9850983

Reputation: 21

PowerShell: Passed Parameters not in Format String

I'm a PowerShell newbie and I've got stuck on something very simple. I am trying to create a variable that use placeholders ({0}{1}...) using string format (-f). The place holder variables are passed as parameters to the function that builds/formats the string. Unfortunately, the place holders remain blank.

Here's my code:

function SendName(){
  BuildReport $MyInvocation.MyCommand, 1, 2
}

Function BuildReport($functionName, $param1, $param2){    
    $report="You are in {0}. Parameter expected is {1}. Actual parameter is {2}" -f $functionName, $param1, $param2
    write-host $report
}

SendName

The output I get is:

You are in System.Object[]. Parameter expected is . Actual parameter is

Upvotes: 2

Views: 630

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You have to omit the comma (,) where you invoke the BuildReport method:

BuildReport $MyInvocation.MyCommand 1 2

Otherwise you will pass an array as the first parameter and $param1 and $param2 won't get populated.

The Output will be:

You are in SendName. Parameter expected is 1. Actual parameter is 
2

Upvotes: 2

Related Questions