Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2018
96
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. #ENCRYPTION DOESNT WORK CHANGING TO PLAINTEXT
  15. ###PASSWORD 1 SETS UP A NEW USER
  16. $pw1='N3o1OUJiSTNXZA=='
  17. ###PASSWORD 2 IS THE NEW ADMIN PASSWORD
  18. $pw2="N3o1OUJiSTNXZA=="
  19. $match = 0
  20. ###CHANGED REGEX2 FROM splunkbeta to splunkforwarder
  21. ##$regex2 = [regex]'(?i)[\\d\\w\\W\\D]+splunkforwarder[\\d\\w\\W\\D]+'
  22. $regex2 = [regex]'splunkforwarder[\\d\\w\\W\\D]+'
  23. ###REGEX1 doesn't seem to be used anywhere
  24. $regex1 = [regex]'[\\d\\w]+VM[\\d\\w]+'
  25. #Functions, generic don't need to change anything here
  26. #decodes encoded string
  27. Function decoder($decode)
  28. {
  29.     $decoded = [System.Convert]::FromBase64String($decode)
  30.     $decodedpd = [System.Text.Encoding]::UTF8.GetString($decoded)
  31.     return $decodedpd
  32. }
  33.  
  34. #Checks if app is installed
  35. Function ckinst($appname)
  36. {
  37.     $installed = Get-WmiObject -Class Win32_Product | Where-object{$_.name -like "*$appname*"}
  38.     If($installed){ return "True"}else{return "False"}
  39. }
  40. #starts process
  41. Function pstart([string]$exec,[string]$argments)
  42. {
  43.     $process = New-Object System.Diagnostics.Process
  44.     $process.StartInfo.FileName = $exec
  45.     $process.StartInfo.RedirectStandardError = 1
  46.     $process.StartInfo.RedirectStandardOutput = 1
  47.     $process.StartInfo.UseShellExecute = 0
  48.     #if there's arguments, append them
  49.     if($argments){write-host $args;$process.StartInfo.Arguments = $argments}
  50.     $process.Start() | Out-Null
  51.     $process.WaitForExit() | Out-Null
  52.     $errorstream = $process.StandardError.ReadToEnd()
  53.     $stdoutstream = $process.StandardOutput.ReadToEnd()
  54.     #if errors occur, write them
  55.     if($errorstream)
  56.     {
  57.         write-host "Error occurred during Execution of $exec with the following arguments: $argments"
  58.         Exit 4
  59.     }elseif($stdoutstream)
  60.     {
  61.         write-host $stdoutstream
  62.     }
  63. }  
  64. write-host "starting"
  65. #building command line string for install
  66. #change this to fit Universal installer
  67. #check UF install page for flags we need
  68. ###MODIFY THIS FOR ADDITIONAL FLAGS
  69. $comstr = " AGREETOLICENSE=Yes INSTALLDIR=$installdir DEPLOYMENT_SERVER=$deployserver LAUNCHSPLUNK=$start"
  70. #get passwords for user type, leave these alone
  71. if($defaultUser)
  72. {
  73.     $password = decoder($password)
  74.     $comstr += " LOGON_USERNAME=`"$domainuser`" LOGON_PASSWORD=$password"
  75. }
  76. elseif($domainuser -and $password)
  77. {
  78.    $comstr += " LOGON_USERNAME=""$domainuser"" LOGON_PASSWORD=`"$password`""
  79. }elseif(($domainuser -and !$password) -or (!$domainuser -and $password))
  80. {
  81.    write-host "Warn: domainuser and password must both be defined"
  82.    Exit 4
  83. }
  84.  
  85. #finding running path and executables
  86. $scriptpath = $MyInvocation.Mycommand.Path
  87. write-host "$scriptpath"
  88. $rdir = Split-Path $scriptpath
  89. write-host "$rdir"
  90. $files = get-childitem $rdir
  91. write-host "$files"
  92. $match = $files | ForEach-Object {$regex2.Matches($_.FullName)}
  93. write-host "$match"
  94. #checking to see if Splunk is already installed
  95. $x = ckinst("UniversalForwarder") #Change this too
  96. if($x -eq "True"){write-host "UF already installed Exiting";Exit}else{Write-Host "Installing Splunk"}
  97. #running installation, change string outputs, leave rest
  98. if($match -ne 0)
  99. {   ###MODIFIED TO JSUT RUN THE FILE WITH NO MATCHING SHIT
  100.     pstart "msiexec" "/i $match $comstr /quiet"
  101.     Start-Sleep -s 10
  102.     write-host "checking install"
  103.     $x = ckinst("UniversalForwarder")
  104.     if($x -eq "True"){write-host "UF successfully installed"}else{Write-Host "UF installed failed"; Exit}
  105. }else
  106. {
  107.     write-host "msi or executable not found"
  108.     Exit 4
  109. }
  110. #run splunk, add users, modify admin passwords
  111. ##ASSUMING EXTRA WAS ADDED, REMOVED FROM ADJACENT TO ' ON RIGHT LIKE SO "'""
  112. ##NEEDED TO PREVENT POWERSHELL FROM THROWING FIT, LEAVE IT BE
  113. $installdir = $installdir.Replace("`"","")
  114. #verifing services
  115. write-host "Verifying Services"
  116. #does this work with UF? need to change splunk probably
  117. ##running the UF seems to be the same as running the Splunk mainline
  118. $splunkprocesses = get-service | where-object{$_.Name -like "*splunk*"}
  119. if ($splunkprocesses.length -ne 0)
  120. {
  121.     write-host "services verified"
  122.     #changing user information
  123.     write-host "changing user info"
  124.     ###CHANGE SIMILAR TO ABOVE REGARDING QUOTATION MARKS
  125.     $installdir = $installdir.Replace("`"","")
  126.     $splunkexe = "`"$installdir\\bin\\splunk.exe`""
  127.     ##decrypt passwords for use
  128.     $pw1 = decoder($pw1)
  129.     $pw2 = decoder($pw2)
  130.     #modify passwords for new user (maybe keep?)
  131.     pstart $splunkexe "add user splunk_local -password $pw1 -role admin -auth admin:changeme"
  132.     #modify admin password
  133.     pstart $splunkexe "edit user admin -password $pw2 -role admin -auth admin:changeme"
  134.     New-Item "$installdir\\etc\.ui_login" -type "file" -force | Out-Null
  135.     write-host "User Info Changed"
  136.     if($start -eq 1)
  137.     {
  138.         write-host "Restarting Splunk"
  139.         pstart $splunkexe "restart"
  140.         Start-Sleep -s 2
  141.         write-host "Restart Complete"
  142.     }
  143. }else #failed install check
  144. {
  145.     write-host "Services do not appear to be installed correctly. Verification required."
  146.     Exit 4
  147. }
  148. write-host "Done"
  149. Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement