Advertisement
Guest User

Untitled

a guest
Feb 5th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #splunkinstaller.ps1
  2. #default parameters and other options
  3. ###CHANGED INSTALL PATH
  4. Param(
  5.    [string][alias("id")] $installdir = $(if((Test-Path -Path "D:\")){"`"D:\Program Files\SplunkUniversalForwarder`""}else{"`"C:\Program Files\SplunkUniversalForwarder`""}),#change directory for UnivFor
  6.    [string][alias("ds")] $deployserver = "localhost:8089",#changed by User
  7.    [switch][alias("du")]$defaultUser,
  8.    [ValidateSet(0,1)][int][alias("s")]$start = 1,
  9.    [string][alias("u")]$domainuser = $(if($defaultUser){"localhost\\myuser"}),#default login
  10.    [string][alias("p")]$password = $(if($defaultUser){"Y2ldmU="})#change login credentials
  11. )
  12. #var initallization
  13. $splunkprocesses = @()
  14. ##########installer filename. change to fit##########
  15. $installname = "splunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi"
  16.  
  17.  
  18. #passwords stored in script as 64bit, decoded mid script when changing passwords
  19. ###PASSWORD 1 SETS UP A NEW USER#######
  20. $pw1='N3o1OUJiSTNXZA=='
  21. ###PASSWORD 2 IS THE NEW ADMIN PASSWORD######
  22. $pw2="N3o1OUJiSTNXZA=="
  23. $match = 0
  24. ###CHANGED REGEX2 FROM splunkbeta to splunkforwarder
  25. ##$regex2 = [regex]'(?i)[\\d\\w\\W\\D]+splunkforwarder[\\d\\w\\W\\D]+'
  26. $regex2 = [regex]'splunkforwarder[\\d\\w\\W\\D]+'
  27. ###REGEX1 doesn't seem to be used anywhere
  28. $regex1 = [regex]'[\\d\\w]+VM[\\d\\w]+'
  29. #Functions, generic don't need to change anything here
  30. $installpriv = 0
  31. #decodes encoded string
  32. Function decoder($decode)
  33. {
  34.     $decoded = [System.Convert]::FromBase64String($decode)
  35.     $decodedpd = [System.Text.Encoding]::UTF8.GetString($decoded)
  36.     return $decodedpd
  37. }
  38.  
  39. #Checks if app is installed
  40. Function ckinst($appname)
  41. {
  42.     $installed = Get-WmiObject -Class Win32_Product | Where-object{$_.name -like "*$appname*"}
  43.     If($installed){ return "True"}else{return "False"}
  44. }
  45. #starts process
  46. Function pstart([string]$exec,[string]$argments)
  47. {
  48.     $process = New-Object System.Diagnostics.Process
  49.     $process.StartInfo.FileName = $exec
  50.     $process.StartInfo.RedirectStandardError = 1
  51.     $process.StartInfo.RedirectStandardOutput = 1
  52.     $process.StartInfo.UseShellExecute = 0
  53.     #if there's arguments, append them
  54.     if($argments){write-host $args;$process.StartInfo.Arguments = $argments}
  55.     $process.Start() | Out-Null
  56.     $process.WaitForExit() | Out-Null
  57.     $errorstream = $process.StandardError.ReadToEnd()
  58.     $stdoutstream = $process.StandardOutput.ReadToEnd()
  59.     #if errors occur, write them
  60.     if($errorstream)
  61.     {
  62.         write-host "Error occurred during Execution of $exec with the following arguments: $argments"
  63.         Exit 4
  64.     }elseif($stdoutstream)
  65.     {
  66.         write-host $stdoutstream
  67.     }
  68. }  
  69. write-host "starting"
  70. #Determine install mode
  71. #check reigstry to determine if Domain controller or not
  72. $key =  'hklm:\SYSTEM\CurrentControlSet\control\ProductOptions'
  73. $systype = (Get-ItemProperty -Path $key -Name ProductType).ProductType
  74. if($systype -eq 'LanmanNT'){
  75.     $installpriv = 1
  76. }
  77. #building command line string for install
  78. #check UF install page for flags we need
  79. ###MODIFY THIS FOR ADDITIONAL FLAGS
  80. $comstr = " AGREETOLICENSE=Yes INSTALLDIR=$installdir DEPLOYMENT_SERVER=$deployserver LAUNCHSPLUNK=$start SET_ADMIN_USER=$installpriv LAUNCHSPLUNK=1 SERVICESTARTTYPE=auto"
  81. #get passwords for user type, leave these alone
  82. if($defaultUser)
  83. {
  84.     $password = decoder($password)
  85.     $comstr += " LOGON_USERNAME=`"$domainuser`" LOGON_PASSWORD=$password"
  86. }
  87. elseif($domainuser -and $password)
  88. {
  89.    $comstr += " LOGON_USERNAME=""$domainuser"" LOGON_PASSWORD=`"$password`""
  90. }elseif(($domainuser -and !$password) -or (!$domainuser -and $password))
  91. {
  92.    write-host "Warn: domainuser and password must both be defined"
  93.    Exit 4
  94. }
  95.  
  96. #finding running path and executables
  97. $scriptpath = $MyInvocation.Mycommand.Path
  98. write-host "$scriptpath"
  99. $rdir = Split-Path $scriptpath
  100. write-host "$rdir"
  101. $files = get-childitem $rdir
  102. write-host "$files"
  103. $match = $files | ForEach-Object {$regex2.Matches($_.FullName)}
  104. write-host "Match Value: $match"
  105. #checking to see if Splunk is already installed
  106. $x = ckinst("UniversalForwarder")
  107. if($x -eq "True"){write-host "UF already installed Exiting";Exit}else{Write-Host "Installing Splunk"}
  108. #running installation, change string outputs, leave rest
  109. if($match -ne 0)
  110. {   ###MODIFIED TO TAKE FILENAME FROM START OF SCRIPT
  111.     pstart "msiexec" "/i $installname $comstr /quiet"
  112.     Start-Sleep -s 10
  113.     write-host "checking install"
  114.     $x = ckinst("UniversalForwarder")
  115.     if($x -eq "True"){write-host "UF successfully installed"}else{Write-Host "UF installed failed"; Exit}
  116. }else
  117. {
  118.     write-host "msi or executable not found"
  119.     Exit 4
  120. }
  121. #run splunk, add users, modify admin passwords
  122. $installdir = $installdir.Replace("`"","")
  123. #verifing services
  124. write-host "Verifying Services"
  125. #does this work with UF? need to change splunk probably
  126. ##running the UF seems to be the same as running the Splunk mainline
  127. $splunkprocesses = get-service | where-object{$_.Name -like "*splunk*"}
  128. if ($splunkprocesses.length -ne 0)
  129. {
  130.     write-host "services verified"
  131.     #changing user information
  132.     write-host "changing user info"
  133.     ###CHANGE SIMILAR TO ABOVE REGARDING QUOTATION MARKS
  134.     $installdir = $installdir.Replace("`"","")
  135.     $splunkexe = "`"$installdir\\bin\\splunk.exe`""
  136.     ##decrypt passwords for use
  137.     $pw1 = decoder($pw1)
  138.     $pw2 = decoder($pw2)
  139.     #modify passwords for new user (maybe keep?)
  140.     pstart $splunkexe "add user splunk_local -password $pw1 -role admin -auth admin:changeme"
  141.     #modify admin password
  142.     pstart $splunkexe "edit user admin -password $pw2 -role admin -auth admin:changeme"
  143.     New-Item "$installdir\\etc\.ui_login" -type "file" -force | Out-Null
  144.     write-host "User Info Changed"
  145.     if($start -eq 1)
  146.     {
  147.         write-host "Restarting Splunk"
  148.         pstart $splunkexe "restart"
  149.         Start-Sleep -s 2
  150.         write-host "Restart Complete"
  151.     }
  152. }else #failed install check
  153. {
  154.     write-host "Services do not appear to be installed correctly. Verification required."
  155.     Exit 4
  156. }
  157. write-host "Done"
  158. Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement