henaree
henaree

Reputation: 21

Powershell, if and elseif, and blank csv fields

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.

Upvotes: 0

Views: 199

Answers (1)

tonypags
tonypags

Reputation: 135

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"
}

Upvotes: 1

Related Questions