I am trying to take my powershell elseif statements and convert them to a switch statement to make my code more optimized, but am unable to figure out how to do so.
Any help would be greatly appreciated.
Code:
If (Get-InstalledApplication -Name "%AutoCAD 2015%") {
#Install AutoCAD 2015 Object Enabler
$exitCode = (Execute-Process -Path 'Civil3D_2015_OE_64Bit.exe' -Parameters '/s /v /qn,', -wait -passthru).ExitCode
"The exit code for the Civil 3D 2015 OE is: $exitCode" | Out-File -Append -FilePath C:\Logs\PW-CE\install.log
}
ElseIf (Get-InstalledApplication -Name "%MEP 2015%") {
#Install AutoCAD 2015 Object Enabler
$exitCode = (Execute-Process -Path 'AutoCAD_Architecture_MEP_2015_OE_x64.exe' -Parameters '/s /v /qn,', -wait -passthru).ExitCode
"The exit code for the Civil 3D 2015 OE is: $exitCode" | Out-File -Append -FilePath C:\Logs\PW-CE\install.log
}
ElseIf (Get-InstalledApplication -Name "%Civil 3D 2015%") {
#Install AutoCAD 2015 Object Enabler
$exitCode = (Execute-Process -Path 'Civil3D_2015_OE_64Bit.exe' -Parameters '/s /v /qn,', -wait -passthru).ExitCode
"The exit code for the Civil 3D 2015 OE is: $exitCode" | Out-File -Append -FilePath C:\Logs\PW-CE\install.log
}
ElseIf (Get-InstalledApplication -Name "%Civil 3D 2017%") {
#Install AutoCAD 2015 Object Enabler
$exitCode = (Execute-Process -Path 'Civil3D_2015_OE_64Bit.exe' -Parameters '/s /v /qn,', -wait -passthru).ExitCode
"The exit code for the Civil 3D 2015 OE is: $exitCode" | Out-File -Append -FilePath C:\Logs\PW-CE\install.log
}
ElseIf (Get-InstalledApplication -Name "%AutoCAD 2017%") {
#Install AutoCAD 2015 Object Enabler
$exitCode = (Execute-Process -Path 'Civil3D_2015_OE_64Bit.exe' -Parameters '/s /v /qn,', -wait -passthru).ExitCode
"The exit code for the Civil 3D 2015 OE is: $exitCode" | Out-File -Append -FilePath C:\Logs\PW-CE\install.log
}
switchvsif-elsedoesn't really "optimize" in powershell; it's just a little easier to read in cases where you have a lot of possibilities to test for.