Skip to content

Instantly share code, notes, and snippets.

@Hexalon
Created June 29, 2016 20:40
Show Gist options
  • Save Hexalon/6b5f7a9dc94fcaf5fa740e82053a2716 to your computer and use it in GitHub Desktop.
Save Hexalon/6b5f7a9dc94fcaf5fa740e82053a2716 to your computer and use it in GitHub Desktop.
Automates installation of System Center Configuration Manager.
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.123
Created on: 6/24/2016 10:24
Created by: Colin Squier <Hexalon@gmail.com>
Filename: Install-SCCM.ps1
===========================================================================
.DESCRIPTION
Automates installation of System Center Configuration Manager.
#>
[CmdletBinding()]
Param (
[switch]$Uninstall = $false,
[switch]$SCEP = $false
)
<#
.SYNOPSIS
Returns the path of the executing script's directory.
.DESCRIPTION
Sapien's implementation of the variable $HostInvocation
causes a conflict the with the system's variable.
.EXAMPLE
PS C:\> Get-ScriptDirectory
.NOTES
Work around for handling Sapien's custom host environment.
#>
function Get-ScriptDirectory
{
if ($null -ne $HostInvocation)
{
Split-Path $HostInvocation.MyCommand.path
}
else
{
Split-Path $script:MyInvocation.MyCommand.Path
}
}
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
$Option = New-CimSessionOption -Protocol Dcom
$Session = New-CimSession -SessionOption $Option -ComputerName $env:COMPUTERNAME
$OS = (Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $Session)
$OSVersion = $OS.Version
$scriptDirectory = Get-ScriptDirectory
$Source = (Split-Path -Path $scriptDirectory -Parent)
$PackagePath = (Join-Path -Path $Source -ChildPath "ComputerSetup\SCCM2012R2Client")
$File = "ccmsetup.exe"
$Version = "5.00.8325.1000"
$Product = "System Center Configuration Manager"
$FullPath = Join-Path -Path $PackagePath -ChildPath $File
$SCCM = (Get-WmiObject -Namespace "Root\CCM" -Class SMS_Client)
if ($Uninstall)
{
if (!($null -eq $SCCM))
{
$InstalledVersion = $SCCM.ClientVersion
Write-Verbose -Message "Installed $Product version is $InstalledVersion"
Write-Verbose "Stopping ccmexec service"
Stop-Service -Name ccmexec | Out-Null
Write-Verbose "Removing SCCM configuration file"
Remove-Item -Path $ENV:WinDir\SMSCFG.ini | Out-Null
Write-Verbose "Removing $Product client"
$Removal = Start-Process -FilePath "`"$FullPath`"" -ArgumentList "/uninstall" -Wait -Passthru -NoNewWindow
$ExitCode = $Removal.ExitCode
Write-Verbose -Message "$Product uninstaller exit code: $ExitCode"
Write-Verbose "Removing `"HKLM:\SOFTWARE\Microsoft\CCMSetup`" from the registry"
Remove-item -Path "HKLM:\SOFTWARE\Microsoft\CCMSetup" -Recurse | Out-Null
Write-Verbose "Removing `"HKLM:\SOFTWARE\Microsoft\SMS`" from the registry"
Remove-item -Path "HKLM:\SOFTWARE\Microsoft\SMS" -Recurse | Out-Null
if ($ExitCode -eq 0)
{
Write-Verbose -Message "Uninstall was successful."
}
}
else
{
Write-Verbose -Message "$Product has already been removed."
}
break;
}
if (!($null -eq $SCCM))
{
$InstalledVersion = $SCCM.ClientVersion
Write-Verbose "Installed $Product version is $InstalledVersion"
if ([version]$InstalledVersion -ge $Version)
{
Write-Verbose "$Product version $InstalledVersion or later already installed."
}
}
if ($OSVersion -ge '10.0')
{
$Arguments = "/skipprereq:WindowsUpdateAgent30-x64.exe /USEPKICERT SMSMP=cvgwpsccm02.ga.afginc.com CCMHOSTNAME=config01.gaig.com SMSSITECODE=PRI SMSCACHESIZE=20480 CCMFIRSTCERT=1 DNSSUFFIX=ga.afginc.com /NoCRLCheck"
}
else
{
$Arguments = "/USEPKICERT SMSMP=cvgwpsccm02.ga.afginc.com CCMHOSTNAME=config01.gaig.com SMSSITECODE=PRI SMSCACHESIZE=20480 CCMFIRSTCERT=1 DNSSUFFIX=ga.afginc.com /NoCRLCheck"
}
if ($null -eq $InstalledVersion -or [version]$InstalledVersion -lt $Version)
{
Write-Verbose "Stopping ccmexec service"
Stop-Service -Name ccmexec | Out-Null
Write-Verbose "Removing SCCM configuration file"
Remove-Item -Path $ENV:WinDir\SMSCFG.ini | Out-Null
Write-Verbose "Removing any existing version of client"
$SCCM = Start-Process -FilePath "`"$FullPath`"" -ArgumentList "/uninstall" -Wait -Passthru -NoNewWindow
$ExitCode = $SCCM.ExitCode
Write-Verbose "$Product uninstaller exit code: $ExitCode"
Write-Verbose "Removing `"HKLM:\SOFTWARE\Microsoft\CCMSetup`" from the registry"
Remove-item -Path "HKLM:\SOFTWARE\Microsoft\CCMSetup" -Recurse | Out-Null
Write-Verbose "Removing `"HKLM:\SOFTWARE\Microsoft\SMS`" from the registry"
Remove-item -Path "HKLM:\SOFTWARE\Microsoft\SMS" -Recurse | Out-Null
Write-Verbose "Installing $Product $Version"
$SCCM = Start-Process -FilePath "`"$FullPath`"" -ArgumentList $Arguments -Wait -Passthru -NoNewWindow
$ExitCode = $SCCM.ExitCode
Write-Verbose "$Product installer exit code: $ExitCode"
}
if ($SCEP)
{
$SCEPFile = "Install-SCEP.ps1"
$SCEPFullPath = Join-Path -Path $scriptDirectory -ChildPath $SCEPFile
Invoke-Expression "& '$SCEPFullPath'"
}
Remove-CimSession -CimSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment