For instance, I’ve just rebooted an Exchange server and need a convenient way to start up all of the services. One way is to write up a batch file and run it whenever you want. I’ve done this on more servers than I can count. The other option is to create a more versatile scripting language to start up only those services you need to. This is what I’ve created in PowerShell:
# Service Start Script # # v1.1 # For Exchange 20xx Servers # $services = Get-WmiObject Win32_Service foreach ($line in $services) { $service = $line.displayname $status = $line.state $startup = $line.startmode if ($service -like "Microsoft Exchange*") { if (($status -eq "Stopped") -and ($startup -eq "Auto")) { write-host $service" needs to be started. Starting it now." start-service $service } # Added for Edge Transport Server if ($service -like "Active Directory Web Services") { if (($status -eq "Stopped") -and ($startup -eq "Auto")) { write-host $service" needs to be started. Starting it now." start-service $service } } } } foreach ($line in $services) { $service = $line.displayname $status = $line.state $startup = $line.startmode if ($service -like "Microsoft Exchange*") { get-service $service } }
Note, this script gets a list of services using WMI and searches for a certain pattern in the display name of the service which in this case is ‘Microsoft Exchange”. The main reason I used WMI is that get-service will not supply enough information such as the startup type (automatic/manual/disabled).
The script will also check to see if the service is started. This gives us line 13 where we check for a service with a startup type of ‘Automatic’ and if the service state is ‘Stopped’. If the service meets both criteria, then the script will attempt to start the service.
Once all the services for Exchange have tried to start, the script will check the services for a current state.
Stopping Services
For stopping the services, we can use pretty much the same code adjusting for service state of started and then stopping the service.
# Service Stop Script # # v1.1 # For Exchange 20xx Servers # $services = Get-WmiObject Win32_Service foreach ($line in $services) { $service = $line.displayname $status = $line.state $startup = $line.startmode if ($service -like "Microsoft Exchange*") { if (($status -eq "Running") -and ($startup -eq "Auto")) { write-host $service" needs to be stopped. Stopping it now." stop-service $service -force } # Added for Edge Transport Server if ($service -like "Active Directory Web Services") { if (($status -eq "Running") -and ($startup -eq "Auto")) { write-host $service" needs to be stopped. Stopping it now." stop-service $service -force } } } } foreach ($line in $services) { $service = $line.displayname $status = $line.state $startup = $line.startmode if ($service -like "Microsoft Exchange*") { get-service $service } }
To make this even fancier, you could potentially add a menu system to select whether to start or stop services. That’s up to you.
*** Update ***
Corrections made after a comment from a reader.
- Added line for Edge Transport service that is unique to that role.
- Added the “-force” parameter to the Stop Service command.
- Added “Running” as my filter was using the wrong criteria.
A couple fixes for Win2012R2 and Exchange 2013:
1) for the stop script, the status parameter needs to be set as “Running” in windows 2012R2 or the script doesn’t works.
example of umodified script where it shows the value in the 1st column:
Status Name DisplayName
—— —- ———–
Running HostControllerS… Microsoft Exchange Search Host Cont…
2) if you filter by $service -like “Microsoft Exchange*” it wont work for ANY localized exchange version as mroe than half the services dont begin like that but do have it somewhere in the name, universal solution is to filter for “*Microsoft Exchange*”.
3) the stop-service needs the -force modifier or the Exchange Ad toplogy service will not stop as it has dependencies, and if that service is left started the rest of the services will auto-start by themselves a short while after the script is run
Guillermo,
Agreed on 1 and 3. Corrections will be up soon. Not sure how I missed that but it’s always good to get feedback like this.
On number 2, I had originally coded the script for Exchange 2013 and had not intended this to be a more universal script. So it works perfect for 2013 (now with the -force parameter). Note that I just checked (to refresh my memory) and all services on 2007 or 2010 start with “Microsoft Exchange”. I believe Exchange 2010 is no different. Wildcard should thus be unneeded. I also added a correction as I forgot that for Exchange 2013 all services start with “Microsoft Exchange” and the only exception to this rule appears on the Edge Transport Server which has a service called “Active Directory Web Services”.
Damian,
on #2 check that i clarify on “localized exchange” meaning: installed in other languages BUT english, at least in spanish Exchange(from 2007 onwards at least) do NOT all start with “Microsoft Exchange”. I dont have access to a exchange server right now(and it’s been incredibly hard to google it) but here’s an example: http://www.bujarra.com/como-desinstalar-un-servidor-microsoft-exchange-2003/ near the end there’s a list of E2010 services, not a single one starts with “microsoft exchange”
Understood. However I would be hard for me to write my scripts to handle every localization. I have access to many Exchange servers, however they are all installed in English (I work out of Chicago, IL, USA). In this case I could see the change is pretty easy to handle the differences in language.
Hi. I plagiarised and added a line for “World Wide Web publishing service”. You might wanna add that to your published script. Not sure if my syntax is correct.
# Added for World Wide Web Publishing Service
if ($service -like “World Wide Web Publishing Service”) {
if (($status -eq “Stopped”) -and ($startup -eq “Auto”)) {
write-host $service” needs to be started. Starting it now.”
start-service $service
}
and the stop
# Added for World Wide Web Publishing Service
if ($service -like “World Wide Web Publishing Service”) {
if (($status -eq “running”) -and ($startup -eq “Auto”)) {
write-host $service” needs to be stopped. Stopping it now.”
stop-service $service
I really need to write an updated version of this script. Might pursue this once I get a few more things off my plate. Thanks for the suggestion.