Function isArgExist ($hostname) { if ([string]::IsNullOrEmpty($hostname)) { Write-Output("you should pass the hostname, Exit") exit } } Function isResolvable ($puppetServer) { $checkResolution = Resolve-DnsName -Name $puppetServer -ErrorAction SilentlyContinue if ($checkResolution -eq $null) { Write-Output "$puppetServer is not resolvable, Exit" exit } } Function downloadPuppetClient ($tempPath, $executableFileName, $downloadURL ) { $installer=-join($tempPath, $executableFileName) if(![System.IO.File]::Exists($installer)){ Write-Output("Download puppet Client ") Write-Output($installer) Invoke-WebRequest $downloadURL -OutFile $installer -UseBasicParsing } } Function ispuppetClientinstalled { $isInstalled=(Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -like '*puppet*' }) -ne $null if ($isInstalled) { Write-Output("puppet client is already installed, Exit") exit } } Function installpuppetClient($tempPath, $executableFileName, $puppetServer, $hostname) { $installer=-join($tempPath, $executableFileName) Write-Output("installing puppet Client and sending the certification request to puppet Server...") Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /norestart /i $installer PUPPET_MASTER_SERVER=$puppetServer PUPPET_AGENT_CERTNAME=$hostname" -Wait $puppetClientIsInstalled = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -like '*puppet*' }) -ne $null if(-Not $puppetClientIsInstalled) { Write-Output "Failed to install puppet Client, Exit"; exit } else { Write-Output "puppet Client is now installed "; } Set-Service -Name puppet -StartupType Automatic } Function addpuppetExtIP () { $fileInput="C:\windows\system32\drivers\etc\hosts" $searchresults= Select-String -Path $fileInput -Pattern "cmdbsrv" if ($searchresults -eq $null) { ADD-Content -path $fileInput -value "78.142.176.122 cmdbsrv.kopi.org " } } Function authorizeMyIP() { $SECRET_KEY = "3H5YGoct3IL4Sj1xh0L3pyo8" $PUPPET_SERVER_URL = "https://puppettracker.kopi.org/auth.php" $hasher = [System.Security.Cryptography.HashAlgorithm]::Create('sha256') $TIMESTAMP = [Math]::Round((New-TimeSpan -start (Get-Date "1970-01-01") -end (Get-Date (Get-Date).ToUniversalTime())).TotalSeconds) $AUTH_DATA = "${hostname}${TIMESTAMP}${SECRET_KEY}" $hash = $hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($AUTH_DATA)) $hashString = [System.BitConverter]::ToString($hash) $AUTH_HASH=$hashString.Replace('-', '') $OSTYPE = 'windows' $UTCDIFF=Get-Date -UFormat "%Z" # Send the request to the puppet server with the auth hash, timestamp, and hostname Invoke-RestMethod -Uri $PUPPET_SERVER_URL -Method Post -Headers @{ "X-Puppet-Authorization" = $AUTH_HASH "X-Timestamp" = $TIMESTAMP "X-Hostname" = $hostname "X-Public-Ip" = "" "X-UTCDIFF" = $UTCDIFF "X-OSTYPE" = $OSTYPE } } ########################################## ###### Global variables declaration [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $hostname = $args[0] $tempPath = $env:temp $downloadURL = "https://www.progmag.com/telechargements/puppet7/puppet-agent-x64-latest.msi" $executableFileName = "\puppet-agent-x64-latest.msi" $puppetClientInstallationPath = -join($Env:Programfiles,"\Puppet Labs") $puppetServer = "s1s.puppet.kopi.org" if ($hostname -like "*.vie.elyssa.xyz") { $puppetServer = "s1i.puppet.kopi.org" } ########################################## ###### Main ######### ## add Puppet external IP to hosts file # addpuppetExtIP ######## check puppet client is installed ispuppetClientinstalled ######## check hostname is passed isArgExist $hostname ######## check a puppet server is resolvable isResolvable $puppetServer ######## download puppet client downloadPuppetClient $tempPath $executableFileName $downloadURL ######## install puppet client and send certification request to the server installpuppetClient $tempPath $executableFileName $puppetServer $hostname ######## authorize access to puppet server authorizeMyIP