FailingFrank
FailingFrank

Reputation: 23

Cant use variable from a function in another function

function User-Search($input)
{
    Write-Host "Searching for user: $input"
    pause
}


function Show-Menu
{
     param (
           [string]$Title = 'MainMenu'
     )
     cls
     Write-Host "================ $Title ================"
     Write-Host " "
     Write-Host "Specify computer / username"
     Write-Host " "
     Write-Host "Q: Press 'Q' to quit."
     Write-Host " "
}

do
{
    Show-Menu
    $input = Read-Host "Search"
    User-Search -input $input
}
until ($input -eq 'q')

Outputs: "Searching for user:", it's empty.

There must be some small mistake i am doing, probably easy for you guys :)

Upvotes: 1

Views: 35

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

$INPUT is an automatic variable:

Contains an enumerator that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions).

So just use another variable, e. g. $user instead of $input

Upvotes: 3

Related Questions