0

Powershell, if and elseif, and blank csv fields

Hi everyone.

This one has me scratching my head.

I have a csv file called logonscripts.csv which looks like

| Name         | Company_3LC | Office_3LC | Department_3LC |
| ------------ | ----------- | -----------|--------------- |
| common.bat   | CY1         | LON        |                |
| accounts.bat | CY1         | LON        | ACC             |
| other.bat    | CY2         | MAN        | ITS             |

The idea is for the code to read through the CSV and select the relevant .bat file depending on company, location and optionally the department.

My code is currently this:

$logonscripts = Import-Csv "$($datadir)\logonscripts.csv"

foreach($logonscript in $logonscripts){
    if($UserCompany3LC -eq $logonscript.Company_3LC -and $UserOffice3LC -eq $logonscript.Office_3LC -and $logonscript.Department_3LC -eq "" -and $UserDepartment3LC -ne $logonscript.Department_3LC){
        $UserLogonScript = $logonscript.Name
        Write-Host "Blank Department?"
        Write-Host "Company 3LC = "$logonscript.Company_3LC
        Write-Host "Office 3LC = "$logonscript.Office_3LC
        Write-Host "Department 3LC = "$logonscript.Department_3LC
        Write-Host "Logon Script = "$logonscript.Name
    }
    elseif($UserCompany3LC -eq $logonscript.Company_3LC -and $UserOffice3LC -eq $logonscript.Office_3LC -and $UserDepartment3LC -eq $logonscript.Department_3LC){
        $UserLogonScript = $logonscript.Name
        Write-Host "Not Blank Department?"
        Write-Host "Company 3LC = "$logonscript.Company_3LC
        Write-Host "Office 3LC = "$logonscript.Office_3LC
        Write-Host "Department 3LC = "$logonscript.Department_3LC
        Write-Host "Logon Script = "$logonscript.Name
    }      
}

but when I run it, it doesn't behave as I would expect:

Not Blank Department?
Company 3LC =  CY1
Office 3LC =  LON
Department 3LC =  ACC
Logon Script =  accounts.bat
Blank Department?
Company 3LC =  CY1
Office 3LC =  LON
Department 3LC =
Logon Script =  common.bat

If I were to append something like

else{
     $UserLogonScript = $null
    }

after the elseif statment the variable would output as blank!

I have a couple of questions:

  1. Why would both if and elseif output in the console if elseif should only run if the previous if statement is FALSE?

  2. How come the output lists Not Blank Department? first when it is listed second in the elseif statement?

Any advice would be greatly appreciated.

3
  • Where is $UserCompany3LC coming from? The variable would have to be assigned to before you conditional statements to $null again so it doesn't hold value: $UserLogonScript = $null; if (...) {...} elseif (.... Commented Aug 17, 2022 at 17:37
  • if and elseif bocks are mutually exclusive - there must be something else going on that the code in the question doesn't show. The order in which the blocks execute in successive calls solely depends on which condition matches first. Commented Aug 17, 2022 at 18:10
  • $UserCompany3LC, $UserOffice3LC and $UserDepartment3LC are all defined by user selection of forms in an earlier part of the script. These are set and aren't relevant to the problem. There isn't anything else going on elsewhere in the script related to this afaik. The script is ~650 lines of code so I wouldn't expect anyone to go through all of it to figure it out but I'm confident it isn't being affected. I still don't have an understanding of why both statements are running. If it weren't for both running I'm pretty sure I would achieve the desired result. Commented Aug 18, 2022 at 8:46

1 Answer 1

1

You could try putting the elseif on the same line as the closing curly brace: ...} elseif () {.... But I think your logic would be orders of magnitude simpler if you did not use if/else syntax. Try using the PowerShell objects to your advantage.

Assuming you use params to define those variables in your code:

param(
  $UserCompany3LC,
  $UserOffice3LC,
  $UserDepartment3LC,
  $datadir
)

$logonscripts = Import-Csv "$($datadir)\logonscripts.csv"

$choice = $logonscripts | Where-Object {
  $_.UserCompany3LC    -eq $UserCompany3LC -and
  $_.UserOffice3LC     -eq $UserOffice3LC  -and
  $_.UserDepartment3LC -eq $UserDepartment3LC
}

if ($Choice) {$choice} else {
  Write-Error "No records found for given parameter values"
}

Sign up to request clarification or add additional context in comments.

5 Comments

The simplification is helpful, but note that the if block's closing } is not required to be on the same line as the elseif (if it were, not doing so would cause a syntax error).
Thanks for this. I'm about to try this code to see how it shakes out, quick question though, does this take in to account that sometimes logonscripts.Department_3LC can be blank?
Just gave it a whirl, and if all 3 variables are present on the csv, it works! but unfortunately it will write out the error if a department_3lc is missing. Not a total deal breaker as I could edit the csv to include multiple entries for each department that should have a "common.bat" logon script instead of "accounts.bat" but I'd rather have a code solution to save on overheard, hence my initial if/else approach
I have a habit of putting if/else and braces on the same line for one reason: when copy/pasting the logic into a shell it will fail otherwise. OP didn't mention how he ran the code, and I added that suggestion in case he was copy/pasting, but didn't want to get into details. The presence of errors when values are blank should be handled in the script. As is there is no handling so I would expect lots of errors.
It should work because null will equal null, unless you aren't really null or you have a space in there. You can try changing all of the $_.UserXxxxxx3LC -eq ... to $_.UserXxxxxxx3LC.Trim() -eq ... and see if that works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.