Solved

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

Posted on 2016-07-25
6
38 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:Benjamin Voglar
ID: 41728862
$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)
ID: 41728877
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
ID: 41729272
I will try this on one of the development server and confirm by tomorrow.... :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PavanPA
ID: 41732267
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
ID: 41732269
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
ID: 41756099
For closing service on multiple computers is this the right answer,
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

628 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