1

I'm tinkering with a PowerShell script that will handle some files for me (which is a good excuse to finally learn PowerShell!) and in the script I have a set of basic logging functions that log to the screen in a standard format, e.g. LogOk($message), LogInfo($message), etc.

I also have this function, which is intended to let me log some of the settings to the console the same way:

function LogSetting($varName, $varVal)
{
    echo " Setting    $varName  =  «$varVal»"
}

(Note: The other functions are single-parameter, this is the only one with two parameters)

Example call:

$myVariableName = "example content"
LogSetting("myVariableName", $myVariableName)

The problem is the console output for that call looks like this:

 Setting    myVariableName example content  =  «»

But it should look like this:

 Setting    myVariableName  =  «example content»

I've tried various combinations of formats, even things like this:

function LogSetting($varName, $varVal)
{
    echo " Setting    $varName  /  $varVal   blah"
}

Which then produces this:

 Setting    myVariableName example content  /     blah

I even went so far as to do this to try and isolate the two parameters in the function to see if I could figure out what is causing the problem:

function LogSetting($varName, $varVal)
{
    echo " Setting.... "
    echo "             $varName"
    echo "             $varVal"
}

But then the output looks like this:

 Setting....
         myVariableName example content
 (a blank line is here)

(Yes there is a blank line at the end of the output, it won't show up unless I type something in there)

Clearly something is messed up, but only for this function. All other functions correctly write output to the console, even with the angle brackets (which I use to wrap paths, to make accidental leading/trailing spaces obvious).

Why is this one function not writing to the console properly?

Dave
  • 1,057
  • 2
  • 12
  • 18

1 Answers1

4

Call with LogSetting "myVariableName" $myVariableName.

When you pass with ("myVariableName", $myVariableName), you are essentially calling with 1 parameter which is an array, and the 2nd parameter is null, and since you do not use param and [Parameter(Mandatory=$true)], you do not see an error.

Note: debugging in the ISE and setting a breakpoint and inspecting the variable would help you troubleshoot this type of stuff in the future.

Hit Line breakpoint on 'D:\test\t1.ps1:3'
[DBG]: PS C:\Windows\System32\WindowsPowerShell\v1.0>> $varname
myVariableName
example content

[DBG]: PS C:\Windows\System32\WindowsPowerShell\v1.0>> $varname.Count
2

[DBG]: PS C:\Windows\System32\WindowsPowerShell\v1.0>> $varVal

[DBG]: PS C:\Windows\System32\WindowsPowerShell\v1.0>> $varVal -eq $null
True
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • I always wondered why calling a function with () parameters didn't do what I expected. – Gerard Sexton Mar 25 '16 at 04:22
  • Ah ok thanks will try that tonight when I get home. I haven't really messed with arrays or hashtables or custom objects yet so I wasn't aware of that issue but now it makes sense. Thanks! – Dave Mar 25 '16 at 14:59
  • Ha what an annoying-yet-incredibly-useful capability -- though I would have expected it to error out with no whitespace between the function name token and the expression. But I've gathered that powershell has some odd characteristics that work in its favor once you learn them. Thanks! :) – Dave Mar 25 '16 at 23:47