Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Alter-WMIServices {
- <#
- .SYNOPSIS
- Find a remote service by name and alter it
- .DESCRIPTION
- Searches for an enabled service on a group of computers within an OU and
- makes changes to that service depending on the parameters set by the user
- .PARAMETER OU
- Target OU name (ie Servers)
- .PARAMETER user
- Admin username
- .PARAMETER service
- The service to search for
- .PARAMETER choice
- Choose whether or not to make changes to the service
- .PARAMETER killservice
- Choose wheter or not to kill the service
- .PARAMETER changestartmode
- Choose a startup mode for the service: 'Boot', 'System', 'Automatic', 'Manual', or 'Disabled'
- .EXAMPLE
- Alter-WMIServices
- This will prompt for everything... :)
- #>
- # Get a list of computers in the
- # servers OU. This could be a variable
- # that could be changed to look in whatever
- # OU the user wishes, I will add it upon request.
- $ou = read-host "Which OU would you like to search for services within?"
- $computers = Get-ADComputer -Filter '*' -SearchBase "OU=$ou,OU=example,DC=dc,DC=example,DC=org" | Select-Object Name | Foreach-Object {$_.Name.Trim() + ".example.org"}
- # Get user credentials for the session
- $user = read-host "Enter login name"
- $password = read-host -assecurestring "Please enter your password"
- $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $password
- # Let the user pick a service to find/alter
- $service = read-host "Which service are we looking for? ie: salt-minion or salt*"
- # Select each Server in the Servers OU
- # Run the service check on each line in $computers
- foreach ($computer in $computers) {
- $namespace = "ROOT\CIMV2"
- $classname = "Win32_Service"
- Write-Output "connecting to...$computer"
- # Test the connection before starting the process
- $testconnection = (Test-Connection -Computername $computer -BufferSize 16 -Count 1 -Quiet)
- if ($testconnection -eq $True) {
- # This is the meat and pudding,
- # it searches the services for
- # the $service variable set by
- # the user using the script
- $wmi = (Get-WmiObject -Class $classname -ComputerName $computer -Namespace $namespace -Credential $credential | Where-Object {$_.name -like $service})
- # This part could be removed...
- # it just lists various attributes
- # of the process being queried
- $processid = $wmi.ProcessId
- $startmode = $wmi.StartMode
- $started = $wmi.started
- $nameofservice = $wmi.Name
- # This part tests for an
- # empty name object...
- # if the name is empty then
- # we can determine that the
- # service must not be running
- if (!$nameofservice) {
- Write-Output "!!----->$service isn't running on $computer<------!!"
- }
- else {
- Write-Output "====================================="
- Write-Output "$service found on $computer"
- Write-Output "The service name is : $nameofservice"
- Write-Output "The Process ID is : $processid"
- Write-Output "The startup type is : $startmode"
- Write-Output "The process has started : $started"
- Write-Output "====================================="
- # Since the service is found
- # running on the server...
- # let the user make choices
- # about what to change
- $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'"
- # If the user chooses 'Yes' then
- # start asking questions about
- # manipulating the service
- if ($choice -eq "Yes" -and $started -eq "True") {$killservice = read-host "Would you like to kill the service? 'Yes' or 'No'"}
- # If the user wishes to
- # kill the service then...
- if ($killservice -eq "Yes") {$wmi.StopService}
- # If the user chooses 'No' then
- # we leave the service running
- # and move on to the start mode
- elseif ($killservice -eq "No") {Write-Output "I will leave the service running."}
- # Here we will decide what
- # type of start mode to
- # assign to the service
- if ($choice -eq "Yes") {
- $changestartmode = read-host "Please enter which startup type would you like for this service: 'Boot', 'System', 'Automatic', 'Manual', or 'Disabled'"
- $rtn = $wmi.ChangeStartMode($changestartmode)
- # A little trick I picked up
- # from the website in the
- # return value output...
- # This checks for success in
- # changing the start mode for
- # the service that was found
- if ($rtn.returnvalue -eq 0) {Write-Output "Successfully changed the Start Mode of $nameofservice"}
- 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."}
- }
- # If the user decides to
- # not change the service
- # then move on...
- elseif ($choice -eq "No") {Write-Output "No changes will be made to the start mode of the service."}
- }
- }
- else {
- Write-Output "v-----------------------------------------v"
- Write-Output "$computer is not available"
- Write-Output "^-----------------------------------------^"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement