1

I'm facing with an annoying problem: my script seemingly doesn't pass any argument to a function I've defined.

$server = 'http://127.0.0.1:8080'

Function Get-WorkingDirectory([string]$address)
{
    #echo $address

    $content = Get-Content -path C:\....\file.txt 
    $content -contains $address
} #end Get-WorkingDirectory function



if(Get-WorkingDirectory $server)
{
    echo "works"
}
else
{
    echo "error"
}

It is stuck on "works". If I try to echo address in the function, it is empty. What the heck I'm doing wrong?! I know this is a pretty noobish question, but I tried everything I found on the net. Thanks in advance for help!

3
  • The code you posted works just fine for me. Commented Apr 27, 2015 at 11:41
  • What do you mean by it is stuck on "works"? Commented Apr 27, 2015 at 11:51
  • @MathiasR.Jessen the if condition is true, then it echoes "works", also if there is no evaluation Commented Apr 27, 2015 at 13:26

2 Answers 2

1

echo is an alias for Write-Output but as you are using the output of the function in the if statement, nothing gets shown.

For testing purposes, use Write-Host in this instance to show the variable being passed correctly.

$server = 'http://127.0.0.1:8080'

Function Get-WorkingDirectory([string]$address)
{
    write-host "$address using write host"
} #end Get-WorkingDirectory function


if (Get-WorkingDirectory $server) {

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

Comments

1

Output of Get-WorkingDirectory is shadowed by if statement.

Try to use it without if and you'll see that argument is passed correctly. For example,

$server = 'http://127.0.0.1:8080'

Function Get-WorkingDirectory([string]$address)
{
    Write-Host $address
} 

Get-WorkingDirectory $server

Address is printed well

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.