Smitty_Werbs
Smitty_Werbs

Reputation: 1

PowerShell Question on Printing Variables inside of Strings

I am new to Powershell.

function weekendplans($a,$b,$c)
{
    $message = "This is a $a $b and an $c"
    $message
}
weekendplans("pear", "apple", "orange")

When I run this code the output I get is This is a pear apple orange and an

I know that I can simply use the string format to fix this but I was curious why this was happening in the first place. Thanks!

Upvotes: 0

Views: 63

Answers (2)

TheGameiswar
TheGameiswar

Reputation: 28940

you should call like below

function weekendplans($a,$b,$c)
{
$message = "This is a $a $b and an $($c)"
$message
}
weekendplans "pear" "apple" "orange"

with the way you passed, all arguments are assigned to $a

function weekendplans($a,$b,$c)
{

Write-Host "a is $a"
Write-host "b is $b"
Write-host "c is $c"

$message = "This is a $a $b and an $c"
$message
}
PS C:\> weekendplans("pear", "apple", "orange")

running above gives me

a is pear apple orange
b is 
c is 
This is a pear apple orange  and an 

Correct way to Pass Parameters in power shell is through space separated and not by comma

if you are not aware, you can call .NetClasses in powershell ,they are called comma seperated.Below is one example

[System.DateTime]::DaysInMonth(2020,6)

Parenthesised arguments are used in .NET methods only

Parameters in calls to functions in PowerShell (all versions) are space-separated, not comma separated. Also, the parentheses are entirely unneccessary and will cause a parse error in PowerShell 2.0 (or later) if Set-StrictMode is active. Parenthesised arguments are used in .NET methods only.

Further reading and References:

How do I pass multiple parameters into a function in PowerShell?

Upvotes: 4

postanote
postanote

Reputation: 16116

Along with @TheGameiswar's answer, you can take a poor man's debug approach, using variable squeezing to assign to a variable and output to screen at the same time. So, step into what you are trying one segment at a time...

function weekendplans($a,$b,$c)
{
($message = "This is a $a $b and an $c")
}
weekendplans('pear', 'apple', 'orange')
<#
# Results

The function or command was called as if it were a method. 
Parameters should be separated by spaces. 
For information about parameters, see the about_Parameters 

Help topic.
At line:6 char:1
+ weekendplans('pear', 'apple', 'orange')
...
#>

function weekendplans($a,$b,$c)
{
($message = "This is a $a $b and an $c")
}
weekendplans 'pear', 'apple', 'orange'
<#
# Results

This is a pear apple orange  and an 
#>

function weekendplans($a,$b,$c)
{
    ($message = "This is a $a")
    ($message = "This is a $b")
    ($message = "This is a $c")
}
weekendplans 'pear', 'apple', 'orange'
<#
# Results

This is a pear apple orange
This is a 
This is a 
#>

function weekendplans($a,$b,$c)
{
    ($message = "This is a $a")
    ($message = "This is a $b")
    ($message = "This is a $c")
}
weekendplans 'pear' 'apple' 'orange'
<#
# Results

This is a pear
This is a apple
This is a orange
#>

Upvotes: 1

Related Questions