Skip to content

Instantly share code, notes, and snippets.

@jordigg
Last active February 16, 2021 07:42
Show Gist options
  • Save jordigg/388d3f0e3bb06d57c02b to your computer and use it in GitHub Desktop.
Save jordigg/388d3f0e3bb06d57c02b to your computer and use it in GitHub Desktop.
Puppet Boostrap for Windows
.SYNOPSIS
Installs Puppet on this machine.
.DESCRIPTION
Downloads and installs the PuppetLabs Puppet MSI package.
This script requires administrative privileges.
You can run this script from an old-style cmd.exe prompt using the
following:
powershell.exe -ExecutionPolicy Unrestricted -NoLogo -NoProfile -Command "& '.\windows.ps1' -Role Engineering -Type desktop"
.PARAMETER MsiUrl
This is the URL to the Puppet MSI file you want to install. This defaults
to a version from PuppetLabs.
.PARAMETER PuppetVersion
This is the version of Puppet that you want to install. If you pass this it will override the version in the MsiUrl.
This defaults to $null.
#>
param(
[string]$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-x64-latest.msi"
,[string]$PuppetVersion = $null
)
if ($PuppetVersion) {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-$($PuppetVersion).msi"
Write-Host "Puppet version $PuppetVersion specified, updated MsiUrl to `"$MsiUrl`""
}
$PuppetInstalled = $false
try {
$ErrorActionPreference = "Stop";
Get-Command puppet | Out-Null
$PuppetInstalled = $true
$PuppetVersion=&puppet "--version"
Write-Host "Puppet $PuppetVersion is installed. This process does not ensure the exact version or at least version specified, but only that puppet is installed. Exiting..."
Exit 0
} catch {
Write-Host "Puppet is not installed, continuing..."
}
if (!($PuppetInstalled)) {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (! ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
Write-Host -ForegroundColor Red "You must run this script as an administrator."
Exit 1
}
# Install it - msiexec will download from the url
$install_args = @("/qn", "/norestart","/i", $MsiUrl)
Write-Host "Installing Puppet. Running msiexec.exe $install_args"
$process = Start-Process -FilePath msiexec.exe -ArgumentList $install_args -Wait -PassThru
if ($process.ExitCode -ne 0) {
Write-Host "Installer failed."
Exit 1
}
# Stop the service that it autostarts
Write-Host "Stopping Puppet service that is running by default..."
Start-Sleep -s 5
Stop-Service -Name puppet
Write-Host "Puppet successfully installed."
# Add custom fact
if ($Role) {
Write-Host "Adding custom role [$($Role)] to Puppet"
new-item -path C:\ProgramData\PuppetLabs\facter\facts.d -name role.txt -type "file" -value "role=$($Role)" -force
}
if ($Type) {
Write-Host "Adding custom type [$($Type)] to Puppet"
new-item -path C:\ProgramData\PuppetLabs\facter\facts.d -name type.txt -type "file" -value "type=$($Type)" -force
}else{
Write-Host "Adding custom type [Desktop] to Puppet"
new-item -path C:\ProgramData\PuppetLabs\facter\facts.d -name type.txt -type "file" -value "type=desktop" -force
}
Write-Host "Running puppet for the first time."
Start-Process -FilePath "C:\Program Files\Puppet Labs\Puppet\bin\puppet.bat" -ArgumentList "agent -t" -Verb runAs
Write-Host "Puppet successfully installed and running."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment