Software installation with PowerShell in Puppet

powershell-in-puppet

PowerShell in Puppet

Integrating PowerShell scripts with Puppet for software installation combines the strengths of both tools to achieve efficient and scalable automation. Puppet, a powerful configuration management tool, allows for the seamless orchestration of infrastructure as code, managing the deployment and configuration of systems across large environments. By incorporating PowerShell scripts into Puppet manifests, administrators can leverage Windows-native scripting capabilities to handle complex software installations, updates, and configurations. This integration enables precise control over the installation process, ensuring that software consistency according to organizational standards. PowerShell’s ability to interact with the Windows Registry, manage system services, and execute command-line utilities complements Puppet’s robust automation framework, resulting in a streamlined, reliable, and maintainable deployment strategy.

Windows Registry

The Windows Registry is a centralized database that stores critical system and application configuration settings in Windows operating systems. It organizes information in a hierarchical structure of keys and values, enabling the system and applications to operate efficiently and consistently. The Registry manages everything from user profiles and system hardware configurations to installed software and user preferences. Understanding and managing the Windows Registry can significantly enhance system performance, troubleshoot issues, and customize the user experience. However, due to its complexity and the potential impact of incorrect modifications, you should handle Registry edits with caution.

Installation script

Updating the Windows Registry using a PowerShell script is a powerful way to automate system configuration and settings adjustments. For instance, consider a script that modifies a registry key to change a custom setting. My_SOFT keeps info about given software installation status. Here’s an example:

...

# Add_registry_key
add-log -LogType "INFO" -Message "Add_registry_key: Creating registry entry for deployment"

try {

  if ( !(Test-Path $logpath\$errorlogfile) ) {

    if ( !(Test-Path HKLM:\SOFTWARE\MY_SOFT) ) {
     
      New-Item HKLM:\SOFTWARE\ -Name MY_SOFT-force

    }

    start-sleep 10

    New-ItemProperty -Path HKLM:\SOFTWARE\MY_SOFT-Name Deployed -Value 1 -Type dword -force

  } else {

    add-log -LogType "INFO" -Message "Add_registry_key: Registry entry NOT created due to errors found"

  }

} catch {

  add-log -LogType "ERROR" -Message "Add_registry_key: $_"

}

...

Simply add such code snippet at the end of the software installation script. Then it will update the windows registry upon successful installation.

Puppet facter script

Further, the MY_SOFT windows registry can be evaluated within puppet facter script as presented below

...

# Check if application was installed
Function Test-Deployment {

  $ErrorActionPreference = "Stop"
  $ServerType = (hostname).Substring(3,3)

  Switch($ServerType){

    "APP" {$Message = "appinstalled"}
    "SQL" {$Message = "sqlinstalled"}
    "INT" {$Message = "intinstalled"}
    "BAK" {$Message = "dpminstalled"}
    default {return}

  }

  if (Test-Path HKLM:\SOFTWARE\MY_SOFT) {

    try {

      $Tmp = Get-ItemProperty HKLM:\SOFTWARE\MY_SOFT-name Deployed
      $Deployed = $Tmp.Deployed

    } catch {

      write-host "$Message=n/a"
      return

    }

  }

  if ($Deployed -eq "1") {

    write-host "$Message=true"

  } else {

    write-host "$Message=false"

  }

}

...

Here You can find more more about puppet facts.

Puppet manifest

Ultimately, the invocation of the software installation is going to be triggered only if the puppet facter variable has been evalutatd to false value. Otherwise the software installation will be skipped.

...

  if $sqlinstalled == 'false'  {

    file { "${scriptspath}\\installsql.ps1":
      ensure => file,
      content => template('windows_sql/install/installsql.ps1'),
      source_permissions => ignore,
    }

    exec { 'Install_SQL':
      path => 'c:\Windows\system32\WindowsPowerShell\v1.0', 
      command => "powershell.exe -executionpolicy bypass -file ${scriptspath}\\installsql.ps1",
      require => File["${scriptspath}\\installsql.ps1"],
      timeout => 0,
    }

  }

...

References:

Leave a Reply

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