Solution
You're not invoking the function how you think you are. PowerShell functions are called without parentheses and the argument delimiter is whitespace, not a comma. Your function call should look like this:
postReq $url $params
What is wrong with using traditional C#-like syntax?
Calling it like you are above as postReq($url, $params)
has two undesired consequences here:
The parentheses indicate a sub-expression, the code in the parentheses will run first before the outer code and be treated as a single argument. If you are familiar with solving algebraic equations, the order of operations is the same as in PEMDAS - parentheses first.
Whitespace (
), and NOT commas (,
) are the argument delimiter to Powershell functions. However, the commas do mean something in Powershell syntax - they signify a collection. [1, 2, 3, 4]
is functionally the same as 1, 2, 3, 4
in Powershell. In your case above, you are rolling both parameters into a single array argument of [$url, $params]
, which the stream-writing cmdlets will do an array join with a ,
as the delimiter in the rendered string.
But what about object instance and static methods?
This can be confusing to some because object instance and class (RE: static) methods ARE called with the traditional C#-like syntax, where you DO need the parentheses to indicate parameter values, and commas are the delimiter. For example:
([DateTime]::Now).ToString()
returns the current local time, and runs the ToString() method on the returned DateTime
object. If you used one of its overloads as shown below, you would separate each argument with a ,
and regardless of whether you need to pass in arguments or not, you still must specify the outer parentheses:
OverloadDefinitions
-------------------
string ToString()
string ToString(string format)
string ToString(System.IFormatProvider provider)
string ToString(string format, System.IFormatProvider provider)
string IFormattable.ToString(string format, System.IFormatProvider formatProvider)
string IConvertible.ToString(System.IFormatProvider provider)
If you were to omit the parentheses on the empty parameter overload above, you would get the preceding output showing the overload definitions, unlike the behavior when calling functions with no argument.
It's a little odd, but I remember that in any programming language, functions and methods are similar, but distinct, and it's no different in Powershell other than functions and methods are less alike than they are in other languages. I find a good rule of thumb for this is:
Invoke methods with C# syntax and functions with shell syntax.