使PowerShell比if语句更高效
我这里有一个脚本,大部分都能完成它的工作。我不熟悉PowerShell脚本,所以我正在尝试从外部了解我应该更改什么 脚本的第一部分询问用户是否要安装该程序使PowerShell比if语句更高效,powershell,loops,if-statement,Powershell,Loops,If Statement,我这里有一个脚本,大部分都能完成它的工作。我不熟悉PowerShell脚本,所以我正在尝试从外部了解我应该更改什么 脚本的第一部分询问用户是否要安装该程序 $<application> = Read-Host -Prompt 'Would you like to install <>? Please type Yes or No' 这将导致一个if-else语句 if ( $<application> -eq 'Yes' ) { start-process
$<application> = Read-Host -Prompt 'Would you like to install <>? Please type Yes or No'
这将导致一个if-else语句
if ( $<application> -eq 'Yes' )
{
start-process <application.exe>
Start-Sleep -s 30
}
else
{
Write-Host "Installation of <Application> was skipped"
}
我之所以开始睡眠是因为它一次打开一个应用程序。您有30秒的时间来设置应用程序,这看起来效率不高
我的问题是
在没有大量if语句的情况下,有没有办法做到这一点?我知道有一个csv文件的方法,但我正在寻找替代品。我喜欢脚本询问您是否应该安装程序的方式
应用程序完成后,是否仍有停止启动-睡眠过程的方法?所以用户不会因为一个应用程序而感到匆忙?
多谢各位
在没有大量if语句的情况下,有没有办法做到这一点
确定-将应用程序组织到一个有序的字典中,并循环浏览条目:
$applications = [ordered]@{
"App One" = "path\to\application1.exe"
"App Two" = "path\to\application2.exe"
# ...
}
foreach($appName in $applications.Keys){
$response = Read-Host -Prompt "Would you like to install '${appName}'? Please type Yes or No"
if($response -eq 'yes'){
Start-Process -Path $applications[$appName]
Start-Sleep -Seconds 30
} else {
Write-Host "Installation of '${appName}' was skipped"
}
}
应用程序完成后,是否仍有停止启动-睡眠过程的方法
是,使用启动进程-等待,而不是在设定的持续时间内睡眠:
Start-Process -Path $applications[$appName] -Wait
在没有大量if语句的情况下,有没有办法做到这一点
确定-将应用程序组织到一个有序的字典中,并循环浏览条目:
$applications = [ordered]@{
"App One" = "path\to\application1.exe"
"App Two" = "path\to\application2.exe"
# ...
}
foreach($appName in $applications.Keys){
$response = Read-Host -Prompt "Would you like to install '${appName}'? Please type Yes or No"
if($response -eq 'yes'){
Start-Process -Path $applications[$appName]
Start-Sleep -Seconds 30
} else {
Write-Host "Installation of '${appName}' was skipped"
}
}
应用程序完成后,是否仍有停止启动-睡眠过程的方法
是,使用启动进程-等待,而不是在设定的持续时间内睡眠:
Start-Process -Path $applications[$appName] -Wait
这太棒了!非常适合我想要的@科里伯顿,不客气!请考虑通过点击左边的复选标记来标记我的答案,这太棒了!非常适合我想要的@科里伯顿,不客气!请考虑通过单击左边的复选标记来标记我的答案。