Download System Center ConfigMgr 2012 SP1 prerequisites

I’ve recently seen on the web a powershell version 2 script responsible for downloading the System Center Configuration Manager (ConfigMgr) SP1.

As you may know you may need to redownload the binaries as well:

Here’s my version for powershell v3 😀

#Requires -Version 3.0

Function Get-ConfigMgr2012SP1Prerequisites {
[CmdletBinding()]    
param(
    [parameter(Mandatory)]            
    [system.string]$TargetFolder = $null           
)
Begin {
    $HT = @{}
    $HT += @{ ErrorAction = 'Stop'}
    # Validate target folder
    try {
        Get-Item $TargetFolder @HT | Out-Null
    } catch {
        Write-Warning -Message "The target folder specified as parameter does not exist"
        break
    }
}
Process {

    # Step 1 download the manifest file
    try {
        Invoke-WebRequest -Uri "http://go.microsoft.com/fwlink/?linkid=269720" -OutFile (Join-Path -Path $TargetFolder -ChildPath SP1.Manifest.cab) @HT
        Get-Item (Join-Path -Path $TargetFolder -ChildPath SP1.Manifest.cab) @HT | Unblock-File @HT
    } catch {
        Write-Warning -Message "Failed to download the manifest file"
        return
    }

    # Create subfolders
    'SP1.Manifest','SP1.Prerequisites' | ForEach-Object -Process {
        if (-not(Test-Path (Join-Path -Path $TargetFolder -ChildPath $_))) {
            try {
                New-Item (Join-Path -Path $TargetFolder -ChildPath $_) -ItemType Directory  @HT |Out-Null
            } catch {
                Write-Warning -Message "Failed to create subfolder because $($_.Exception.Message)"
                return
            }
        }
    }

    if (Test-Path (Join-Path -Path $TargetFolder -ChildPath SP1.Manifest)) {
    
        # Step2: Expand the manifest cab
        if (Test-Path (Join-Path -Path $TargetFolder -ChildPath "SP1.Manifest\sp1.manifest.cab")) {
            try {
                Remove-Item -Path (Join-Path -Path $TargetFolder -ChildPath "SP1.Manifest\sp1.manifest.cab") -Force @HT
            } catch {
                Write-Warning -Message "Failed to remove previous extracted content because $($_.Exception.Message)"
                return
            }
        }
        & (Get-Command expand) @("$TargetFolder\SP1.Manifest.cab",'-F:*.*',"$TargetFolder\SP1.Manifest") |Out-Null

        # Step 3: Read its XML content
        $updates = ([xml](Get-Content (Join-Path -Path $TargetFolder -ChildPath "SP1.Manifest\sp1.manifest.cab"))).ConfigMgr.Group.SelectNodes("*")

        # Step 4: Download each file
        if ($updates) {
        $i = 0
        $updates | ForEach-Object -Process {
                $i++
                Write-Progress -activity "Downloading file: $($_.FriendlyName)" -status "Percent completed: " -PercentComplete (($i/$updates.Count)*100)
                try {
                    Invoke-WebRequest -Uri ([URI]$($_.Source)) -OutFile (Join-Path -Path $TargetFolder -ChildPath "SP1.Prerequisites\$($_.CopyName)") @HT
                } catch {
                    Write-Warning -Message "Failed to download content because $($_.Exception.Message)"
                    return
                }
                if ($?) {
                    Write-Verbose "Validating signature of file $($_.FriendlyName)" 
                    $DownloadedFileSHA2 = $null
                    $DownloadedFileSHA2 = Get-CheckSum -FilePath (Join-Path -Path $TargetFolder -ChildPath "SP1.Prerequisites\$($_.CopyName)") -Type SHA256
                    if ($DownloadedFileSHA2 -eq $_.SHA256) {
                        Write-Verbose "Digital signature of file $($_.FriendlyName) is valid" 
                    } else {
                        Write-Warning -Message "Digital signature of file $($_.FriendlyName) is invalid"
                    }
                }
            }
        } else {
            Write-Warning -Message "Failed to read or find the files to download in the XML file"
        }
     } else {
        Write-Warning -Message "Failed to find directory $(Join-Path -Path $TargetFolder -ChildPath SP1.Manifest)"
    }
}
End {}
}

NB: The Get-Checksum function being used in the above code is available on this page: https://p0w3rsh3ll.wordpress.com/2012/06/26/getting-file-checksum/

Get-ConfigMgr2012SP1Prerequisites -TargetFolder "E:\CM2012\Test" -Verbose

Advertisements

One thought on “Download System Center ConfigMgr 2012 SP1 prerequisites

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s