[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

PowerShell or batch script for restart service on remote PC

Guys I need some help with PowerShell or batch script which I am not very good in.

I want to stop/kill, delete any temp file and then start a service on remote machine.

Machine name is Leo10
Service name is Leoadmin.exe
Some time I restart service but it doesn’t stop so I kill the process then start and delete some temp folders.

Here is the process which I want in script.

On Leo10 server
Stop Leoadmin.exe
If failed then kill process Leoadmin.exe
Delete file on Leo10 server desktop/temp folder if any
Start Leoadmin.exe service

Thank you
Vijay
0
Vijaytyagi
Asked:
Vijaytyagi
2 Solutions
 
xDUCKxCommented:
net stop leoadmin
taskkill /F /IM leoadmin.exe
del C:\User\Destkop\Temp\*.tmp
net start leoadmin

Open in new window


The "Net Stop" and "Net start" may or may not work depending on what the actual name of the service is displayed as in services.msc.

Test it before putting it into production.

EDIT:  This is a batch file.
0
 
David Johnson, CD, MVPOwnerCommented:
Powershell

stop-Service -InputObject $(Get-Service -Computer LEO10-Name leoadmin
#give it a few seconds to stop
Start-Sleep -s 5
Stop-Process -InputObject $(Get-Service -Computer LEO10 -Name leoadmin.exe
start-sleep -s 2
start-service -InputObject $(Get-Service -Computer LEO10 -Name leoadmin
0
 
VijaytyagiAuthor Commented:
Guys,

Thanks for your advice but both way (PS and batch script) not able to stop service.

Killing process is working both way.

Any other suggestion?

Thank you
0
 
d_nedelchevCommented:
Run:
sc query leoadmin

Open in new window

to see if the servce is STOPPABLE at all.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now