Majestic view of a serene lake surrounded by Tatra Mountains under a cloudy sky in Poland.

Automating MECM Package Creation with PowerShell

Introduction

This automates the creation of a Microsoft Endpoint Configuration Manager (MECM) package for upgrading 11 Education to Enterprise. It streamlines the process of defining package settings, creating the package, adding a program to run a , and distributing the content to distribution points.

Use Case

This script is beneficial for:

  • Efficient Package Deployment: Automating the creation and distribution of MECM packages, saving time and reducing manual effort.
  • Standardizing Deployments: Ensuring consistency in package configurations and deployments.
  • Upgrading Windows Editions: Facilitating the upgrade of Windows 11 Education to Enterprise edition across multiple devices.

Script

PowerShell

# ---------------------------------------------
# Script: Create-MECMPackage.ps1
# Purpose: Create a MECM/SCCM package to upgrade Windows 11 Education to Enterprise
# ---------------------------------------------

# Define Variables
$SiteCode = "<Your MECM Site Code>"  
$PackageName = "Education to Enterprise License Swap"
$PackageDescription = "Deploys a PowerShell script to upgrade Windows 11 Education to Enterprise using a MAK key."
$PrimarySourcePath = "\\<Your Primary Server>\Sources\Scripts"  
$AlternatePath = "S:\Sources\Scripts"  
$ScriptFileName = "EducationToEnterprise.ps1" 
$ProgramName = "Run Education to Enterprise License Swap"
$ProgramCommand = "powershell.exe -ExecutionPolicy Bypass -File `"$SourceFolderPath\$ScriptFileName`""
$DistributionPointGroup = "All Distribution Points" 

# Define the path to the ConfigurationManager.psd1 module
$ConfigMgrModulePath = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"

# Import the ConfigurationManager module
try {
    Import-Module $ConfigMgrModulePath -ErrorAction Stop
    Write-Host "ConfigurationManager module imported successfully." -ForegroundColor Green
}
catch {
    Write-Host "Failed to import ConfigurationManager module: $_" -ForegroundColor Red
    exit 1
}

# Connect to the MECM Site
try {
    Set-Location "$SiteCode`:"
    Write-Host "Connected to MECM site: $SiteCode" -ForegroundColor Green
}
catch {
    Write-Host "Failed to connect to MECM site: $_" -ForegroundColor Red
    exit 1
}

# Determine the Source Folder Path
if (Test-Path $PrimarySourcePath) {
    $SourceFolderPath = $PrimarySourcePath
    Write-Host "Using primary source path: $SourceFolderPath" -ForegroundColor Green
}
elseif (Test-Path $AlternatePath) {
    $SourceFolderPath = $AlternatePath
    Write-Host "Using alternate source path: $SourceFolderPath" -ForegroundColor Green
}
else {
    Write-Host "Neither '$PrimarySourcePath' nor '$AlternatePath' are accessible." -ForegroundColor Red
    exit 1
}

# Check if the Source Folder Exists
if (-not (Test-Path -Path $SourceFolderPath)) {
    Write-Host "Source folder does not exist at $SourceFolderPath. Creating it now..." -ForegroundColor Yellow
    try {
        New-Item -Path $SourceFolderPath -ItemType Directory -Force
        Write-Host "Source folder created successfully." -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to create source folder: $_" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "Source folder already exists at $SourceFolderPath." -ForegroundColor Green
}

# Verify the Script Exists in the Source Folder
$FullScriptPath = Join-Path -Path $SourceFolderPath -ChildPath $ScriptFileName
if (-not (Test-Path -Path $FullScriptPath)) {
    Write-Host "Script '$ScriptFileName' does not exist in $SourceFolderPath." -ForegroundColor Red
    Write-Host "Please ensure the script is placed in the source folder before running this script." -ForegroundColor Yellow
    exit 1
} else {
    Write-Host "Script '$ScriptFileName' found in $SourceFolderPath." -ForegroundColor Green
}

# Check if the Package Already Exists
$ExistingPackage = Get-CMPackage -Name $PackageName -ErrorAction SilentlyContinue
if ($ExistingPackage) {
    Write-Host "Package '$PackageName' already exists in MECM." -ForegroundColor Yellow
    # Optionally, you can exit or continue to update the package 
    exit 
}

# Create the Package
Write-Host "Creating Package: $PackageName" -ForegroundColor Cyan
try {
    New-CMPackage -Name $PackageName -Description $PackageDescription -Path $SourceFolderPath -Priority High
    Write-Host "Package '$PackageName' created successfully." -ForegroundColor Green
}
catch {
    Write-Host "Failed to create package: $_" -ForegroundColor Red
    exit 1
}

# Create the Program within the Package
Write-Host "Creating Program: $ProgramName" -ForegroundColor Cyan
try {
    New-CMProgram -PackageName $PackageName `
                 -Name $ProgramName `
                 -CommandLine $ProgramCommand `
                 -RunMode Hidden `
                 -ProgramCanRun 1 `
                 -ScriptType PowerShell 
    Write-Host "Program '$ProgramName' created successfully within package '$PackageName'." -ForegroundColor Green
}
catch {
    Write-Host "Failed to create program: $_" -ForegroundColor Red
    exit 1
}

# Distribute Content to Distribution Points
Write-Host "Distributing content to Distribution Points..." -ForegroundColor Cyan
try {
    Start-CMContentDistribution -PackageName $PackageName -DistributionPointGroupName $DistributionPointGroup 
    Write-Host "Content distribution initiated successfully." -ForegroundColor Green
}
catch {
    Write-Host "Failed to distribute content: $_" -ForegroundColor Red
    exit 1
}

Write-Host "Package '$PackageName' created and distributed successfully." -ForegroundColor Green 

Explanation

  • Variables: The script defines variables for MECM site code, package name, description, source paths, script name, program details, and distribution point group.
  • Import Module and Connect to Site: It imports the ConfigurationManager module and connects to the specified MECM site.
  • Source Path Validation: The script checks for the existence of the primary and alternate source paths.
  • Script Existence Check: It verifies if the PowerShell script to be included in the package exists in the source folder.
  • Package Creation: It creates a new MECM package with the specified name, description, and source path.
  • Program Creation: A program is created within the package to execute the PowerShell script.
  • Content Distribution: The script initiates content distribution to the designated distribution point group.

Outro

This script demonstrates how to automate the creation and deployment of an MECM package using PowerShell. You can modify the script to accommodate different package types, scripts, and configurations based on your specific needs.

Tags

PowerShell, MECM, SCCM, Package Deployment, , , Windows 11, Upgrade, Education, Enterprise

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress Cookie Plugin by Real Cookie Banner