0

With my following code seems Write-Host puts out variables in a strange way (for me coming from C# at least).

Code is here

function Run(
[string] $command,
[string] $args
)
{
    Write-Host 'from function - command is:' $command '.args is: ' $args
}

$cmd = "ping"
$args = "208.67.222.222"
Write-Host 'from main - command is:' $cmd '.args is: ' $args
Run("ping","208.67.222.222")

Output is here

from main - command is: ping .args is:  208.67.222.222
from function - command is: ping 208.67.222.222 .args is:  

How come Write-Host from main works as I expect, but within the function it outputs all variables at the same time? How can I correct this behaviour?

1
  • 1
    Actually, your function call is wrong. It should be Run "ping" "208.67.222.222". Function calls in PowerShell do not use parentheses and commas. Commented Apr 14, 2013 at 13:49

1 Answer 1

1

$args in the function is an automatic variable. It is an array that contains all the arguments passed to the function.

Use something besides $args for your IP address variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.