Sunday, January 5, 2014

PowerShell: passing parameters by reference, and assigning values

example:
function ParamTest($ArgA, [ref] $ArgB)
{
    $ArgA = "Hello A!"
    // The following line won't work; it is referencing System.Management.Automation.PSReference
    $ArgB = "Hello B!" 
    // This line is the correct way to assign
   $ArgB.value = "Hello Test!"
    return $ArgA
}
$a = "A.."
$b = "B.."
write-host "a is $a"
write-host "b is $b"
$output = ParamTest -ArgA $a -ArgB ([ref] $b)
write-host "a is now $a"
write-host "b is now $b"


Related Posts Plugin for WordPress, Blogger...