Link to home
Start Free TrialLog in
Avatar of Noah Williams
Noah Williams

asked on

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

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.
Avatar of becraig
becraig
Flag of United States of America image

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.
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.
Avatar of Steve Whitcher
Steve Whitcher

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.
Avatar of Noah Williams

ASKER

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

Looks like becraig beat me to the punch on the script. But if you have any other questions let me know.
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial