3

I'm writing a set of PowerShell scripts to monitor the size of various folders. I've run into an error, and I've got no idea what's causing it.

Here is the code, with Write-Host showing what I am expecting and what the variables $ip and $loc actually contain:

function getDriveLetter($ip) {
    Write-Host $ip    # prints:   192.168.10.10 myfolder1\myfolder2\
                      # expected: 192.168.10.10
    switch($ip) {
        "192.168.10.10" {return "E`$"; break}
        "192.168.10.20" {return "D`$"; break}
        default {"Unknown"; break}
    }
}

function getFullPath($loc,$folder) {
    Write-Host $loc    # prints:   192.168.10.10 myfolder1\myfolder2\
                       # expected: 192.168.10.10
    $drive = getDriveLetter("$loc")
    $str = "\\$loc\$drive\DATA\$folder"
    return $str
}

function testPath($loc,$folder) {
    $mypath = getFullPath("$loc","$folder")
    if (Test-Path $mypath) {
        return $true
    } else {
        return $false
    }
}

When I run the command:

testPath("192.168.10.10","myfolder1\myfolder2\")

I'm getting a "False" result, but if I run:

Test-Path "\\192.168.10.10\E`$\DATA\myfolder1\myfolder2\"

The command returns True (as it should).


What have I missed? I've tried forcing the variables to be set with:

$mypath = getFullPath -loc "$loc" -folder "$folder"

but there's no change. If it changes anything, this is on Powershell version 4.

G42
  • 9,791
  • 2
  • 19
  • 34
Scott Gardner
  • 75
  • 1
  • 9

3 Answers3

3

I'd suggest that you review the syntax of PowerShell a bit more, because there's many mistakes in there. PowerShell is quite different from C# and you seem to make a lot of assumptions. :)

First of all, that's not how you call PowerShell functions. Also not sure why you added quotes around the parameters? They are redundant. If you fix the function calls your code should function as expected.

$mypath = getFullPath $loc $folder

Then there's a semicolon in your switch statement, which is also wrong. Then, you don't have to escape the $ if you just use ''. The break is also redundant because return exits the function in this case.

"192.168.10.10" { return 'E$' }

Also, one interesting thing about PowerShell: You could just get rid of the return in getFullPath:

function getFullPath($loc, $folder) {
    $drive = getDriveLetter($loc)
    "\\$loc\$drive\DATA\$folder"
}

PowerShell returns uncaptured output, which is important to be aware of, it can be the cause of many obscure bugs.

Thomas Glaser
  • 1,670
  • 1
  • 18
  • 26
  • This solved it, thank you so much! I'll also update the script (and quite a few others actually) with your other suggestions. I do wish Powershell supported multiple arguments with parenthesis, I feel like the space separated arguments doesn't look as clean. – Scott Gardner Aug 11 '17 at 15:25
2

The problem is in how you are calling your functions. Function arguments are space delimited in PowerShell, and do not use parentheses to enclose the arguments.

getFullPath $loc $folder

When you wrap arguments in parentheses, you are creating an array containing two values, and passing that array as the first argument.

getFullPath($loc, $folder)

This line passes an array containing two strings @($loc, $folder) as the first argument, and then, because there are no other arguments on the line, it passes $null to the second. Inside the function, the array is then joined to be used as a string, which is the behavior you observed.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
2

The problem is how you pass the parameters to the functions. See more details on above link: How do I pass multiple parameters into a function in PowerShell?

function getDriveLetter() {
    param($ip)
    switch($ip) {
        "192.168.0.228" {return "E`$"; break}
        "192.168.10.20" {return "D`$"; break}
        default {"Unknown"; break}
    }
}

function getFullPath() {
    param($loc, $folder)
    $drive = getDriveLetter -ip $loc
    $str = "\\$loc\$drive\DATA\$folder"
    return $str
}

function testPath() {
    param($loc, $folder)
    $mypath = getFullPath -loc $loc -folder $folder
    if (Test-Path $mypath) {
        return $true
    } else {
        return $false
    }
}
testPath -loc "192.168.10.10" -param "myfolder1\myfolder2\"
Krisz
  • 701
  • 7
  • 23