Flying Thunder
Flying Thunder

Reputation: 940

Powershell - Store function output in variable not working

stuck at this one trying to store function output in a variable:

function AD-prompt($Text)
{
do
{
$in = read-host -prompt "$Text"
}
while($in -eq "")
}

calling the function with

$type = AD-prompt "Sample Text"

does not store anything in $type - only when i remove the entire do-while loop it works. it seems the function output is empty as the read-host output is stored in the $in variable, but i have no idea how to solve this - i didnt find another way to loop the read-host aswell sadly.

Upvotes: 1

Views: 444

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23355

You need to return $in from your function by outputting it. You can do this by putting it on a line on its own after your loop:

function AD-prompt($Text)
{
    do
    {
       $in = read-host -prompt "$Text"
    }
    while($in -eq "")

    $in
}

Upvotes: 4

Related Questions