Thursday, October 22, 2015

Calling a PowerShell function with multiple parameters

I don't usually write functions when I write my PowerShell scripts because they are often quick scripts to perform one or a few operations for administration.

However, I recently was assigned a task to write a PowerShell script that would update a large number of nodes in an XML file and I suddenly found myself with a need to write a PowerShell function!

Of course, writing the function was easy enough, however, when it came to actually CALLING the function, I ran into quite a bit of difficulty due to PowerShell's default behavior.

Since PowerShell interprets comma-delimited arguments as an array, when you pass a comma-delimited set of arguments to a PowerShell function, the function is not called correctly!

Instead, you have to "space delimit" your arguments to your PowerShell function or else use named arguments.

Therefore, instead of calling your function in the same manner as you would do in other languages such as C#, you would use this syntax in its place:

Update-XMLNode $xmlFile "xmlnodename" "xmlnodevalue"





No comments:

Post a Comment