haddona
haddona

Reputation: 3

How to number an array and output to screen

Semi-new to powershell and having a hard time finding what I am wanting to do. Basically I am seeing if it is possible, or if anyone knows how to take an array and number them for user input. Example would be if I am running a get-aduser command and it returned 3 results, can I somehow number those results for later use. I am working on a script for our help desk that lets you search for a user based on display name, adds that to an array, then prints that in a numbered order on the screen where you can select that for more involved tasks.

Thanks and hope that makes sense.

Upvotes: 0

Views: 355

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36297

You can do that with a For loop, like this:

$Results = Get-ADUser -Filter "name -like '*Joe*'"
For($i = 0; $i -lt $Results.Count; $i++)
{
    "{0}. {1} {2} ({3})" -f (1+$i), $Results[$i].GivenName, $Results[$i].SN, $Results[$i].samAccountName
}

That would end up with something like:

1. Joe Dirt (JDirt)
2. Joe Pesci (JPesci)

...and so on. The formatting of the data you want to display is often times the hard part. If you wanted to easily reference that later you should build a hashtable to go with it so that you can easily reference it later.

$Results = @{}
Get-ADUser -Filter "name -like '*Joe*'" |ForEach-Object -begin {$x=0} -process 
{
    ++$x
    $Results.Add($x,$_)
    "{0}. {1} {2} ({3})" -f ($x), $_.GivenName, $_.SN, $_.samAccountName
}

That would display the same, but later you could reference $Results[2] to work with the Joe Pesci record as it was displayed on the screen.

You didn't ask for it, but I keep a function on hand to make menus like that. I build hashtables like that, then use this:

Function MenuMaker{
    param(
        [parameter(Mandatory=$true,
        ValueFromPipeline = $true)][String[]]$Selections,
        [string]$Title = $null
        )

    $Width = if($Title){$Length = $Title.Length;$Length2 = $Selections|%{$_.length}|Sort -Descending|Select -First 1;$Length2,$Length|Sort -Descending|Select -First 1}else{$Selections|%{$_.length}|Sort -Descending|Select -First 1}
    $Buffer = if(($Width*1.5) -gt 78){[math]::floor((78-$width)/2)}else{[math]::floor($width/4)}
    if($Buffer -gt 6){$Buffer = 6}
    $MaxWidth = $Buffer*2+$Width+$($Selections.count).length+2
    $Menu = @()
    $Menu += "╔"+"═"*$maxwidth+"╗"
    if($Title){
        $Menu += "║"+" "*[Math]::Floor(($maxwidth-$title.Length)/2)+$Title+" "*[Math]::Ceiling(($maxwidth-$title.Length)/2)+"║"
        $Menu += "╟"+"─"*$maxwidth+"╢"
    }
    For($i=1;$i -le $Selections.count;$i++){
        $Item = "$(if ($Selections.count -gt 9 -and $i -lt 10){" "})$i`. "
        $Menu += "║"+" "*$Buffer+$Item+$Selections[$i-1]+" "*($MaxWidth-$Buffer-$Item.Length-$Selections[$i-1].Length)+"║"
    }
    $Menu += "╚"+"═"*$maxwidth+"╝"
    $menu
}

Then it's just:

MenuMaker -Selections ($Results.Values|ForEach("{1} {2} ({3})" -f $_.GivenName, $_.SN, $_.samAccountName) -Title "Choose a user"

And it spits out this:

╔═════════════════════════════╗
║        Choose a user        ║
╟─────────────────────────────╢
║    1. Joe Dirt (JDirt)      ║
║    2. Joe Pesci (JPesci)    ║
╚═════════════════════════════╝

Upvotes: 2

Related Questions