Reputation: 18760
I'm trying to add a step to my Octopus Deployment
that will install IIS if it isn't installed on the target, but I can't find anything. If there isn't anything in place at the minute, is there a powershell script that I can use that will add the Web Server (IIS)
role / feature to the machine?
This would make it much easier for firing up new virtual machines with minimal features installed, then when deploying specific applications, they can manage whether IIS is required, rather than manually adding it to the appropriate machines.
Thanks in advance!
Upvotes: 2
Views: 1044
Reputation: 18760
I've just found a solution after digging through the templates available on Octopus, WesleySSmith
has a step template called Windows - Ensure Features Installed
that can be added to your process. It allows you to provice the feature name you wish to install, for example IIS-WebServer
.
For the PowerShell fanatics, the script behind this step as of this moment, looks like this:
$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
Upvotes: 1
Reputation: 4099
You can use the Enable-WindowsOptionalFeature
or Install-WindowsFeature
depending on the machine you are configuring. A Quick Google search led me here
Here's an example to check if IIS is installed and install if it is'nt
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
}
Upvotes: 3