Kernel
Kernel

Reputation: 105

Powershell parameter passing to function

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!

Upvotes: 1

Views: 66

Answers (2)

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58431

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) {

}

Upvotes: 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

Upvotes: 1

Related Questions