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 " } } ########################################## ###### Global variables declaration $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 = "cmdbsrv.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