Office Servers and Services

"La connaissance ne vaut que si elle est partagée" / "An effective Knowledge is a shared one"

Exchange Services Script

Posted by David PEKMEZ sur janvier 24, 2011


Voici un script permettant de démarrer, arrêter ou redémarrer les services Exchange Server 2010 (en démarrage automatique)

Script allows you to Start, Stop or Restart Exchange Server 2010 Services (only theses in Automatic startup mode)




J’espère que ce script pourra vous faire gagner du temps !

Download the script

Source Code


#=================================================================================
# ExchangeServices.ps1
#
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
# KIND.
# Description: 
#
# This Script Written By: David Pekmez ( https://unifiedit.wordpress.com )
#
# Version: 1
# Last Updated: 19/01/2011
#==================================================================================

#=======================================
# Parameter definition
#=======================================
  
param([string]$Server,[string]$Services)
    
#==========================================================================
# Function that returns true if the incoming argument is a help request
#==========================================================================
function IsHelpRequest
{
 param($argument)
 return ($argument -eq "-?" -or $argument -eq "-help");
}
  
#===================================================================
# Function that displays the help related to this script
#===================================================================
function Usage
{

@"
NAME: ExchangeServices.ps1

SYNOPSIS:
Start / Stop or Restart Exchange Server 2010 Services
SYNTAX:
ExchangeServices.ps1
`t[-Server <ServerName>]
`t[-Services <Start / Stop / Restart>]
`t[-Help Or -?]

PARAMETERS:
-Server (Requiered)
The Server Name of the Exchange Server you want to Start / Stop or ReStart Exchange Services

-Services (Requiered)
Choose Start Or Stop Or ReStart

-? Or -Help (Optional)
To Show the Help Menu
-------------------------- EXAMPLE 1 --------------------------

.\ExchangeServices.ps1 -Server SRVEXCH -Services Start

-------------------------- EXAMPLE 2 --------------------------

.\ExchangeServices.ps1 -Server SRVEXCH -Services Stop

-------------------------- EXAMPLE 3 --------------------------

.\ExchangeServices.ps1 -Server SRVEXCH -Services ReStart

"@
} 

#=======================================
# Check for Usage Statement Request
#=======================================
$args | foreach { if (IsHelpRequest $_) { Usage; exit; } }
#==============================================
# Script Parameter Check
#==============================================

 $validInputs = $true
 $errorString =  ""

 if ($Server -eq "")
 {
  $validInputs = $false
  $errorString += "`nMissing Parameter: The -Server parameter is required. Please enter the Exchange Server Name."
 }

  if ($Services -eq "")
 {
  $validInputs = $false
  $errorString += "`nMissing Parameter: The -Services parameter is required. Please enter the Start / Stop Or ReStart parameter."
 }
 
 if (!$validInputs)
 {
  Write-error "$errorString"
 }

#===================================================
# Exchange Services Start / Stop Or ReStart
#===================================================

if($Services -like 'start')
{
$Service = Get-WmiObject -ComputerName $Server win32_service -filter "StartMode='Auto' and Name like '%Exchange%'"
ForEach ($ExchangeService in $Service)
 {
  if($ExchangeService.State -notlike 'Running')
   {
   Write-Host -ForegroundColor Green "Starting $ExchangeService"
   Start-Service $ExchangeService.Name
   }
  else {Write-Host "$ExchangeService is already Running."}
 }
Write-Host -ForegroundColor Green "All Exchange Services in Automatic Startup Mode are now Started"
}
elseif($Services -like 'stop')
{

$Service = Get-WmiObject -ComputerName $Server win32_service -filter "StartMode='Auto' and Name like '%Exchange%'"
ForEach ($ExchangeService in $Service)
 {
   
  if($ExchangeService.State -notlike 'Stopped')
   {
   Write-Host -ForegroundColor Green "Stopping $ExchangeService"
   Stop-Service $ExchangeService.Name -Force
   }
  else {Write-Host "$ExchangeService is already Stopped."} 
 }
Write-Host -ForegroundColor Green "All Exchange Services in Automatic Startup Mode are now Stopped"
}
 
elseif($Services -like 'restart')
{

 $Service = Get-WmiObject -ComputerName $Server win32_service -filter "StartMode='Auto' and Name like '%Exchange%'"
  
 ForEach ($ExchangeService in $Service)
  {
  Write-Host -ForegroundColor Green "Restarting $ExchangeService"
  ReStart-Service $ExchangeService.Name -Force
  }

  Write-Host -ForegroundColor Green "All Exchange Services in Automatic Startup Mode are now Restarted"
}
else
{
  Write-Host -ForegroundColor Green "'$Services' is an invalid command. Please choose Start Or Stop Or Restart Command."
  Write-Host -ForegroundColor Green "Get Help running -? Or -Help Switch."
}

David

Laisser un commentaire

Choisissez une méthode de connexion pour poster votre commentaire:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

 
%d blogueurs aiment cette page :