I am creating a script to somewhat automate our new user setup. Part of it is selecting their dept and I decided to go with a dept code.
I’m using a CSV that lists Location, Dept, and the coinciding code. I’m using a switch statement based on the input, with the default for incorrect inputs displaying the csv and looping back to the input request.
The issue is, the first time I input an invalid code, nothing displays and it loops, the 2nd time, it displays the CSV contents twice, and each time after it displays once (as it should).
import-csv .\Data\DeptCodes.csv
function DeptCheck
{
#$DC
"Please enter Department Code: " #-ForegroundColor Red -NoNewline
[string]$dept = Read-Host
Check -x $dept
switch ($dept)
{
"NJQA" { }
"NJQC" { }
"NJWH" { }
"LVMC" { }
"LVAD" { }
"LVTR" { }
"LVQA" { }
"FLAC" { }
"FLAD" { }
"FLBS" { }
"FLBI" { }
"FLMC" { }
"FLEX" { }
"FLHR" { }
"FLMG" { }
"FLDM" { }
"FLPD" { }
"FLQC" { }
"FLSM" { }
"FLTC" { }
"FLTR" { }
"FLPU" { }
"FLBL" { }
"FLCR" { }
"FLRS" { }
"FLSH" { }
"FLQA" { }
"RMOW" { }
default {$DC; DeptCheck}
}
}
In regard to the loop method, I may use a while loop with the input being checked against $DC.Code column instead of the current.
I’ve also been reading about not using Write-Host, recently, and have tried various other output methods for the beginning Prompt, but that doesn’t seem to affect the CSV.
I also considered maybe there wasn’t enough time for the CSV to load and have tried pauses and combining this with the rest of the functions I’ve created so far, that take place before this step. Nothing seems to help so far.