我正在尝试使用我的powershell elseif语句并将它们转换为switch语句以使我的代码更加优化,但我无法弄清楚如何执行此操作。
非常感谢任何帮助。
代码:
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
}
答案 0 :(得分:1)
正如其他评论已经指出的那样,您可以使用if/else
和switch
完全相同。
关于优化:如果有办法获取所有已安装的应用程序的列表,我宁愿一次获取该列表,然后切换/大小写,而不是多次调用Cmdlet Get-InstalledApplication
。
优化代码的另一种方法是使用带有搜索词和可执行文件的地图/字典,然后遍历键并检查是否安装了与该键对应的应用程序:
$map = @{};
$map["%Civil 3D 2015%"] = 'Civil3D_2015_OE_64Bit.exe';
$map["%MEP 2015%"] = 'AutoCAD_Architecture_MEP_2015_OE_x64.exe';
$map["%AutoCAD 2015%"] = 'Civil3D_2015_OE_64Bit.exe';
foreach($key in $map.Keys)
{
if(!(Get-InstalledApplication -Name $key)) { continue; };
<# install your application #>;
<# Exeute-Process -Path $map[$key] ... #>
}
我根据@TheMadTechnician的反馈编辑了我的答案
答案 1 :(得分:1)
简单来说,这相当于你的If / ElseIf链:
Switch($true){
{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
continue
}
{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
continue
}
{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
continue
}
{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
continue
}
{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
continue
}
}
你确实意识到你正在安装相同的应用程序,无论你检测到什么,除了一个案例,对吧?如果是我,我想我会使用哈希表,其中键是您要搜索的应用程序标题,值包含可执行文件的路径和输出日志中使用的标题。我认为这会让这个变得更干净。
$Apps = @{
'%AutoCAD 2015%' = [pscustomobject]@{'Path'='Civil3D_2015_OE_64Bit.exe';'Title'='Civil 3D 2015 OE'}
'%MEP 2015%' = [pscustomobject]@{'Path'='AutoCAD_Architecture_MEP_2015_OE_x64.exe';'Title'='Civil 3D 2015 OE'}
'%Civil 3D 2015%' = [pscustomobject]@{'Path'='Civil3D_2015_OE_64Bit.exe';'Title'='Civil 3D 2015 OE'}
'%Civil 3D 2017%' = [pscustomobject]@{'Path'='Civil3D_2015_OE_64Bit.exe';'Title'='Civil 3D 2015 OE'}
'%AutoCAD 2017%' = [pscustomobject]@{'Path'='Civil3D_2015_OE_64Bit.exe';'Title'='Civil 3D 2015 OE'}
}
Switch($Apps.Keys){
{Get-InstalledApplication -Name $_} {
#Install AutoCAD 2015 Object Enabler
$exitCode = (Execute-Process -Path $Apps[$_].Path -Parameters '/s /v /qn,', -wait -passthru).ExitCode
"The exit code for the $($Apps[$_].Title) is: $exitCode" | Out-File -Append -FilePath C:\Logs\PW-CE\install.log
break
}
}
break
行使得当其中一个匹配时,它会退出Switch
。