Guest User

Untitled

a guest
Feb 23rd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. $hostname = [Net.Dns]::GetHostName()
  2.  
  3. function Install-Puppet($opts)
  4. {
  5. $params = @('/qn', '/i', 'pkg\puppet-agent.msi', '/l*v', 'install.log')
  6. if ($opts.Account) { $params += "PUPPET_AGENT_ACCOUNT_USER=`"$($opts.Account)`"" }
  7. if ($opts.Domain) { $params += "PUPPET_AGENT_ACCOUNT_DOMAIN=`"$($opts.Domain)`"" }
  8. if ($opts.Password) { $params += "PUPPET_AGENT_ACCOUNT_PASSWORD=`"$($opts.Password)`"" }
  9. if ($opts.Startup) { $params += "PUPPET_AGENT_STARTUP_MODE=`"$($opts.Startup)`"" }
  10.  
  11. Write-Host "`nInstalling Puppet Agent with $params"
  12. $process = Start-Process 'msiexec' -ArgumentList $params -Wait -PassThru
  13.  
  14. if ($process.ExitCode -ne 0)
  15. {
  16. Write-Error "Puppet Agent failed to install with : $($process.ExitCode)"
  17. return $process.ExitCode
  18. }
  19.  
  20. # Automatic is returned as Auto from WMI
  21. $expectedMode = $opts.Startup
  22. if (!$expectedMode -or $expectedMode -eq 'Automatic') { $expectedMode = 'Auto' }
  23. $expectedState = 'Stopped'
  24. if ($expectedMode -eq 'Auto') { $expectedState = 'Running' }
  25.  
  26. $expectedUser = if ($opts.Account) { $opts.Account } else { 'LocalSystem' }
  27. $expectedDomain = if ($opts.Domain) { $opts.Domain } else { '.' }
  28. if (@('.', $hostname) -contains $expectedDomain)
  29. {
  30. if (@('LocalService', 'NetworkService') -contains $expectedUser)
  31. { $expectedUser = "NT AUTHORITY\$expectedUser" }
  32. elseif ('LocalSystem' -ne $expectedUser)
  33. { $expectedUser = ".\$expectedUser" }
  34. }
  35. else
  36. {
  37. $expectedUser = "$expectedDomain\$expectedUser"
  38. }
  39.  
  40. Write-Host "Verifying service user $expectedUser / state $expectedState / startup $expectedMode"
  41.  
  42. $puppet = Get-WmiObject Win32_Service -Filter 'Name = "Puppet"'
  43. if (!$puppet)
  44. {
  45. Write-Error 'Puppet service not installed!'
  46. return 1
  47. }
  48.  
  49. if ($puppet.StartMode -ne $expectedMode)
  50. {
  51. Write-Error "Puppet service mode [$($puppet.StartMode)] does not match requested [$expectedMode]"
  52. return 1
  53. }
  54.  
  55. if ($puppet.State -ne $expectedState)
  56. {
  57. Write-Error "Puppet service state [$($puppet.State)] does not match requested [$expectedState]"
  58. return 1
  59. }
  60.  
  61. # local user accounts get prefixed with the . -- unless LocalService, LocalSystem, NetworkService
  62. if ($puppet.StartName -ne $expectedUser)
  63. {
  64. Write-Error "Puppet user [$($puppet.StartName)] does not match requested [$expectedUser]"
  65. return 1
  66. }
  67. Write-Host 'Verified successfully'
  68.  
  69. Write-Host "Successfully installed Puppet with $params" -ForegroundColor Green
  70. return 0
  71. }
  72.  
  73. function Uninstall-Puppet
  74. {
  75. Write-Host 'Uninstalling Puppet'
  76. $uninst = @('/qn', '/x', 'pkg\puppet-agent.msi')
  77. $process = Start-Process 'msiexec' -ArgumentList $uninst -Wait -PassThru
  78.  
  79. if ($process.ExitCode -ne 0)
  80. {
  81. Write-Error "Puppet Agent failed to uninstall with : $($process.ExitCode)"
  82. }
  83.  
  84. return $process.ExitCode
  85. }
  86.  
  87. @(
  88. @{},
  89. @{ Account = 'LocalSystem' },
  90. @{ Domain = '.'; Account = 'LocalSystem' },
  91. @{ Domain = $hostname; Account = 'LocalSystem' },
  92. @{ Domain = $hostname; Account = 'LocalSystem'; Startup = 'Manual' },
  93. @{ Domain = $hostname; Account = 'LocalSystem'; Startup = 'Disabled' },
  94. @{ Domain = $hostname; Account = 'LocalSystem'; Startup = 'Automatic' },
  95. @{ Account = 'vagrant'; Password = 'vagrant' },
  96. @{ Domain = $hostname; Account = 'vagrant'; Password = 'vagrant' },
  97. @{ Account = 'LocalService' },
  98. @{ Domain = '.'; Account = 'LocalService' },
  99. @{ Account = 'NetworkService' },
  100. @{ Domain = '.'; Account = 'NetworkService' }
  101. @{ Domain = 'NT Authority'; Account = 'NetworkService' }
  102. ) |
  103. % {
  104. try
  105. {
  106. $exitCode = Install-Puppet $_
  107. }
  108. finally
  109. {
  110. $exitCode = Uninstall-Puppet
  111. if ($exitCode -ne 0) { exit $exitCode }
  112. }
  113. }
Add Comment
Please, Sign In to add comment