Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active February 17, 2022 04:41
Show Gist options
  • Save natemccurdy/2d3298b10724481ad17a24b9ba251c12 to your computer and use it in GitHub Desktop.
Save natemccurdy/2d3298b10724481ad17a24b9ba251c12 to your computer and use it in GitHub Desktop.
Puppet code to install SCCM Agent on Windows
<%- | String $client_source,
String $argument_list
| -%>
$ClientSource = "<%= $client_source %>"
$TempDir = Join-Path "C:\Windows\Temp" "ccm_client"
$CCMSetup = Join-Path $TempDir "ccmsetup.exe"
if (-not (Test-Path $CCMSetup)) {
Try {
Write-Output "Copying $ClientSource to $TempDir"
Copy-Item -Path $ClientSource -Destination $TempDir -Recurse
}
Catch {
Write-Error "The SCCM installation files could not be copied"
exit 1
}
}
Try {
Write-Output "Running $CCMSetup"
$instclient = $(start-process $CCMSetup -ArgumentList "<%= $argument_list %>" -PassThru)
$instclient | wait-process
}
Catch {
Write-Output "Something went wrong with the ccmsetup.exe process"
exit 2
}
$ClientLog = "C:\Windows\ccmsetup\Logs\client.msi.log"
$ClientInstalled = "Installation operation completed successfully"
$ClientFailed = "Installation operation failed"
While (!(test-path "C:\Windows\ccmsetup\Logs\client.msi.log"))
{
Write-Output "The client.msi log file is not present. Waiting 10s..."
start-sleep 10
}
Write-Output "The client.msi log file is now present"
$service = "ccmsetup"
while (get-service -name $service -ErrorAction SilentlyContinue)
{
Write-Output "The CCMSetup service is still present. Waiting 30s..."
Start-Sleep -Seconds 30
}
Write-Output "The CCMSetup service is not present"
Start-Sleep -Seconds 15
Write-Output "Reading client.msi log file to determine installation success or failure"
If (select-string -Path $ClientLog -Pattern $ClientInstalled -SimpleMatch)
{
Write-Output "Installation was successful"
$LASTEXITCODE = 0
Write-Output "Cleaning up installation files"
Remove-Item $TempDir -Force -Recurse
}
ElseIf (select-string -Path $ClientLog -Pattern $ClientFailed -SimpleMatch)
{
Write-Error "Client installation failed"
$LASTEXITCODE = 3
}
Else
{
Write-Output "Something went wrong with the installation"
$LASTEXITCODE = 4
}
exit $LASTEXITCODE
# This profile installs the SCCM agent on Windows servers.
#
class profile::win::sccm {
$install_flags = [
'/FORCEINSTALL',
]
$install_options = [
'SMSCACHEFLAGS=PERCENTDISKSPACE;NTFSONLY',
'SMSCACHESIZE=10',
'IGNOREAPPVVERSIONCHECK=TRUE',
'RESETKEYINFORMATION=TRUE',
]
case $facts['datacenter'] {
'AMER': {
$custom_options = [
'SMSMP=foo.na.corp.net',
'SMSSITECODE=AMER',
'FSP=foo.na.corp.net',
]
}
'EMEA': {
$custom_options = [
'SMSMP=foo.ea.corp.net',
'SMSSITECODE=EMEA',
'FSP=foo.EA.corp.net',
]
}
'APAC': {
$custom_options = [
'SMSMP=foo.apac.corp.net',
'SMSSITECODE=APAC',
'FSP=foo.apac.corp.net',
]
}
default : {
fail("No match found for datacenter: ${facts['datacenter']}")
}
}
# Get the location to the folder that contains the installation files.
$client_source = lookup('sccm_source_folder')
# Combine the arguments together into a single-line, space-separated string.
$argument_list = ($install_flags + $install_options + $custom_options).join(' ')
# Install the SCCM client by executing a PowerShell script that tracks the installation process.
# The script is neccesary because the SCCM install forks into a bunch of subprocess and Puppet can't
# keep track of the installation.
#
# The script it built from a template. The template uses:
# client_source: The full path to the ccmsetup.exe.
# argument_list: A one-line string with all arguments separated by spaces.
#
# We are making this exec idempotent by checking for the existence of ccmexec.exe.
# This IS NOT a good way to verify that SCCM is installed, but it works for now.
exec { 'Install SCCM Client':
command => epp('profile/win/Install-CCM-Agent.ps1.epp', {
'client_source' => $client_source,
'argument_list' => $argument_list,
}),
provider => 'powershell',
timeout => '600', # Increase from default of 300s. SCCM takes a while to install.
logoutput => true,
creates => 'c:\Windows\CCM\CcmExec.exe',
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment