1

I am very new to Powershell, Started today actually, and I need some help with getting a command to display output. The command works when run from powershell but when I try to run from my script I pasted it seems to fail. I've even changed the variable field to my username and it still fails.

Get-ADUser jdoe -Properties Description

Like I said it works when run in powershell but not when run in a script.

Clear-Host

$script:ChosenFunction=Get-function  #Get-Choice
 

function Get-function
{
    Write-Host "1. IP (future addon)"
    write-host "2. UserInfo"
    write-host "3. CopyZ (Future addon)"
    write-host "4. Local Users (Future addon)"
    Write-Host "X. Exit"

    $Action=Read-Host "Choose an action:"
    Switch ($Action)
    {
        1 {$Choice="IP"}
        2 {$Choice="UserInfo"}
        3 {$Choice="CopyZ"}
        4 {$Choice="Local Users"}
        5 {$Choice="Exit"}
    }
    
    write-host "Function chosen is $Choice"

    if ($Action -eq 2)
    {
        write-host "if is working"
        $script:ChosenFunction=userInfo
    }

}

function userInfo
{
    cls
    $Name=Read-Host "Enter user name: "
    Write-Host "finding $Name"

    Get-ADUser $Name -Properties Description # <---- Here's my problem get user's AD information
}

2 Answers2

0

There is too much to say about this script so let's focus on your main issue...

First thing is that you need to declare the functions before the main part of script. If you call a function before it is declared (written) in the script, it will fail at call time.

Second thing, as mentioned in the comments is that you cannot expected an output on screen as you assign the result of Get-ADUser to your variable $Script:ChoosenFunction... and you do it twice. See comment below:

Clear-Host

function Get-function
{
    Write-Host "1. IP (future addon)"
    write-host "2. UserInfo"
    write-host "3. CopyZ (Future addon)"
    write-host "4. Local Users (Future addon)"
    Write-Host "X. Exit"

    $Action=Read-Host "Choose an action"
    Switch ($Action)
    {
        1 {$Choice="IP"}
        2 {$Choice="UserInfo"}
        3 {$Choice="CopyZ"}
        4 {$Choice="Local Users"}
        5 {$Choice="Exit"}
    }
    
    write-host "Function chosen is $Choice"

    if ($Action -eq 2)
    {
        write-host "if is working"
        $script:ChosenFunction=userInfo # Assign the value to $script:ChosenFunction but function doesn't return any value
        return $script:ChosenFunction # return value to caller (that's missing).
    }
}

function userInfo
{
    cls
    $Name=Read-Host "Enter user name"
    Write-Host "finding $Name"

    Get-ADUser $Name -Properties Description # <---- Here's my problem get user's AD information
}

$script:ChosenFunction=Get-function  #Why reassigning the value to $script:ChosenFunction also here?
# Display the content of $script:ChosenFunction
$script:ChosenFunction

# Or simply call the Get-Function as the value of $script:ChosenFunction is already set within the function itself
Get-Function

Here is now an example if the same script with less code:

Clear-Host

function Get-function {
    Write-Host "1. IP (future addon)" ;
    write-host "2. UserInfo" ;
    write-host "3. CopyZ (Future addon)" ;
    write-host "4. Local Users (Future addon)" ;
    Write-Host "X. Exit" ;

    Switch (Read-Host "Choose an action") {
        1 {$Choice="IP"}
        2 {$Choice="UserInfo";userInfo}
        3 {$Choice="CopyZ"}
        4 {$Choice="Local Users"}
        5 {$Choice="Exit"}
    }
    
    Write-Host "Function choosen is: $Choice" ;
}

function userInfo {
    Clear-Host ;
    $Name = Read-Host "Enter user name" ;
    Write-Host "finding $Name" ;
    Get-ADUser $Name -Properties Description ;
}

$script:ChosenFunction = Get-function ;
# Display Result
$script:ChosenFunction ;
ZivkoK
  • 366
  • 3
  • 6
0

Very impressive for a first day! By reading the comments I understand you're already aware of why userInfo was not producing any output, here is how you can simplify your code and maybe help learn something new. I added comments to help you we the thought process.

function Show-Menu {
# Simple function to Show a Menu each time it's called
    Clear-Host
    Write-Host "1. IP (future addon)"
    write-host "2. UserInfo"
    write-host "3. CopyZ (Future addon)"
    write-host "4. Local Users (Future addon)"
    Write-Host "X. Exit"
}

function Get-UserInfo {
# Function to get a user from AD
param(
    [parameter(Mandatory)]
    [string]$User
)
    try
    {
        Get-ADUser $Name -Properties Description
    }
    catch
    {
        $PSCmdlet.WriteError($_)
    }
}

function WaitForInput {
# Stops the loop until a Key is pressed
    $null = [console]::ReadKey()
}

do  # Start a loop
{
    
    Show-Menu # Show the Menu
    $Action = Read-Host "Choose an Action" # Get input from user
    Switch ($Action) # Switch the input to see which option was chosen
    {
        1 { "IP" }
        2 {
            # If option 2 was chosen
            $Name = Read-Host "Enter User Name" # Ask for User to Find
            Write-Host "Finding $Name" # Display the user to Find
            Get-UserInfo -User $Name # Call the Function passing the user as Argument
            WaitForInput # Wait here until a Key is Pressed
        }
        3 { "CopyZ" }
        4 { "Local Users" }
    }
} until($Action -eq 'X') # Loop until X was chosen
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37