Issue Displaying CSV when called

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 3 months ago.

  • Author
    Posts
  • #30306
    Profile photo of Jay Ramirez
    Jay Ramirez
    Participant

    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.

  • #30308
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Your logic doesn't do anything with the CSV file. Below is an example of how you would implement what your trying to do:

    function Get-Dept {
        param ()
        begin {
            #import the CSV
            $departments = Import-Csv C:\MyDepartments.csv
        }
        process {
            #prompt user for dept
            $dept = Read-Host "What is the department code?"
            #Query object created by CSV
            $query = $departments | where { $_.Code -eq $dept }
            #If the query has no result ($null), then...
            if ($query) {
                Write-Host ("{0} is not a valid entry, try again" -f $dept)
                Get-Dept
            }
        }
        end { $dept }
    }
    
    Get-Dept
    
  • #30310
    Profile photo of Jay Ramirez
    Jay Ramirez
    Participant

    Rob, thank you for your response but 2 things.
    You forgot the ! in the if statement

    if (! $query)
    

    or

    if ($query -eq $Null)
    

    Otherwise it repeats if you get it right, not wrong.

    2nd thing, this doesn't actually fix my actual issue, which is the displaying the CSV. In my code, the CSV is supposed to display every time there is an incorrect input to show valid codes. The first invalid code shows nothing, just jumps back to the prompt. The 2nd invalid entry shows the CSV twice before returning to prompt, each invalid entry after that, finally does what it is supposed to and displays the CSV once, then the prompt.

  • #30311
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Sadly, I had the ! there and removed it. This is what happens when they don't stock my coffee in the office!!

    If you want to display the departments, you could just do this:

    function Get-Dept {
        param ()
        begin {
            #import the CSV
            $departments = Import-Csv C:\MyDepartments.csv
        }
        process {
            #Show valid departments
            $departments
            #prompt user for dept
            $dept = Read-Host "What is the department code?"
            #Query object created by CSV
            $query = $departments | where { $_.Code -eq $dept }
            #If the query has no result ($null), then...
            if (!($query)) {
                Write-Host ("{0} is not a valid entry, try again" -f $dept)
                Get-Dept
            }
        }
        end { $dept }
    }
    
    Get-Dept
    

    I don't know if you have a ton of departments, but you might consider doing something like this to make it a comma separated values displayed to the user:

    "Valid Department Codes:  {0}" -f (($departments | Select -ExpandProperty Code) -join ", ")
    

You must be logged in to reply to this topic.