The Usual Tech Ramblings

PowerShell; Changing startup properties of a service

Windows 2008 doesn’t have SMTP as part of IIS any more. It’s only kept around by shipping the IIS 6 libraries with it, which means it’s managed by the old IIS 6 utilities. We installed SMTP so that our web servers had a relaying agent so if there were any delays talking to the main SMTP server the app wouldn’t crash out. The problem is, the default setting is for the SMTP service to be set to a manual startup, rather than Automatic. This is easily remedied by going through the services management utilities, or powershell one liner…

gwmi win32_service | ?{$ -like "*SMTP*"} | %{$_.changestartmode("Automatic")}

This is actually 3 steps. The first step connects to WMI and fetches all the services. The second limits the scope to just services with SMTP in the name. And the third is using the returned object and changing settings.

When run, a buunch of data is returned telling to the state of the command:

__GENUS          : 2
__CLASS          : __PARAMETERS
__RELPATH        :
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0

You can verify that the command worked properly by looking at the ReturnValue, or by running the command again, but without the set step…

ExitCode  : 0
Name      : SMTPSVC
ProcessId : 7024
StartMode : Auto
State     : Running
Status    : OK

Running this command on a 2008 server is about 10 times faster than waiting for the services utility to open.