Home > OS >  Powershell Show-Menu script not getting any result from get-ADuser
Powershell Show-Menu script not getting any result from get-ADuser

Time:03-31

I'm not getting any result from user's input.. but this message: Get-ADUser : Cannot validate argument on parameter 'Identity'

My apologies, I'm just a starter in PS. what am I doing wrong here? I'm trying to get the input from the user in to the Get-ADUser -Identity $Username but it seems that it's not taking the input..

function Show-Menu {

    
    param (
        [string]$Title = 'UserInfo V3.1'
    )

    Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
    $UserName = Read-Host
    Write-Host ""
    
    Write-Host "================ $Title ================"
    
    Write-Host "Press 'U' for general user info."
    Write-Host "Press 'P' for account and password information."
    Write-Host "Press 'C' for computer info."
    Write-Host "Press 'G' for Virtual Applications (AVC)."
    write-Host "Press 'S' for SNOW info details"
    Write-Host "Press 'Q' to  Quit."
    write-Host "=============================================="
}


do
{
    Show-Menu 
    $Selection = Read-Host "Please make a selection" 
    
    switch ($Selection) 
    {

    'U' {Get-ADUser -Identity $Username}
  
    }
    Pause
 }
 until ($Input -eq 'q')

CodePudding user response:

There are 2 problems with your code, the first one, $UserName is defined and only exists in the scope of your function Show-Menu, to correct this, you can have the call to Read-Host inside your function but don't have it assigned to any variable so we can capture the user name on each function call ($userName = Show-Menu). The second problem is your use of the automatic variable $input on until ($input -eq 'q').

function Show-Menu {
    param (
        [string]$Title = 'UserInfo V3.1'
    )

    Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
    Read-Host # This can be output from your function
    Write-Host ""   
    Write-Host "================ $Title ================"   
    Write-Host "Press 'U' for general user info."
    Write-Host "Press 'P' for account and password information."
    Write-Host "Press 'C' for computer info."
    Write-Host "Press 'G' for Virtual Applications (AVC)."
    write-Host "Press 'S' for SNOW info details"
    Write-Host "Press 'Q' to  Quit."
    write-Host "=============================================="
}

do {
    $userName = Show-Menu # which is captured here
    $Selection = Read-Host "Please make a selection"
    switch ($Selection) {
        'U' { Get-ADUser -Identity $Username; pause }
    }
} until ($Selection -eq 'q')
  • Related