Monitoreo a Microsoft Exchange Server 2010/2007 por PowerShell

Esta secuencia de comandos PowerShell seguimiento de los distintos servicios de los controladores de dominio, los servicios de Exchange, bases de datos de Exchange y realiza una prueba de flujo de correo.

1. Abrir el bloc de notas

2. Copiar y pegar el texto a continuación a la agenda

3. Guarde el archivo con extensión. Ps1.

# ———- SCRIPT STARTS HERE————–

$runscript=”true”

Do {

function SendEmail
{param ($title,$body)
$SmtpServer = “mail.domain.com”
$From = “it@domain.com”
$To = “administrator@domain.com”
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = $SmtpServer
$SmtpClient.Send($From,$To,$Title,$Body)
}

#CSV spreadsheet must contain hostname and role columns
#multiple roles – dc,hubcas,mailbox,
#28. Create monitoring script for major services and forward eventlongs via WMI?
#> services, processor, forward error eventlogs(wmi)?, exchange test commandlets
#> monitor cluster resources / if DBs are mounted (check if msexchangeis is start, if yes.. check dbs are mounted)
#forward error event logs
# For DCs, it monitors DNS, DHCP, DFS, NTFRS, KDC, NETLOGON, W32TIME
$serverlist=”c:\AD_exchange_serverlist.csv”
import-CSV $serverlist | foreach-object {

if ( $_.role -match “dc” ) {
$hostname=$_.hostname

$dnsservice=gwmi -computername $hostname -query “select * from win32_service where name=’dns'”
if ($dnsservice.state -eq “Stopped”) {SendEmail -title “$hostname – DNS service is stopped” -body “$hostname DNS service is stopped, please take action”}

$dhcpservice=gwmi -computername $hostname -query “select * from win32_service where name=’DHCPServer'”
if ($dhcpservice.state -eq “Stopped”) {SendEmail -title “$hostname – DHCP service is stopped” -body “DHCP service is stopped on $hostname, please take action”}

$dfsservice=gwmi -computername $hostname -query “select * from win32_service where name=’DFS'”
if ($dfsservice.state -eq “Stopped”) {SendEmail -title “$hostname – DFS service is stopped” -body “DFS service is Stopped on $hostname, please take action”}

$ntfrsservice=gwmi -computername $hostname -query “select * from win32_service where name=’NtFrs'”
if ($ntfrsservice.state -eq “Stopped”) {SendEmail -title “$hostname – NtFRS service is stopped” -body “NtFRS service is stopped on $hostname, please take action”}

$kdcservice=gwmi -computername $hostname -query “select * from win32_service where name=’kdc'”
if ($kdcservice.state -eq “Stopped”) {SendEmail -title “$hostname – KDC service is stopped” -body “KDC service is stopped on $hostname, please take action”}

$netlogonservice=gwmi -computername $hostname -query “select * from win32_service where name=’netlogon'”
if ($netlogonservice.state -eq “Stopped”) {SendEmail -title “$hostname – NetLogon service is stopped” -body “NetLogon service is stopped on $hostname, please take action”}

$timeservice=gwmi -computername $hostname -query “select * from win32_service where name=’w32time'”
if ($timeservice.state -eq “Stopped”) {SendEmail -title “$hostname – W32Time service is stopped” -body “W32Time service is stopped on $hostname, please take action”}
}

if ( $_.role -match “hubcas” ) {
$hostname=$_.hostname
$stoppedservices=gwmi -computername $hostname -query “select * from win32_service where state=’stopped’ and startmode=’auto'”
$servicestatus=Test-ServiceHealth -Server $hostname
$servicestatus | foreach { if ($_.requiredservicesrunning -ne $true){sendemail -title “$hostname – Some services are not running” -body $stoppedservices}}

}

if ( $_.role -match “mailbox” ) {
$hostname=$_.hostname
$stoppedservices=gwmi -computername $hostname -query “select * from win32_service where state=’stopped’ and startmode=’auto'”
$servicestatus=Test-ServiceHealth -Server $hostname
$servicestatus | foreach { if ($_.requiredservicesrunning -ne $true){sendemail -title “$hostname – some services are not running” -body $stoppedservices}}

$mailflowtest=Test-Mailflow -Identity $hostname
if ($mailflowtest.testmailflowresult -ne “Success”) {sendemail -title “$hostname – Mailflow test Failed” -body $mailflowtest}

$dbstatus=Get-MailboxDatabase -Status -server $hostname
$dbcheck=Get-MailboxDatabase -Status -server $hostname | foreach { if ($_.mounted -ne $true) {sendemail “Database disounted: $_” “$_ on $hostname is dismounted. Please take action.”}}

}
}
sleep -Seconds 3600
} while ($runscript -eq “true”)

# ———- SCRIPT ENDSS HERE————–

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s


A %d blogueros les gusta esto: