Powershell variable is assigned the result of a function AND the parameter I passed to the function -


i'm running on , on in script. have line of code:

$errors = get-deploymenterrors $uniqueid 

when runs, $errors assigned result of get-deploymenterrors , value of $uniqueid. want $errors assigned result of get-deploymenterrors.

here get-deploymenterrors function:

function get-deploymenterrors($uniqueid) { $errors = @()  $conn = new-object -typename system.data.sqlclient.sqlconnection $conn.connectionstring = 'removed connection string'  $cmd = new-object -typename system.data.sqlclient.sqlcommand $cmd.connection = $conn $cmd.commandtext = "removed sql statement" $cmd.parameters.addwithvalue("@uniqueid", $uniqueid)  $conn.open() $reader = $cmd.executereader()  if($reader.hasrows) {     while ($reader.read())     {         $error = new-object -typename psobject          $error | add-member -membertype noteproperty -name stepid -value $reader["stepid"]         $error | add-member -membertype noteproperty -name deploymentid -value $reader["deploymentid"]         $error | add-member -membertype noteproperty -name messageid -value $reader["messageid"]         $error | add-member -membertype noteproperty -name severity -value $reader["severity"]         $error | add-member -membertype noteproperty -name message -value $reader["message"]         $error | add-member -membertype noteproperty -name stepname -value $reader["stepname"]         $error | add-member -membertype noteproperty -name currentstep -value $reader["currentstep"]         $error | add-member -membertype noteproperty -name totalsteps -value $reader["totalsteps"]         $error | add-member -membertype noteproperty -name currenttime -value $reader["currenttime"]          $errors += $error     } }  return $errors } 

$cmd.parameters.addwithvalue() echoes added parameter, , powershell functions return entire non-captured output on success output stream, not argument of return keyword.

quoting about_return (emphasis mine):

short description
exits current scope, can function, script, or script block.

long description
return keyword exits function, script, or script block. can used exit scope @ specific point, return value, or indicate end of scope has been reached.

users familiar languages c or c# might want use return keyword make logic of leaving scope explicit.

in windows powershell, the results of each statement returned output, without statement contains return keyword. languages c or c# return value or values specified return keyword.

use of following methods suppress undesired output:

  • [void]$cmd.parameters.addwithvalue("@uniqueid", $uniqueid)
  • $cmd.parameters.addwithvalue("@uniqueid", $uniqueid) | out-null
  • $cmd.parameters.addwithvalue("@uniqueid", $uniqueid) > $null
  • $param = $cmd.parameters.addwithvalue("@uniqueid", $uniqueid)

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -