Monday, August 5, 2013

Changing StartMode and State for Exchange Services

When patching Exchange Mailbox servers, there is often a need to reboot several times, and we have often stopped and disabled the exchange services to allow for quicker boot times. I have written two powershell scripts as follows:
1.     Stop-ExchangeServices.ps1 - This script will list all the exchange services (DisplayName, StartMode, State), stop the services and then set any that have a StartMode of Automatic to Disabled. It transcripts its every action and makes an entry to the Application Event Log of the server. It also will leave a text file (called Stop-ExchangeServices.txt) transcript "C:\Transcripts" folder of the server.
2.     Start-ExchangeServices.ps1 - This script will list all the exchange services (DisplayName, StartMode, State), set any that have a StartMode of Disabled to Automatic and then starts the services. It transcripts its every action and makes an entry to the Application Event Log of the server. It also will leave a text file (called Start-ExchangeServices.txt) transcript "C:\Transcripts" folder of the server.
Both scripts require elevated privileges, but you should be logged into the server with your admin account, so that won’t be a worry. To prep the mailbox server for patching, first dismount the mail databases, through the EMC or powershell. I will write a robust script to do this later on. Once all databases are dismounted, open a powershell prompt and navigate to the E:\scripts directory. Then type “.\Stop-ExchangeServices.ps1” and hit ENTER. You will see the following:
PS C:\Scripts> .\Stop-ExchangeServices.ps1
Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt
Stop-ExchangeServices.ps1 - Exchange Service States at beginning:
DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Auto                      Running
Microsoft Exchange Information Store                      Auto                      Running
Microsoft Exchange Mail Submission Service            Auto                     Running
Microsoft Exchange Mailbox Assistants                    Auto                      Running
Microsoft Exchange Monitoring                               Manual                  Stopped
Microsoft Exchange Replication Service                   Auto                      Running
Microsoft Exchange RPC Client Access                    Auto                      Running
Microsoft Exchange Search Indexer                         Auto                      Running
Microsoft Exchange Service Host                            Auto                      Running
Microsoft Exchange System Attendant                      Auto                      Running
Microsoft Exchange Throttling                                Auto                      Running
Microsoft Exchange Transport Log Search               Auto                      Running

Stopping Exchange Services

Disabling Exchange Services
Exchange Service States at conclusion:

DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology        Disabled                Stopped
Microsoft Exchange Information Store                     Disabled                Stopped
Microsoft Exchange Mail Submission Service          Disabled                Stopped
Microsoft Exchange Mailbox Assistants                   Disabled                Stopped
Microsoft Exchange Monitoring                              Manual                  Stopped
Microsoft Exchange Replication Service                  Disabled                Stopped
Microsoft Exchange RPC Client Access                   Disabled                Stopped
Microsoft Exchange Search Indexer                         Disabled                Stopped
Microsoft Exchange Service Host                            Disabled                Stopped
Microsoft Exchange System Attendant                      Disabled                Stopped
Microsoft Exchange Throttling                                Disabled                Stopped
Microsoft Exchange Transport Log Search               Disabled                Stopped

Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt
Script concluded, Services stopped and disabled.

The transcript file that is written looks like:
**********************
Windows PowerShell transcript start
Start time: 20130804173135
Username  : DOMAIN\AdminAccount
Machine          : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt
Stop-ExchangeeServices.ps1 - Exchangee Service States at beginning:
   
DisplayName                                                         StartMode             State
-----------                                                                ---------                 -----
Microsoft Exchange Active Directory Topology       Auto                     Running
Microsoft Exchange Information Store                    Auto                     Running
Microsoft Exchange Mail Submission Service          Auto                     Running
Microsoft Exchange Mailbox Assistants                   Auto                     Running
Microsoft Exchange Monitoring                              Manual                 Stopped
Microsoft Exchange Replication Service                  Auto                     Running
Microsoft Exchange RPC Client Access                  Auto                     Running
Microsoft Exchange Search Indexer                        Auto                     Running
Microsoft Exchange Service Host                           Auto                     Running
Microsoft Exchange System Attendant                    Auto                     Running
Microsoft Exchange Throttling                               Auto                     Running
Microsoft Exchange Transport Log Search              Auto                     Running

Stopping Exchange Services

Disabling Exchange Services

Exchange Service States at conclusion:

DisplayName                                                         StartMode             State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Disabled                Stopped
Microsoft Exchange Information Store                      Disabled                 Stopped
Microsoft Exchange Mail Submission Service            Disabled                Stopped
Microsoft Exchange Mailbox Assistants                     Disabled                Stopped
Microsoft Exchange Monitoring                                Manual                  Stopped
Microsoft Exchange Replication Service                    Disabled                 Stopped
Microsoft Exchange RPC Client Access                     Disabled                Stopped
Microsoft Exchange Search Indexer                           Disabled                Stopped
Microsoft Exchange Service Host                              Disabled                Stopped
Microsoft Exchange System Attendant                       Disabled                Stopped
Microsoft Exchange Throttling                                  Disabled                Stopped
Microsoft Exchange Transport Log Search                 Disabled                Stopped

Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt

**********************
Windows PowerShell transcript end
End time: 20130804173136
**********************



The Script also makes an entry into the Application Event Log of the server that will look like this:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Start-AdobeServices" />
  <EventID Qualifiers="0">1111</EventID>
  <Level>4</Level>
  <Task>1</Task>
  <Keywords>0x80000000000000</Keywords>
  <TimeCreated SystemTime="2013-08-05T01:18:57.000000000Z" />
  <EventRecordID>20005</EventRecordID>
  <Channel>Application</Channel>
  <Computer>YOUR-SERVER </Computer>
  <Security />
  </System>
- <EventData>
  <Data>**********************Windows PowerShell transcript start Start time: 20130804173135 Username  : DOMAIN\AdminAccount Machine      : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1) ********************** Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt Stop-ExchangeeServices.ps1 - Exchangee Service States at beginning:
DisplayName StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Auto  Running Microsoft Exchange Information Store  Auto Running Microsoft Exchange Mail Submission Service     Auto Running Microsoft Exchange Mailbox Assistants Auto                Running Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Auto Running Microsoft Exchange RPC Client Access Auto Running Microsoft Exchange Search Indexer       Auto Running Microsoft Exchange Service Host                     Auto Running Microsoft Exchange System Attendant Auto Running Microsoft Exchange Throttling Auto Running Microsoft Exchange Transport Log Search Auto Running Stopping Exchange Services Disabling Exchange Services Exchange Service States at conclusion: DisplayName  StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Disabled Stopped Microsoft Exchange Information Store Disabled Stopped Microsoft Exchange Mail Submission Service Disabled Stopped Microsoft Exchange Mailbox Assistants Disabled Stopped Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Disabled Stopped Microsoft Exchange RPC Client Access    Disabled Stopped Microsoft Exchange Search Indexer Disabled Stopped Microsoft Exchange Service Host Disabled Stopped Microsoft Exchange System Attendant Disabled             Stopped Microsoft Exchange Throttling Disabled Stopped Microsoft Exchange Transport Log Search              Disabled Stopped Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt ********************** Windows PowerShell transcript end End time: 20130804173136 **********************</Data>
  <Binary>0A14</Binary>
  </EventData>
  </Event>

The server is now ready for patching and can be rebooted as many times as necessary to apply the appropriate number of passes from Windows Update. Once all patching is complete, you can run the next script. The Start-ExchangeServices.ps1 script has all the same outputs, except, of course that it sets the StartMode of the disabled services to automatic and starts the services.
Once all patches are applied, open a powershell prompt and navigate to the E:\scripts directory. Then type “.\Start-ExchangeServices.ps1” and hit ENTER. You will see the following:

PS E:\Scripts> .\Start-ExchangeServices.ps1
Transcript started, output file is C:\Transcripts\Start-ExchangeServices.txt
Start-ExchangeServices.ps1 - Exchange Service States at beginning:

DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology        Disabled                Stopped
Microsoft Exchange Information Store                     Disabled                Stopped
Microsoft Exchange Mail Submission Service          Disabled                Stopped
Microsoft Exchange Mailbox Assistants                   Disabled                Stopped
Microsoft Exchange Monitoring                              Manual                  Stopped
Microsoft Exchange Replication Service                  Disabled                Stopped
Microsoft Exchange RPC Client Access                   Disabled                Stopped
Microsoft Exchange Search Indexer                         Disabled                Stopped
Microsoft Exchange Service Host                            Disabled                Stopped
Microsoft Exchange System Attendant                      Disabled                Stopped
Microsoft Exchange Throttling                                Disabled                Stopped
Microsoft Exchange Transport Log Search               Disabled                Stopped


Enabling Exchange Services

Starting Exchange Services

Exchange Service States at conclusion:

DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology        Auto                     Running
Microsoft Exchange Information Store                     Auto                     Running
Microsoft Exchange Mail Submission Service           Auto                     Running
Microsoft Exchange Mailbox Assistants                    Auto                     Running
Microsoft Exchange Monitoring                               Manual                Stopped
Microsoft Exchange Replication Service                   Auto                     Running
Microsoft Exchange RPC Client Access                    Auto                     Running
Microsoft Exchange Search Indexer                         Auto                      Running
Microsoft Exchange Service Host                            Auto                      Running
Microsoft Exchange System Attendant                      Auto                      Running
Microsoft Exchange Throttling                                Auto                      Running
Microsoft Exchange Transport Log Search               Auto                      Running

Server is now restored to normal. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Start-ExchangeServices.txt
Script concluded, Services stopped and disabled.

The transcript file that is written looks like:
**********************
Windows PowerShell transcript start
Start time: 20130804212013
Username  : DOMAIN\AdminAccount
Machine          : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\Transcripts\Start-ExchangeServices.txt
Start-ExchangeServices.ps1 - Exchange Service States at beginning:

DisplayName                                                         StartMode             State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Disabled                Stopped
Microsoft Exchange Information Store                      Disabled                 Stopped
Microsoft Exchange Mail Submission Service            Disabled                 Stopped
Microsoft Exchange Mailbox Assistants                     Disabled                Stopped
Microsoft Exchange Monitoring                                Manual                  Stopped
Microsoft Exchange Replication Service                    Disabled                Stopped
Microsoft Exchange RPC Client Access                    Disabled                Stopped
Microsoft Exchange Search Indexer                          Disabled                Stopped
Microsoft Exchange Service Host                             Disabled                Stopped
Microsoft Exchange System Attendant                      Disabled                Stopped
Microsoft Exchange Throttling                                 Disabled                Stopped
Microsoft Exchange Transport Log Search                Disabled                Stopped

Starting Exchange Services

Enabling Exchange Services

Exchange Service States at conclusion:
DisplayName                                                         StartMode             State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Auto                     Running
Microsoft Exchange Information Store                      Auto                     Running
Microsoft Exchange Mail Submission Service            Auto                     Running
Microsoft Exchange Mailbox Assistants                     Auto                     Running
Microsoft Exchange Monitoring                                Manual                 Stopped
Microsoft Exchange Replication Service                    Auto                     Running
Microsoft Exchange RPC Client Access                    Auto                     Running
Microsoft Exchange Search Indexer                          Auto                     Running
Microsoft Exchange Service Host                             Auto                      Running
Microsoft Exchange System Attendant                      Auto                      Running
Microsoft Exchange Throttling                                 Auto                     Running
Microsoft Exchange Transport Log Search                Auto                     Running

Server is now restored to normal. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt

**********************
Windows PowerShell transcript end
End time: 20130804212014
**********************

The Script also makes an entry into the Application Event Log of the server that will look like this:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Start-AdobeServices" />
  <EventID Qualifiers="0">1111</EventID>
  <Level>4</Level>
  <Task>1</Task>
  <Keywords>0x80000000000000</Keywords>
  <TimeCreated SystemTime="2013-08-05T01:18:57.000000000Z" />
  <EventRecordID>20005</EventRecordID>
  <Channel>Application</Channel>
  <Computer>YOUR-SERVER.DOMAIN.COM</Computer>
  <Security />
  </System>
- <EventData>
  <Data>**********************Windows PowerShell transcript start Start time: 20130804173135 Username  : DOMAIN\AdminAccount Machine      : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1) ********************** Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt Stop-ExchangeeServices.ps1 - Exchangee Service States at beginning:
DisplayName StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Auto  Running Microsoft Exchange Information Store  Auto Running Microsoft Exchange Mail Submission Service     Auto Running Microsoft Exchange Mailbox Assistants Auto                Running Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Auto Running Microsoft Exchange RPC Client Access Auto Running Microsoft Exchange Search Indexer       Auto Running Microsoft Exchange Service Host  Auto Running Microsoft Exchange System Attendant Auto Running Microsoft Exchange Throttling Auto Running Microsoft Exchange Transport Log Search Auto Running Stopping Exchange Services Disabling Exchange Services Exchange Service States at conclusion: DisplayName  StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Disabled Stopped Microsoft Exchange Information Store Disabled Stopped Microsoft Exchange Mail Submission Service Disabled Stopped Microsoft Exchange Mailbox Assistants Disabled Stopped Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Disabled Stopped Microsoft Exchange RPC Client Access Disabled Stopped Microsoft Exchange Search Indexer Disabled Stopped Microsoft Exchange Service Host Disabled Stopped Microsoft Exchange System Attendant Disabled Stopped Microsoft Exchange Throttling Disabled Stopped Microsoft Exchange Transport Log Search Disabled Stopped Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt ********************** Windows PowerShell transcript end End time: 20130804173136 **********************</Data>
  <Binary>0A14</Binary>
  </EventData>
  </Event>


The server is now in a normalized state, and the Mail Databases can be mounted and normal operation can be resumed. 

The Scripts:
Note: Log_this function used in both scripts written by: Som Dutt Tripathi, used from: http://powershell-tips.blogspot.com/2012/10/how-to-write-in-windows-event-log-with.html

