Wednesday, November 29, 2017

How to Stop and Start Service using Powershell until service status equal to Started or stopped

Powershell gives us functionality to stop or start service until either it's fully stopped or started. 
Lest copy and paste this script into notepad and save as stop-start-service.ps1 name. replace service name with the service name you want to restart. in my case, I am restarting World wide web service.

@"
===============================================================================
Title:               Start and Stop Service 
Description:         Stop, Start
Requirements:        Windows Powershell 
Author:              Amar Singh
Date:                Nov 29, 2017
===============================================================================
"@ 

# Define Service Name
$servicename = "w3svc"

#To stop the service
Stop-Service $servicename -Verbose

do {
Start-Sleep 10
}
until((Get-Service $servicename).Status -eq "stopped")


#To Start Service
Start-Service $servicename -Verbose

do {
Start-Sleep 10
}
until ((Get-Service $servicename).Status -eq "running")


#To Validate service status
Get-Service $servicename
start-sleep 10














AT the end of script compilation you get the status of service on your screen.

Thank you!!!


No comments:

Post a Comment