2017-03-03 18 views
1

Powershell相当新颖。我正在开发一个脚本,让用户安装不同的软件。此过程目前正在手动完成,安装所需的所有内容可能需要30-45分钟。但是,并非所有工作站都需要安装,因此我需要为用户提供灵活性。这是我到目前为止所提出的。 (为了简洁起见)如何通过脚本循环让用户选择不同的选项?

$Software1Path = "Path to installer" 
$Software2Path = "Path to installer" 
$Software3Path = "Path to installer" 


function Software1 { 
    $Software1Arguments = "/S" 
    Start-Process -FilePath $Software1Installer $Software1Arguments -Wait 

    InstallOptions 
} 

function Software2 { 
    $Software2Arguments = "/silent" 
    Start-Process -FilePath $Software2Installer $Software2Arguments -Wait 

    InstallOptions 
} 


function Software3 { 
    $Software3Arguments = "/passive" 
    Start-Process -FilePath $Software3Installer $Software3Arguments -Wait 

    InstallOptions 
} 



function InstallOptions { 
    Do { 
     Clear-Host 

     Write-Host('1. Install Software1') 
     Write-Host('2. Install Software1') 
     Write-Host('3. Install Software1') 
     Write-Host('4. Install All Three') 
     Write-Host('0. Exit') 
     $value = Read-Host 'Input your selection (0-3)' 
     } 

    Until ($value -eq "o"){ 
     switch ($value) { 
       "0" {exit} 
       "1" { Software 1} 
       "2" { Software 2} 
       "3" { Software 3} 
       "4" { Software1 
         Software2 
         Software3} 
     } 
    } 

} 

它没有给出期望的结果。我可以安装一个软件,但脚本退出,我也不能安装所有三个。我玩过InstallOptions并以十种不同的方式写出来,但我仍然没有得到理想的结果。有什么建议么?

+1

递归每次你安装的东西 –

+0

那是因为你叫什么InstallOptions将循环更好的方式回到我的身边来了“菜单”? – MBH

+0

好吧,如果我正在做这件事,我会完全重新设计的方法,tbh,没有一点尝试使这更好 – 4c74356b41

回答

1

您可以在while($true) {}中调用InstallOptions(只要您保留0退出)或其他类型的循环使其返回菜单。如果您从软件安装功能调用菜单,则在使用“全部安装”时,它将永远不会达到2和3。尝试:

$Software1Path = "Path to installer" 
$Software2Path = "Path to installer" 
$Software3Path = "Path to installer" 


function Software1 { 
    $Software1Arguments = "/S" 
    Start-Process -FilePath $Software1Path -ArgumentList $Software1Arguments -Wait 
} 

function Software2 { 
    $Software2Arguments = "/silent" 
    Start-Process -FilePath $Software2Path -ArgumentList $Software2Arguments -Wait 
} 


function Software3 { 
    $Software3Arguments = "/passive" 
    Start-Process -FilePath $Software3Path -ArgumentList $Software3Arguments -Wait 
} 



function InstallOptions { 
    Clear-Host 

    Write-Host('1. Install Software1') 
    Write-Host('2. Install Software2') 
    Write-Host('3. Install Software3') 
    Write-Host('4. Install All Three') 
    Write-Host('0. Exit') 
    $value = Read-Host 'Input your selection (0-3)' 

    switch ($value) { 
     "0" {exit} 
     "1" { Software1} 
     "2" { Software2} 
     "3" { Software3} 
     "4" { 
      Software1 
      Software2 
      Software3 
      } 
     } 
} 

#Start the train 
while($true) { InstallOptions } 

您可能还想清理它。防爆。 $Sofware1Path在功能之外,而$Software1Arguments在里面。对于简单的安装,你可以清理它以使用前。一个csv存储的数组(如果需要可以读取存储在一个单独的文件中)。喜欢的东西:

$Installations = @(@" 
Name,Step,Path,Arguments 
Software1,1,"c:\Install Files\Product1\Setup.exe",/S 
Software2,2,"c:\Install Files\Product2\SetupPart2.exe",/silent 
Software2,1,"c:\Install Files\Product2\Setup.exe","/silent ""space test with ,""" 
Software3,1,"c:\Install Files\Product3\Setup.exe",/passive "space test" 
"@ | ConvertFrom-Csv) 


function InstallSoftware ($Name) { 
    Write-Host "Installing $Name..." 
    $Installations | Where-Object { $_.Name -eq $Name } | Sort-Object { $_.Step -as [int] } | ForEach-Object { 
     ExecuteStep -Path $_.Path -Arguments $_.Arguments 
    } 
} 

function ExecuteStep ($Path, $Arguments) { 
    Write-Host "Executing '$Path' '$Arguments'" 
    Start-Process -FilePath $Path -ArgumentList $Arguments -Wait 
} 

function Menu { 
    $UniqueSoftware = $Installations | Select-Object -ExpandProperty Name -Unique | Sort-Object 
    $NumOfSoftware = $UniqueSoftware.Count 

    #Generate menu 
    Clear-Host 
    for($i=0;$i -lt $NumOfSoftware; $i++) { 
     Write-Host ("{0}. Install {1}" -f ($i+1), $Software[$i].Name) 
    } 
    Write-Host ("{0}. Install all" -f ($NumOfSoftware+1)) 
    Write-Host "0. Exit" 

    do { 
     #Get input 
     $value = (Read-Host "Input your selection (0-$($NumOfSoftware+1))") -as [int] 

     #Execute 
     switch ($value) { 
      0 { exit } 
      { $_ -gt 0 -and $_ -le $NumOfSoftware } { InstallSoftware -Name $UniqueSoftware[($_-1)] } 
      ($NumOfSoftware+1) { 0..($NumOfSoftware-1) | ForEach-Object { InstallSoftware -Name $UniqueSoftware[($_)] } } 
      default { Write-Host "Invalid input..." } 
     } 
    #Validate input or retry 
    } until ($value -ge 0 -and $value -le $NumOfSoftware+1) 

} 

#Start the train 
while($true) { Menu } 
+0

谢谢。我一直在一块一块地构建它,但我绝对会看到它的价值,因为它效率更高。我会试一试,看看它是否按我需要的方式工作。 – MBH

+0

没问题。如果你已经为你工作,那么简单的解决方案就是第一个样本中的修复。另一个只是一个更具动态性的替代解决方案,但有很多方法可以做到,正确的解决方案取决于软件。防爆。这可能不是复杂安装脚本的最佳步骤。我实际上再次更新了替代解决方案以支持多个步骤(已排序),以便为您提供多部分安装的想法 –

+0

谢谢,那真是太好了。如果我不用这个脚本,我肯定会用它来创建我需要的第二个脚本。我想逐行阅读,真正理解它在做什么。 – MBH

相关问题