安装IIS作为Octopus Deploy中的一个步骤

时间:2018-08-22 10:47:52

标签: powershell iis octopus-deploy octopus

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

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

谢谢!

2 个答案:

答案 0 :(得分:3)

您可以根据所配置的计算机使用Enable-WindowsOptionalFeatureInstall-WindowsFeature。快速的Google搜索使我here

这里是一个示例,用于检查是否已安装IIS并安装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
    }

答案 1 :(得分:1)

在深入研究了章鱼可用的模板之后,我才找到了解决方案,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