Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Alter-WMIServices {
  2.  
  3. <#
  4.  
  5. .SYNOPSIS
  6. Find a remote service by name and alter it
  7. .DESCRIPTION
  8. Searches for an enabled service on a group of computers within an OU and
  9. makes changes to that service depending on the parameters set by the user
  10. .PARAMETER OU
  11. Target OU name (ie Servers)
  12. .PARAMETER user
  13. Admin username
  14. .PARAMETER service
  15. The service to search for
  16. .PARAMETER choice
  17. Choose whether or not to make changes to the service
  18. .PARAMETER killservice
  19. Choose wheter or not to kill the service
  20. .PARAMETER changestartmode
  21. Choose a startup mode for the service: 'Boot', 'System', 'Automatic', 'Manual', or 'Disabled'
  22. .EXAMPLE
  23. Alter-WMIServices
  24. This will prompt for everything... :)
  25. #>
  26.  
  27. # Get a list of computers in the
  28. # servers OU. This could be a variable
  29. # that could be changed to look in whatever
  30. # OU the user wishes, I will add it upon request.
  31. $ou = read-host "Which OU would you like to search for services within?"
  32. $computers = Get-ADComputer -Filter '*' -SearchBase "OU=$ou,OU=example,DC=dc,DC=example,DC=org" | Select-Object Name | Foreach-Object {$_.Name.Trim() + ".example.org"}
  33.  
  34. # Get user credentials for the session
  35. $user = read-host "Enter login name"
  36. $password = read-host -assecurestring "Please enter your password"
  37. $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $password
  38.  
  39. # Let the user pick a service to find/alter
  40. $service = read-host "Which service are we looking for? ie: salt-minion or salt*"
  41.  
  42. # Select each Server in the Servers OU
  43. # Run the service check on each line in $computers
  44. foreach ($computer in $computers) {
  45. $namespace = "ROOT\CIMV2"
  46. $classname = "Win32_Service"
  47. Write-Output "connecting to...$computer"
  48.  
  49. # Test the connection before starting the process
  50. $testconnection = (Test-Connection -Computername $computer -BufferSize 16 -Count 1 -Quiet)
  51. if ($testconnection -eq $True) {
  52.  
  53. # This is the meat and pudding,
  54. # it searches the services for
  55. # the $service variable set by
  56. # the user using the script
  57. $wmi = (Get-WmiObject -Class $classname -ComputerName $computer -Namespace $namespace -Credential $credential | Where-Object {$_.name -like $service})
  58.  
  59. # This part could be removed...
  60. # it just lists various attributes
  61. # of the process being queried
  62. $processid = $wmi.ProcessId
  63. $startmode = $wmi.StartMode
  64. $started = $wmi.started
  65. $nameofservice = $wmi.Name
  66.  
  67. # This part tests for an
  68. # empty name object...
  69. # if the name is empty then
  70. # we can determine that the
  71. # service must not be running
  72. if (!$nameofservice) {
  73. Write-Output "!!----->$service isn't running on $computer<------!!"
  74. }
  75. else {
  76. Write-Output "====================================="
  77. Write-Output "$service found on $computer"
  78. Write-Output "The service name is : $nameofservice"
  79. Write-Output "The Process ID is : $processid"
  80. Write-Output "The startup type is : $startmode"
  81. Write-Output "The process has started : $started"
  82. Write-Output "====================================="
  83.  
  84. # Since the service is found
  85. # running on the server...
  86. # let the user make choices
  87. # about what to change
  88. $choice = read-host "$service was found running on $computer as $startmode and the status of the process is $started. Would you like to change it? Enter 'Yes' or 'No'"
  89.  
  90. # If the user chooses 'Yes' then
  91. # start asking questions about
  92. # manipulating the service
  93. if ($choice -eq "Yes" -and $started -eq "True") {$killservice = read-host "Would you like to kill the service? 'Yes' or 'No'"}
  94.  
  95. # If the user wishes to
  96. # kill the service then...
  97. if ($killservice -eq "Yes") {$wmi.StopService}
  98.  
  99. # If the user chooses 'No' then
  100. # we leave the service running
  101. # and move on to the start mode
  102. elseif ($killservice -eq "No") {Write-Output "I will leave the service running."}
  103.  
  104. # Here we will decide what
  105. # type of start mode to
  106. # assign to the service
  107. if ($choice -eq "Yes") {
  108. $changestartmode = read-host "Please enter which startup type would you like for this service: 'Boot', 'System', 'Automatic', 'Manual', or 'Disabled'"
  109. $rtn = $wmi.ChangeStartMode($changestartmode)
  110.  
  111. # A little trick I picked up
  112. # from the website in the
  113. # return value output...
  114. # This checks for success in
  115. # changing the start mode for
  116. # the service that was found
  117. if ($rtn.returnvalue -eq 0) {Write-Output "Successfully changed the Start Mode of $nameofservice"}
  118. else {Write-Output "The return value $($rtn.returnvalue) was reported please see https://msdn.microsoft.com/en-us/library/windows/desktop/aa384896(v=vs.85).aspx for a list of return values."}
  119. }
  120.  
  121. # If the user decides to
  122. # not change the service
  123. # then move on...
  124. elseif ($choice -eq "No") {Write-Output "No changes will be made to the start mode of the service."}
  125. }
  126. }
  127. else {
  128. Write-Output "v-----------------------------------------v"
  129. Write-Output "$computer is not available"
  130. Write-Output "^-----------------------------------------^"
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement