Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions in Powershell - can they be called within a string?

I'm loving how functions can vastly reduce the busywork within Powershell scripts, and they've saved me a ton of redundant coding. But I'm now having issues invoking a declared function within a Powershell string (as part of a concatenated string using '+' sign) and wondering if there is a trick to doing this. Some sample code:

#this function takes input and either returns the input as the value, or returns 
placeholder text as the value

function val ($valinput)
{ if ($valinput)
   {return $valinput}
   else
   {$valinput = "No Results!"; return $valinput}
}

If I call the function at the beginning of the line or by itself:

val("yo!")

It runs fine. But if I attempt to concatenate it as part of a string, for example:

"The results of the tests are: " + val($results)

Powershell seems to have problems executing the function there, and I get 'You must provide a value expression on the right-hand side of the '+' operator.' and 'Unexpected token 'val' in expression or statement.' errors.

Is there a way to properly call a function within the concatenated string? I know I can push the results of the function to another variable and concatenate the resulting variable as part of the string, but that would be cumbersome to do that each and every time I call this function. Thanks in advance...!

like image 723
KTM Avatar asked May 17 '17 22:05

KTM


People also ask

Can you have a function within a function in PowerShell?

In a standard Windows PowerShell script or function, names of nested functions can be reused. Calls invoke the last function defined with the specific name before the call.

How do functions work in PowerShell?

A function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces. The function shown is a simple example that returns the version of PowerShell.

How do you use a variable inside a string in PowerShell?

PowerShell has another option that is easier. You can specify your variables directly in the strings. $message = "Hello, $first $last." The type of quotes you use around the string makes a difference.

How do you call another function in PowerShell?

To call another function, you need to use the Invoke-Command cmdlet and pass in the argument using the ArgumentList parameter like below.


1 Answers

Wrap the command/function call in a subexpression inside an expandable string:

"The results of the test are: $(val "yo!")"

Also worth pointing out, the syntax for command invocation in PowerShell doesn't require parentheses. I would discourage using parentheses like you do in the example altogether, since you'll end up in situations where consecutive arguments are being treated as one:

function val ($inputOne,$inputTwo)
{
    "One: $inputOne"
    "Two: $inputTwo"
}

Now, using C#-like syntax, you'd do:

val("first","second")

but find that the output becomes:

One: first second
Two: 

because the PowerShell parser sees the nested expression ("first","second") and treats it as a single argument.

The correct syntax for positional parameter arguments would be:

val "first" "second"
like image 155
Mathias R. Jessen Avatar answered Oct 27 '22 15:10

Mathias R. Jessen