Posted On December 16, 2022

Use DISM To Install Windows Features

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> Use DISM To Install Windows Features

Deployment Image Servicing and Management (DISM.exe) is a command-line tool that can be used to service and prepare Windows images, according to Microsoft. Although this tool predates PowerShell, it is often invoked from PowerShell sessions to manage the OS. Here is a script as an illustration of this usefulness.

# dismInstallFeatures.ps1

$featuresToInstall = ('FileAndStorage-Services','Storage-Services','Web-Server','Web-WebServer','Web-Common-Http','Web-Default-Doc','Web-Dir-Browsing','Web-Http-Errors',
'Web-Static-Content','Web-Http-Redirect','Web-Health','Web-Http-Logging','Web-Log-Libraries','Web-ODBC-Logging','Web-Request-Monitor',
'Web-Http-Tracing','Web-Performance','Web-Stat-Compression','Web-Dyn-Compression','Web-Security','Web-Filtering','Web-Basic-Auth',
'Web-Client-Auth','Web-Digest-Auth','Web-Cert-Auth','Web-IP-Security','Web-Url-Auth','Web-Windows-Auth','Web-App-Dev','Web-Net-Ext',
'Web-Net-Ext45','Web-ASP','Web-Asp-Net','Web-Asp-Net45','Web-CGI','Web-ISAPI-Ext','Web-ISAPI-Filter','Web-Includes','Web-WebSockets',
'Web-Ftp-Server','Web-Ftp-Service','Web-Mgmt-Tools','Web-Mgmt-Console','Web-Mgmt-Compat','Web-Metabase','Web-Lgcy-Mgmt-Console','Web-Lgcy-Scripting',
'Web-WMI','Web-Scripting-Tools','Web-Mgmt-Service','NET-Framework-Features','NET-Framework-Core','NET-Framework-45-Features','NET-Framework-45-Core',
'NET-Framework-45-ASPNET','NET-WCF-Services45','NET-WCF-HTTP-Activation45','NET-WCF-TCP-PortSharing45','RSAT','RSAT-Feature-Tools','RSAT-SMTP',
'RSAT-SNMP','FS-SMB1','SMTP-Server','SNMP-Service','User-Interfaces-Infra','Server-Gui-Mgmt-Infra','Server-Gui-Shell','PowerShellRoot','PowerShell',
'PowerShell-V2','PowerShell-ISE','WAS','WAS-Process-Model','WAS-Config-APIs','WoW64-Support')

# Remove any feature that is not available to prevent dism failures. MSMQ-Container for example is only available 
# on Windows 7 but not on Windows Server 2008 R2. 
$availableFeatures = dism /online /Get-Features
$featuresToRemove = @()
$featuresToInstall | % { if (-not ($availableFeatures | Select-String ('{0}$' -f $_) -Quiet)) { $featuresToRemove += $_} }
$featuresToInstall = Compare-Object -ReferenceObject $featuresToInstall -DifferenceObject $featuresToRemove | select -ExpandProperty InputObject

$dismParameter = @('/online', '/Enable-Feature', ($featuresToInstall | % { '/FeatureName:{0}' -f $_ }), '/NoRestart', '/all')
$output = dism @dismParameter

# throw an error if dism wasn't successful
if ($global:LastExitCode -ne 0){
    throw 'Error while installing Windows Features. {0}' -f ($output | Select-String '\.log$')
}
# dismCheckWindowsFeature.ps1

$featuresToCheck = ('RDS-RD-Server')
$availableFeatures = dism /online /Get-Features
$featuresToRemove = @()
$featuresToInstall | % { if (-not ($availableFeatures | Select-String ('{0}$' -f $_) -Quiet)) { $featuresToRemove += $_} }
$featuresToInstall = Compare-Object -ReferenceObject $featuresToInstall -DifferenceObject $featuresToRemove | select -ExpandProperty InputObject

$dismParameter = @('/online', '/Get-Features', ($featuresToCheck | % { '/FeatureName:{0}' -f $_ }), '/NoRestart', '/all')

$output = dism @dismParameter

# throw an error if dism wasn't successful
if ($global:LastExitCode -ne 0)
{
    throw 'Error while installing Windows Features. {0}' -f ($output | Select-String '\.log$')
}

dism /online /get-Features 'RDS-RD-Server'

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Special Parameter datatype [switch]

# Demo of a simple function to turn lights on and offfunction setLight{ param( [switch]$on,…

PowerShell: Encapsulating Function Inside a Function and Inside an Invoke-Command or Job

$domain='google.com' function pingSomething($x){ $result=ping $x function getTraceroute($y){ pathping $y } $result+=getTraceroute $x return $result }…

Basic JavaScript: Access Multi-Dimensional Arrays With Indexes

// Setupvar myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];// Using bracket notation select an…