Solved

Write script to remotely restart a windows service and run in Task Scheduler

Posted on 2014-09-10
8
606 Views
Last Modified: 2014-09-11
I'm needing help and direction in the best script to run from within Task Scheduler on both Windows 7 and 8.1 systems. We have an application running on a Windows 2008R2 server that is using a service that occasionally needs to be restarted. We want to give certain users the ability to run or manually kickoff a task in Task Scheduler that will restart the offending service. We'd like to make this a manual process/task that they can run at anytime. What would you suggest? And how should we write the script? Should we use powershell to to do something like this--- Restart-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler) ?

Or should we use sc \\server stop service
sc \\server start service ?

Or net stop and start command?

We will need the script to login to the server as a privileged user and then run the restart of the service specified. Your guidance and recommendations will be greatly appreciated. Thank you.
0
Comment
Question by:Noah_Williams35
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40315611
Just for ease of use I would create a bat file that runs:
sc \\server stop service
sc \\server start service

You can save the task or even a bat file on the users computer to have it run on demand.
0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40315651
This can be done in powershell pretty easily.

get-service -computername NAME | where-object {$_.name -eq "Servicename" -and $_.Status -eq "Running"}).stop()

and to start

(get-service -computername NAME| where-object {$_.name -eq "Servicename" -and $_.status -eq "Stopped"}).start()


I also have a nice script that can do this on a csv list if you need it.
0
 
LVL 6

Expert Comment

by:Steve Whitcher
ID: 40315658
Do the users who will run the script have the right access to restart services on the remote system?  If not, it will be a bit more complicated.  Otherwise, if I needed to restart a remote serivce I'd run

get-service spooler -cn SERVER  | restart-service

Open in new window


To make it a one click solution, save that line in a ps1 file, sign the script if your security requires it, and pass it to those who need it.
0
 

Author Comment

by:Noah_Williams35
ID: 40315680
Steve Whitcher the users themselves do not have the proper level of access, so they would need to login with proper access using th e script or we'd need Task Scheduler to run the script with proper account...

Joseph Daly I'm very interested in seeing the script using the csv list because there are multiple services fro some users. Thak you for sharing!

becraig I plan on using the sc commands if I can't get direction on something more robust.

Joseph Daly, can I use the powershell scripts to run from Task Scheduler with priviledged access without running powershell locally?
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 6

Expert Comment

by:Steve Whitcher
ID: 40315692
So, the users don't have access - do they have credentials for an account that DOES have access, or do you need the script to store the credentials without making them available/visible to the users?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40315703
Using a scheduled task which will run as a user with the required privileges would be a great solution:

Your approach can be either:
CMD/BAT:
for /f %%a in (services.txt) do sc \\server stop %%a & sc \\server start %%a

Open in new window


Powershell (two approaches):
First start any service that should be running but isn't:

$slist = gwmi Win32_Service -computername $computer
$slist | % {
$svcmode = $_.StartMode; $svcname = $_.Name; $svcstate = $_.State 
if ($svcmode -eq "Automatic" -and $svcstate -ne "Running")
{Get-Service -Name $svcname -ComputerName  $computer | Set-Service -Status Running}[/code


2) Start services from a list:
[code]gc services.txt | %{
$svc = $_
$slist = gwmi Win32_Service -computername $computer
$slist | % {
{Get-Service -Name $svc -ComputerName  $computer | Set-Service -Status Running}
}

Open in new window

0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40315712
Looks like becraig beat me to the punch on the script. But if you have any other questions let me know.
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40315715
Update should be Auto and not Automatic:

$slist = gwmi Win32_Service -computername $computer
$slist | % {
$svcmode = $_.StartMode; $svcname = $_.Name; $svcstate = $_.State 
if ($svcmode -eq "Auto" -and $svcstate -ne "Running")
{Get-Service -Name $svcname -ComputerName  $computer | Set-Service -Status Running}[/code

Open in new window



2) Start services from a list:
gc services.txt | %{
$svc = $_
$slist = gwmi Win32_Service -computername $computer
$slist | % {
{Get-Service -Name $svc -ComputerName  $computer | Set-Service -Status Running}
}

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

929 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

15 Experts available now in Live!

Get 1:1 Help Now