Advertisement
Guest User

Untitled

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