Solved

Schedule Restart of a specific windows service (Application Specific) PowerShell

Posted on 2016-07-25
6
25 Views
Last Modified: 2016-08-22
Hello all,

I want to stop a specific windows service using powershell and if it fails to stop, want to force stop using the script.
All I want to do is to schedule the script which will stop the service and force stop if it does not stop.

Even in case the service will not stop is there any possibility to check or get a notification that it has not stopped.
In one of the maintenance we are suppose to login to 100 servers to stop one service and run a script to start the application maintenance which is taking a long time. Any idea will be appreciated and of great help

Urgent :(
0
Comment
Question by:PavanPA
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:Benjamin Voglar
Comment Utility
$ServiceName = "WinRM"


$arrService = Get-Service -Name $ServiceName



if ($arrService.Status -eq "Running"){
stop-Service $ServiceName -Force }

Open in new window

0
 
LVL 12

Accepted Solution

by:
Benjamin Voglar earned 500 total points (awarded by participants)
Comment Utility
This script will stop service "Winrm" on all computers in file servesrs.txt


$comp = Get-Content -Path 'C:\it\servers.txt'
 


ForEach  ($obj in $comp) {


Invoke-Command -ComputerName $obj -ScriptBlock{


$ServiceName = "WinRM"


$arrService = Get-Service -Name $ServiceName 



if ($arrService.Status -eq "Running"){
stop-Service $ServiceName -Force }

}

}
    

Open in new window

0
 

Author Comment

by:PavanPA
Comment Utility
I will try this on one of the development server and confirm by tomorrow.... :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:PavanPA
Comment Utility
Hi Benjamin, Thank you so much for the script, yes it works but what I am looking here is to stop a particular service at server local time 8:00 PM, and we have server in different time zones? IS there any thing we can add to the script to work?

If possible it will be of a good learning to me as well.
0
 

Author Comment

by:PavanPA
Comment Utility
Or I should use the first script you gave and setup a schedule task to run at local time on all servers...
1
 
LVL 12

Expert Comment

by:Benjamin Voglar
Comment Utility
For closing service on multiple computers is this the right answer,
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A procedure for exporting installed hotfix details of remote computers using powershell
In this Micro Tutorial viewers will learn how they can get their files copied out from their unbootable system without need to use recovery services. As an example non-bootable Windows 2012R2 installation is used which has boot problems.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now