Skip to content

Instantly share code, notes, and snippets.

@markcagatandavis
Last active June 22, 2023 08:25
Show Gist options
  • Save markcagatandavis/e44400a7c6246768b5b56f662fcf7136 to your computer and use it in GitHub Desktop.
Save markcagatandavis/e44400a7c6246768b5b56f662fcf7136 to your computer and use it in GitHub Desktop.
Install & Configure the required roles and features for MECM
# Define the log file
$logFile = "C:\Windows\Temp\FeatureInstall.log"
# Define the source files location for .NET Framework 3.5
$dotNet35InstallerPath = "D:\sources\sxs" # replace with your actual path
# Define the source file location for .Net Framework 4.8
$dotNet48InstallerPath = "F:\Installers\ndp48-x86-x64-allos-enu.exe"
# Define the features to install
$features = @(
"Web-Windows-Auth", # Windows Authentication
"Web-ISAPI-Ext", # ISAPI Extensions
"Web-Metabase", # Metabase
"Web-WMI", # IIS 6 Management Compatibility
"RDC", # Remote Differential Compression
"Web-Asp-Net", # ASP.NET
"Web-Asp-Net45", # ASP.NET 4.5
"NET-HTTP-Activation", # HTTP Activation
"NET-Non-HTTP-Activ", # Non-HTTP Activation
"BITS", # BITS Server Extensions
"UpdateServices", # WSUS
"UpdateServices-DB" # WSUS with SQL Server database
)
# Function to log messages
function Write-Log {
param (
[Parameter(Mandatory=$true)]
[string] $Message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logFile -Value "$timestamp - $Message"
}
try {
# Check if .NET Framework 3.5 feature is not already installed
if (!(Get-WindowsFeature -Name NET-Framework-Features).Installed) {
# Install the .NET Framework 3.5 feature with source files
Install-WindowsFeature -Name NET-Framework-Features -Source $dotNet35InstallerPath -ErrorAction Stop
Write-Log "Successfully installed NET-Framework-Features"
} else {
Write-Log "NET-Framework-Features is already installed"
}
} catch {
Write-Log "Failed to install NET-Framework-Features: $_"
}
# Check if .NET Framework 4.8 is already installed
$dotNetVersionKey = "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"
if (Test-Path $dotNetVersionKey) {
$dotNetVersion = (Get-ItemProperty -Path $dotNetVersionKey -Name Release).Release
if ($dotNetVersion -ge 528040) {
Write-Log ".NET Framework 4.8 is already installed."
} else {
# Install .NET Framework 4.8
$process = Start-Process -FilePath $dotNet48InstallerPath -ArgumentList "/q /norestart" -PassThru -Wait
$process.WaitForExit()
# Check the exit code of the installation
$exitCode = $process.ExitCode
if ($exitCode -eq 0) {
Write-Log "Successfully installed .NET Framework 4.8."
}
if ($exitCode -eq 5100) {
Write-Log ".Net Framework 4.8 is already installed. Exit code: $exitCode"
} else {
Write-Log "Failed to install .NET Framework 4.8. Exit code: $exitCode"
}
}
}
# Loop through each other feature
foreach ($feature in $features) {
try {
# Check if the feature is not already installed
if (!(Get-WindowsFeature -Name $feature).Installed) {
# Install the feature
Install-WindowsFeature -Name $feature -IncludeManagementTools -ErrorAction Stop
Write-Log "Successfully installed $feature"
} else {
Write-Log "$feature is already installed"
}
} catch {
Write-Log "Failed to install $feature : $_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment