Solved

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

Posted on 2014-09-10
8
593 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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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

 
LVL 6

Expert Comment

by:Steve Whitcher
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
Looks like becraig beat me to the punch on the script. But if you have any other questions let me know.
0
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

772 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

16 Experts available now in Live!

Get 1:1 Help Now