Stop-ExchangeServices.ps1
<#
    .SYNOPSIS
    Stops Exchange Services and sets their start mode to disabled. 
   
                Sean McNamara sean.mcnamara@spx.com
                Log_this function written by: Som Dutt Tripathi, used from: http://powershell-tips.blogspot.com/2012/10/how-to-write-in-windows-event-log-with.html
               
                THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE 
                RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
               
                Version 1 - August 4, 2013
               
    .DESCRIPTION
               
    This script Stops Exchange Services and sets their start mode to disabled. It writes a transcript to the Application 
                Event log on the server. 
               
                .EXAMPLE
    .\Stop-ExchangeServices.ps1 
               
    #>
$SCRIPT_NAME="Stop-ExchangeServices" 
$Script_Path="C:\Sandbox\Stop-ExchangeServices.txt"

function log_this([string]$MESSAGE) { 

                if ( !([System.Diagnostics.EventLog]::SourceExists($SCRIPT_NAME)) ) 
                { 
                                New-EventLog -LogName Application -Source $SCRIPT_NAME 
                } 

                Write-EventLog -logname Application -source $SCRIPT_NAME -eventID 1111 -entrytype Information -message $MESSAGE -category 1 -rawdata 10,20 
}


Start-Transcript -path $Script_Path 

# Show Current Service Status
Write-Host "Stop-ExchangeServices.ps1 - Exchange Service States at beginning:"
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
write-host ""

# Stop Exchange Services
Write-host "Stopping Exchange Services"
# Note ** Using -Force because MsExchangeADTopology Service has dependancies and will not stop without it
Get-Service | where{($_.DisplayName –Like ‘*Exchange*’) -and ($_.Status -eq "Running")}  | Stop-Service -Force

# Disable Services
Write-host""
Write-host "Disabling Exchange Services"
Get-WmiObject Win32_Service | where{($_.DisplayName -like '*Exchange*') -and ($_.StartMode -eq "Auto")} | set-Service –StartupType ‘Disabled’
Write-Host""

# Show current Start Mode
Write-Host "Exchange Service States at conclusion:"
Write-Host""
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
Write-Host "Server is now ready for Patching. For a transcript of the activities, go to " $Script_Path
stop-transcript

# Write information to Event Log
$Trans_Info = Get-Content $Script_Path  

log_this $Trans_Info

#End Script

Start-ExchangeServices.ps1

<#
    .SYNOPSIS
    Sets Exchange Services StartMode to Auto and starts the service. 
   
                Sean McNamara sean.mcnamara@spx.com
                Log_this function written by: Som Dutt Tripathi, used from: http://powershell-tips.blogspot.com/2012/10/how-to-write-in-windows-event-log-with.html
               
                THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE 
                RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
               
                Version 1 - August 4, 2013
               
    .DESCRIPTION
               
    This script sets the startup type of any Exchange service that are disabled to automatic, starts the service and writes a transcript
                to the application event log on the server. 
               
                .EXAMPLE
    .\Start-ExchangeServices.ps1 
               
    #>
$SCRIPT_NAME="Start-ExchangeServices" 
$Script_Path="E:\files\Transcripts\Start-ExchangeServices.txt" 

function log_this([string]$MESSAGE) { 

                if ( !([System.Diagnostics.EventLog]::SourceExists($SCRIPT_NAME)) ) 
                { 
                                New-EventLog -LogName Application -Source $SCRIPT_NAME 
                } 

                Write-EventLog -logname Application -source $SCRIPT_NAME -eventID 1111 -entrytype Information -message $MESSAGE -category 1 -rawdata 10,20 
}


Start-Transcript -path $Script_Path

# Show Current Service Status
Write-Host "Start-ExchangeServices.ps1 - Exchange Service States at beginning:"
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
write-host ""

# Enable Services
Write-host""
Write-host "Enabling Exchange Services"
Get-WmiObject Win32_Service | where{($_.DisplayName -like '*Exchange*') -and ($_.StartMode -eq "Disabled")} | set-Service –StartupType ‘Auto’
Write-Host""

# Start Exchange Services
Write-host "Starting Exchange Services"
Get-WmiObject Win32_Service | where{($_.DisplayName –Like ‘*Exchange*’) -and ($_.StartMode -eq "Auto")} | Start-Service 

# Show current Start Mode
Write-Host "Exchange Service States at conclusion:"
Write-Host""
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
Write-Host "Server is now restored to normal. For a transcript of the activities, go to " $Script_Path
Stop-transcript

# Write information to Event Log
$Trans_Info = Get-Content $Script_Path  

log_this $Trans_Info


Write-host "Script concluded, Services enabled and started."

#End Script


1 comment:

  1. A very nice post, the unified communication solutions
    is very must in nowdays and this type of communication companies make the services better. One of same kind or Conferencing is Global UC . You Should go through it and check its services.

    ReplyDelete