I’m trying to enable a number of Windows Features for IIS.
To accomplish that, I have a PowerShell script that works when I run it manually.
in deploy-application.ps1, I try to run it by using the following command:
Execute-Process -Path ‘Powershell’ -Parameters “-Command & { & "$dirFiles\Support\EneableIIS.ps1
”; Exit `$LastExitCode }" -Wait
The file is located in a subfolder “Support” that resides inside the folder “Files”.
I get no errors while running this command, however, the Windows features are not being enabled.
The ps1 script I want to run is the following:
==================================================================================
$Hostname = Hostname
$iisSite = “Default Web Site”
$iis = Get-WmiObject Win32_Service -ComputerName $hostname -Filter “name=‘IISADMIN’”
## Install IIS with features
if ($iis.State -eq "Running") {
Write-Host "IIS is running on $hostname"
}
else {
Write-Host "IIS is not running on $hostname"
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole, IIS-WebServer, IIS-CommonHttpFeatures, IIS-HttpRedirect, NetFx4Extended-ASPNET45, IIS-NetFxExtensibility45, IIS-Security, IIS-WebServerManagementTools, IIS-ManagementConsole, IIS-BasicAuthentication, IIS-DefaultDocument, IIS-ASPNET45
}
## Create certificate with expiration date 10 years from execution date and add certificate to local machinbe store ##
## Add certificate to trusted root ##
$cert = New-SelfSignedCertificate -DnsName "$hostname" -CertStoreLocation "cert:\LocalMachine\My" -KeyAlgorithm RSA -KeyLength 2048 -NotAfter (Get-Date).AddMonths(120)
$DestStore = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::Root,"localmachine")
$DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$DestStore.Add($cert)
$DestStore.Close()
dir cert:\localmachine\my
$cert = (Get-ChildItem cert:\LocalMachine\My | where-object { $_.Subject -like "*$hostname*" } | Select-Object -First 1).Thumbprint
$cert
"Cert Hash: " + $cert
# http.sys mapping of ip/hostheader to cert ##
$guid = [guid]::NewGuid().ToString("B")
netsh http add sslcert hostnameport="${hostname}:443" certhash=$cert certstorename=MY appid="$guid"
# iis site mapping ip/hostheader/port to cert - also maps certificate if it exists ##
# for the particular ip/port/hostheader combo ##
New-WebBinding -name $iisSite -Protocol https -HostHeader $hostname -Port 443 -SslFlags 1
==================================================================================
Is there a way to enable the needed Windows Features for IIS directly by using dism or something like that?