Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-09-10
8
Medium Priority
?
708 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
[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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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
 
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 2000 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

715 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