2

我正在尝试向我添加一个步骤,Octopus Deployment如果它没有安装在目标上,它将安装 IIS,但我找不到任何东西。如果目前没有任何东西,是否有我可以使用的 powershell 脚本将Web Server (IIS)角色/功能添加到机器?

这将使启动安装了最少功能的新虚拟机变得更加容易,然后在部署特定应用程序时,他们可以管理是否需要 IIS,而不是手动将其添加到适当的机器上。

提前致谢!

4

2 回答 2

3

您可以使用Enable-WindowsOptionalFeatureInstall-WindowsFeature根据您正在配置的机器。一个快速的谷歌搜索把我带到了这里

这是一个检查 IIS 是否已安装的示例,如果未安装则安装

 if ((Get-WindowsFeature Web-Server).InstallState -eq "Installed") {
        Write-Host "IIS is installed on $vm"
    } 
    else {
        Write-Host "IIS is not installed on $vm"
        Write-Host "Installing IIS.."
        Install-WindowsFeature -name Web-Server -IncludeManagementTools
    }
于 2018-08-22T10:59:39.040 回答
1

在挖掘 Octopus 上可用的模板后,我刚刚找到了一个解决方案,WesleySSmith它有一个名为的步骤模板Windows - Ensure Features Installed,可​​以添加到您的流程中。它允许您提供要安装的功能名称,例如IIS-WebServer.

对于 PowerShell 狂热者来说,此时此步骤背后的脚本如下所示:

$requiredFeatures = $OctopusParameters['WindowsFeatures'].split(",") | foreach { 

$_.trim() }
if(! $requiredFeatures) {
    Write-Output "No required Windows Features specified..."
    exit
}
$requiredFeatures | foreach { $feature = DISM.exe /ONLINE /Get-FeatureInfo /FeatureName:$_; if($feature -like "*Feature name $_ is unknown*") { throw $feature } }

Write-Output "Retrieving all Windows Features..."
$allFeatures = DISM.exe /ONLINE /Get-Features /FORMAT:List | Where-Object { $_.StartsWith("Feature Name") -OR $_.StartsWith("State") } 
$features = new-object System.Collections.ArrayList
for($i = 0; $i -lt $allFeatures.length; $i=$i+2) {
    $feature = $allFeatures[$i]
    $state = $allFeatures[$i+1]
    $features.add(@{feature=$feature.split(":")[1].trim();state=$state.split(":")[1].trim()}) | OUT-NULL
}

Write-Output "Checking for missing Windows Features..."
$missingFeatures = new-object System.Collections.ArrayList
$features | foreach { if( $requiredFeatures -contains $_.feature -and $_.state -eq 'Disabled') { $missingFeatures.add($_.feature) | OUT-NULL } }
if(! $missingFeatures) {
    Write-Output "All required Windows Features are installed"
    exit
}
Write-Output "Installing missing Windows Features..."
$featureNameArgs = ""
$missingFeatures | foreach { $featureNameArgs = $featureNameArgs + " /FeatureName:" + $_ }
$dism = "DISM.exe"
IF ($SuppressReboot)
{
    $arguments = "/NoRestart "
}
ELSE
{
    $arguments = ""
}
$arguments = $arguments + "/ONLINE /Enable-Feature $featureNameArgs"
IF ($Source)
{
    if (!(Test-Path $Source)) {
        throw "Could not find the file $Source or access denied"
    }
    $arguments = $arguments + " /Source:$Source"
}
Write-Output "Calling DISM with arguments: $arguments"
start-process -NoNewWindow $dism $arguments
于 2018-08-22T12:21:26.327 回答