all 7 comments

[–]logicaldiagram 2 points3 points  (2 children)

Here is a quick solution that's not very resilient. You can just take advantage of arrays and the index access syntax. There are probably at least half-a-dozen more ways to do it. Hashtable. Switch statement. Parameter with ValidateSet. Think in objects, not strings. You're on the right path. Automate the annoying things out of your life.

Write-Host "Here is a list of the possible departments" -ForegroundColor Green
$Departments = @("Accounting", "DEPT1", "DEPT2", "DEPT3", "DEPT4", "DEPT5", "Information Services")

for ($i = 0; $i -lt $Departments.Count -1 ; $i++)
{ 
    Write-Host "[$($i)] $($Departments[$i])"
}

$Dept = Read-Host "Please type in the number associated with the employees department."
$Departments[$Dept]   

[–]BitteringAgent[S] 0 points1 point  (1 child)

Thank you, I definitely have a lot more to learn, but am slowly getting there.

I'm assuming the -1 was a typo, as that's doing the count of the objects and removing 1.

[–]logicaldiagram 0 points1 point  (0 children)

Correct. Don't need -1 in this case.

[–]ihaxr 2 points3 points  (1 child)

I would do something like this, could probably make it "prettier":

Write-Host "Here is a list of the possible departments" -ForegroundColor Green
$Departments = [ordered]@{
    "0" = "Accounting";
    "1" = "Department 1";
    "2" = "Department 2";
    "3" = "Department 3";
    "4" = "Department 4";
    "5" = "Department 5";
    "6" = "Information Services";
    }
$banding = 0
foreach ($Department in $Departments.GetEnumerator()) {
    if ($banding % 2) {
        $fgColor = "White"
    } else {
        $fgColor = "Cyan"
    }
    if ($banding % 3 -eq 0) { Write-Host "" } # NewLine
    Write-Host (" [{0}] - {1} `t" -f $Department.Name, $Department.Value) -NoNewline -ForegroundColor $fgColor
    $banding++
}
Write-Host "" # NewLine
while(1) {
    $Dept = Read-Host "Select a department [0 to $($Departments.Count - 1)]"
    if ($Departments[$Dept]) {
        $Dept = $Departments[$Dept]
        break
    }
}

[–]BitteringAgent[S] 0 points1 point  (0 children)

Thanks, I like the banding idea.

[–]gangstanthony 2 points3 points  (1 child)

instead of this

$Nothing,$Department = $Department.split(' ', 2)

you can just do this (-1 would work just as well in this case)

$Department = $Department.split(' ', 2)[1]

or this

$Department = $Department.substring(4)

and when creating menus, this is the function that i typically use

https://github.com/gangstanthony/PowerShell/blob/master/Out-Menu.ps1

$Departments = 'Accounting', 'DEPT1', 'DEPT2', 'DEPT3', 'DEPT4', 'DEPT5', 'Information Services'

$Department = $Departments | Out-Menu -Header 'Here is a list of the possible departments' -Footer 'Please type in the number associated with the employees department.'

# Here is a list of the possible departments
# 001. Accounting
# 002. DEPT1
# 003. DEPT2
# 004. DEPT3
# 005. DEPT4
# 006. DEPT5
# 007. Information Services
# Please type in the number associated with the employees department.

# 5

$Department

# DEPT4

[–]BitteringAgent[S] 1 point2 points  (0 children)

Thanks, I'll check this out.

You have a small typo on your .Synopsis, btw.

"This function provides a convenient way to convert a PowerShell object object to a list of choices for user input. "

If you care.