Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-10
8
Medium Priority
?
734 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
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 

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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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…
Progress

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