locked
Issues with function when calling it during variable assignment RRS feed

  • General discussion

  • I have a script that runs several functions one after another.  When I call one of my functions like this:

    $Var1 = Get-Stuff $Var2

    It does not work.  When I output the variables inside the function they appear blank.  I even tried defining some of the variables globally and then not passing them but I got the same result.

    If I just run:

    Get-Stuff $Var2

    The output is exactly as expected, but how do I assign it to a variable to use later!!

    Any input is appreciated.

    • Changed type Bill_Stewart Tuesday, February 10, 2015 1:44 PM Unanswerable drive-by question
    Monday, February 9, 2015 3:00 PM

All replies

  • Your code fragment is too short to draw any conclusions.
    Monday, February 9, 2015 3:09 PM
  • We have no idea what "does not work" means, especially since we do not have the content of the Get-Stuff function.

    I am going to exercise "psychic debugging" and make a guess, though. If you're using Write-Host, then that's why it doesn't work, because Write-Host does not output data to the pipeline (it displays data in the console only).


    -- Bill Stewart [Bill_Stewart]

    Monday, February 9, 2015 3:09 PM
  • Sorry.  I am in fact outputting an object and not using write-host.  Does not work means that the returned object is not being assigned to the variable.  When I would try to send the $var1 to the pipeline to see it's value I got nothing.

    Interestingly enough I copied my function to notepad, made a new one that just passed back the value I had sent to it and it worked.

    So I copied my function back from notepad and it is now returning the object and assigning it as expected.  O_o

    Thanks anyway.

    Monday, February 9, 2015 3:26 PM
  • Of course, it's impossible for others to debug code that they have not seen. Remember that we have no information other than what you posted.

    -- Bill Stewart [Bill_Stewart]

    Monday, February 9, 2015 3:28 PM