Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

have the following function:

function appendToSB([System.Text.StringBuilder]$sb,
                    [string]$value){
    [void]$sb.append($value)
    $sb
}

$sb = new-object -typename system.text.stringbuilder
$sb = appendToSb($sb, "1,")

$sb.tostring() | out-host

i want to build string using StringBuilder using my function for that, but i receive the following error:

appendToSB : Cannot process argument transformation on parameter 'sb'. Cannot convert the "System.Object[]" value of ty pe "System.Object[]" to type "System.Text.StringBuilder". At E:\powershell\test.ps1:8 char:11 + appendToSb([system.text.stringbuilder]$sb, "1,") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [appendToSB], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,appendToSB

does anybody can explain how function/function parameter/return values works in powershell?

share|improve this question

1 Answer

up vote 3 down vote accepted

Classic PowerShell issue. You don't use parens or comma separated args when calling commands or functions e.g.:

appendToSb $sb "1,"

You only use that syntax when calling .NET methods. If you use Set-StrictMode -Version 2 it will catch this sort of issue. What you passed ($sb, "1,") is how you would pass an array to a single parameter. Technically the parens aren't needed but don't change the value i.e. you could pass an array like this as well $sb, ",".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.