2017-03-03 50 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

相關問題