GAPTHEGURU

Geek with special skills

Exchange 2007 healt check script

param( [string] $auditlist)

Function Get-CustomHTML ($Header){

$Report = @”

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Frameset//EN” “http://www.w3.org/TR/html4/frameset.dtd“>

<html><head><title>$($Header)</title>

<META http-equiv=Content-Type content=’text/html; charset=windows-1252′>

<meta name=”save” content=”history”>

<style type=”text/css”>

DIV .expando {DISPLAY: block; FONT-WEIGHT: normal; FONT-SIZE: 8pt;
RIGHT: 8px; COLOR: #ffffff; FONT-FAMILY: Arial; POSITION: absolute;
TEXT-DECORATION: underline}

TABLE {TABLE-LAYOUT: fixed; FONT-SIZE: 100%; WIDTH: 100%}

*{margin:0}

.dspcont { display:none; BORDER-RIGHT: #B1BABF 1px solid; BORDER-TOP:
#B1BABF 1px solid; PADDING-LEFT: 16px; FONT-SIZE: 8pt;MARGIN-BOTTOM:
-1px; PADDING-BOTTOM: 5px; MARGIN-LEFT: 0px; BORDER-LEFT: #B1BABF 1px
solid; WIDTH: 95%; COLOR: #000000; MARGIN-RIGHT: 0px; PADDING-TOP: 4px;
BORDER-BOTTOM: #B1BABF 1px solid; FONT-FAMILY: Tahoma; POSITION:
relative; BACKGROUND-COLOR: #f9f9f9}

.filler {BORDER-RIGHT: medium none; BORDER-TOP: medium none; DISPLAY:
block; BACKGROUND: none transparent scroll repeat 0% 0%; MARGIN-BOTTOM:
-1px; FONT: 100%/8px Tahoma; MARGIN-LEFT: 43px; BORDER-LEFT: medium
none; COLOR: #ffffff; MARGIN-RIGHT: 0px; PADDING-TOP: 4px;
BORDER-BOTTOM: medium none; POSITION: relative}

.save{behavior:url(#default#savehistory);}

.dspcont1{ display:none}

a.dsphead0 {BORDER-RIGHT: #B1BABF 1px solid; PADDING-RIGHT: 5em;
BORDER-TOP: #B1BABF 1px solid; DISPLAY: block; PADDING-LEFT: 5px;
FONT-WEIGHT: bold; FONT-SIZE: 8pt; MARGIN-BOTTOM: -1px; MARGIN-LEFT:
0px; BORDER-LEFT: #B1BABF 1px solid; CURSOR: hand; COLOR: #FFFFFF;
MARGIN-RIGHT: 0px; PADDING-TOP: 4px; BORDER-BOTTOM: #B1BABF 1px solid;
FONT-FAMILY: Tahoma; POSITION: relative; HEIGHT: 2.25em; WIDTH: 95%;
BACKGROUND-COLOR: #CC0000}

a.dsphead1 {BORDER-RIGHT: #B1BABF 1px solid; PADDING-RIGHT: 5em;
BORDER-TOP: #B1BABF 1px solid; DISPLAY: block; PADDING-LEFT: 5px;
FONT-WEIGHT: bold; FONT-SIZE: 8pt; MARGIN-BOTTOM: -1px; MARGIN-LEFT:
0px; BORDER-LEFT: #B1BABF 1px solid; CURSOR: hand; COLOR: #ffffff;
MARGIN-RIGHT: 0px; PADDING-TOP: 4px; BORDER-BOTTOM: #B1BABF 1px solid;
FONT-FAMILY: Tahoma; POSITION: relative; HEIGHT: 2.25em; WIDTH: 95%;
BACKGROUND-COLOR: #7BA7C7}

a.dsphead2 {BORDER-RIGHT: #B1BABF 1px solid; PADDING-RIGHT: 5em;
BORDER-TOP: #B1BABF 1px solid; DISPLAY: block; PADDING-LEFT: 5px;
FONT-WEIGHT: bold; FONT-SIZE: 8pt; MARGIN-BOTTOM: -1px; MARGIN-LEFT:
0px; BORDER-LEFT: #B1BABF 1px solid; CURSOR: hand; COLOR: #ffffff;
MARGIN-RIGHT: 0px; PADDING-TOP: 4px; BORDER-BOTTOM: #B1BABF 1px solid;
FONT-FAMILY: Tahoma; POSITION: relative; HEIGHT: 2.25em; WIDTH: 95%;
BACKGROUND-COLOR: #7BA7C7}

a.dsphead1 span.dspchar{font-family:monospace;font-weight:normal;}

td {VERTICAL-ALIGN: TOP; FONT-FAMILY: Tahoma}

th {VERTICAL-ALIGN: TOP; COLOR: #CC0000; TEXT-ALIGN: left}

BODY {margin-left: 4pt}

BODY {margin-right: 4pt}

BODY {margin-top: 6pt}

</style>

<script type=”text/javascript”>

function dsp(loc){

if(document.getElementById){

var foc=loc.firstChild;

foc=loc.firstChild.innerHTML?

loc.firstChild:

loc.firstChild.nextSibling;

foc.innerHTML=foc.innerHTML==’hide’?’show’:’hide’;

foc=loc.parentNode.nextSibling.style?

loc.parentNode.nextSibling:

loc.parentNode.nextSibling.nextSibling;

foc.style.display=foc.style.display==’block’?’none’:’block’;}}

if(!document.getElementById)

document.write(‘<style type=”text/css”>\n’+’.dspcont{display:block;}\n’+ ‘</style>’);

</script>

</head>

<body>

<b><font face=”Arial” size=”5″>$($Header)</font></b><hr size=”8″ color=”#CC0000″>

<font face=”Arial” size=”1″><b>Version 1  |M & C | <A

HREF=’http://mail.exchange.com’>mail.exchange.com</A></b></font><br>

<font face=”Arial” size=”1″>Report created on $(Get-Date)</font>

<div></div>

<div></div>

<div></div>

<div>

“@

Return $Report

}

Function Get-CustomHeader0 ($Title){

$Report = @”

<div>

<h1><a href=”javascript:void(0)” onclick=”dsp(this)”>

<span>show</span>$($Title)</a></h1>

<div>

“@

Return $Report

}

Function Get-CustomHeader ($Num, $Title){

$Report = @”

<h2><a href=”javascript:void(0)” onclick=”dsp(this)”>

<span>show</span>$($Title)</a></h2>

<div>

“@

Return $Report

}

Function Get-CustomHeaderClose{

$Report = @”

</DIV>

<div></div>

“@

Return $Report

}

Function Get-CustomHeader0Close{

$Report = @”

</DIV>

<div></div>

“@

Return $Report

}

Function Get-CustomHTMLClose{

$Report = @”

</div>

</body>

</html>

“@

Return $Report

}

Function Get-HTMLTable{

param([array]$Content)

$HTMLTable = $Content | ConvertTo-Html

$HTMLTable = $HTMLTable -replace ‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”  “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>’, “”

$HTMLTable = $HTMLTable -replace ‘<html xmlns=”http://www.w3.org/1999/xhtml”>’, “”

$HTMLTable = $HTMLTable -replace ‘<head>’, “”

$HTMLTable = $HTMLTable -replace ‘<title>HTML TABLE</title>’, “”

$HTMLTable = $HTMLTable -replace ‘&lt;’, “<”

$HTMLTable = $HTMLTable -replace ‘&gt;’, “>”

$HTMLTable = $HTMLTable -replace ‘</head><body>’, “”

$HTMLTable = $HTMLTable -replace ‘</body></html>’, “”

Return $HTMLTable

}

Function Get-HTMLLink ($activeURL){

$Report = @”

<a href=$activeURL>$activeURL</a>

“@

Return $Report

}

Function Get-Ink ([String]$inData){

[String]$inclPercentage = @(35..100)

$positive = ($inclPercentage, ‘Success’, ‘Ready’, ‘Running’, ‘OK’, ‘True’, ‘Information’)

If ($positive -match $inData)

{

$Report = @”

<font color=’#009900′>$inData</font>

“@

}

Else

{

$Report = @”

<font color=’#FF0000′>$inData</font>

“@

}

Return $Report

}

Function Get-HTMLBasic ($Detail){

$Report = @”

<TABLE>

<tr>

<td width=’75%’>$($Detail)</td>

</tr>

</TABLE>

“@

Return $Report

}

Function Get-HTMLDetail ($Heading, $Detail){

$Report = @”

<TABLE>

<tr>

<th width=’25%’><b>$Heading</b></font></th>

<td width=’75%’>$($Detail)</td>

</tr>

</TABLE>

“@

Return $Report

}

$input | foreach {$targets += @($_)}

If ((Test-Path variable:\targets) -eq $True){

Write-Host “Server list input detected on pipeline” -ForegroundColor Yellow

}

Else{

if ($auditlist -eq “”){

Write-Host “No server list specified, getting all Exchange 2007 servers” -ForegroundColor Yellow

$targets = Get-ExchangeServer | Where-Object {$_.IsExchange2007OrLater -eq $True}

}

else

{

if ((Test-Path $auditlist) -eq $false)

{

Write-Host “Invalid server list specified: $auditlist” -ForegroundColor DarkRed

exit

}

else

{

Write-Host “Using Audit list: $auditlist” -ForegroundColor Cyan

$Targets = Get-Content $auditlist

}

}

}

$now = Get-Date

#Custom Expressions

$latencyMS = @{Name=”Latency(MS)”;expression={[Math]::Round(([TimeSpan] $_.Latency).TotalMilliSeconds)}}

$MessageLatencyTime = @{Name=”MessageLatencyTime”;expression={[Math]::Round(([TimeSpan] $_.Latency).TotalMilliSeconds)}}

$hotLink = @{Name=”URL”;expression={Get-HTMLLink ($_.URL)}}

$colourResult = @{Name=”Result”;expression={Get-Ink ($_.Result)}}

$colourStatus = @{Name=”Status”;expression={Get-Ink ($_.Status)}}

$colourType = @{Name=”Status”;expression={Get-Ink ($_.Type)}}

$newResult = @{Name=”Result”;expression={If ($_.Result.ToString() -ne
‘Success’){Get-Ink (‘Failure’)} Else {Get-Ink (‘Success’) }}}

$fullReport = Get-CustomHTML “Exchange 2007 “

$fullReport += Get-CustomHTMLClose

Foreach ($Target in $Targets){

Write-Host “Collating Detail for $Target” -ForegroundColor Yellow

Write-Host “..getting basic computer configuration”

$ComputerSystem = Get-WmiObject -computername $Target Win32_ComputerSystem

switch ($ComputerSystem.DomainRole){

0 { $ComputerRole = “Standalone Workstation” }

1 { $ComputerRole = “Member Workstation” }

2 { $ComputerRole = “Standalone Server” }

3 { $ComputerRole = “Member Server” }

4 { $ComputerRole = “Domain Controller” }

5 { $ComputerRole = “Domain Controller” }

default { $ComputerRole = “Information not available” }

}

$OperatingSystems = Get-WmiObject -computername $Target Win32_OperatingSystem

$TimeZone = Get-WmiObject -computername $Target Win32_Timezone

$Keyboards = Get-WmiObject -computername $Target Win32_Keyboard

$SchedTasks = Get-WmiObject -computername $Target Win32_ScheduledJob

$BootINI = $OperatingSystems.SystemDrive + “boot.ini”

$RecoveryOptions = Get-WmiObject -computername $Target Win32_OSRecoveryConfiguration

$exServer = Get-ExchangeServer | where {$_.Name -eq “$Target”}

$exVersion = “Version ” + $exServer.AdminDisplayVersion.Major + “.” +
$exServer.AdminDisplayVersion.Minor + ” (Build ” +
$exServer.AdminDisplayVersion.Build + “.” +
$exServer.AdminDisplayVersion.Revision + “)”

switch ($ComputerRole){

“Member Workstation” { $CompType = “Computer Domain”; break }

“Domain Controller” { $CompType = “Computer Domain”; break }

“Member Server” { $CompType = “Computer Domain”; break }

default { $CompType = “Computer Workgroup”; break }

}

$LBTime=$OperatingSystems.ConvertToDateTime($OperatingSystems.Lastbootuptime)

$MyReport += Get-CustomHeader0  “$Target – Role(s): $($exServer.ServerRole)”

$MyReport += Get-CustomHeader “2″ “Basic Server Information”

$MyReport += Get-HTMLDetail “Computer Name” ($ComputerSystem.Name)

$MyReport += Get-HTMLDetail “Computer Role” ($ComputerRole)

$MyReport += Get-HTMLDetail “Exchange Role(s)” ($exServer.ServerRole)

$MyReport += Get-HTMLDetail “Last System Boot” ($LBTime)

$MyReport += Get-CustomHeaderClose

Write-Host “..getting logical disk configuration”

$Disks = Get-WmiObject -ComputerName $Target Win32_LogicalDisk

$MyReport += Get-CustomHeader “2″ “Logical Disk Configuration”

$LogicalDrives = @()

Foreach ($LDrive in ($Disks | Where {$_.DriveType -eq 3})){

$Details = “” | Select “Drive Letter”, Label, “File System”, “Disk Size (GB)”, “Disk Free Space”, “% Free Space”

$Details.”Drive Letter” = $LDrive.DeviceID

$Details.Label = $LDrive.VolumeName

$Details.”File System” = $LDrive.FileSystem

$Details.”Disk Size (GB)” = [math]::round(($LDrive.size / 1GB))

$Details.”Disk Free Space” = [math]::round(($LDrive.FreeSpace / 1GB))

$Details.”% Free Space” = Get-Ink ([Math]::Round(($LDrive.FreeSpace /1GB) / ($LDrive.Size / 1GB) * 100))

$LogicalDrives += $Details

}

$MyReport += Get-HTMLTable ($LogicalDrives)

$MyReport += Get-CustomHeaderClose

if ($exServer.ServerRole -like “*ClientAccess*”)

{$MyReport += Get-CustomHeader “2″ “OWA Connectivity”

Write-Host “..performing OWA connectivity test”

$colOWAResults = Test-OwaConnectivity -ClientAccessServer $Target

$MyReport += Get-HTMLTable ($colOWAResults | select MailboxServer, $hotLink, Scenario, $colourResult, $latencyMS, Error)

$MyReport += Get-CustomHeaderClose

}

Write-Host “..getting queue details”

if ($exServer.ServerRole -like “*HubTransport*”)

{

$MyReport += Get-CustomHeader “2″ “Queue Information”

$colQs = Get-Queue -server $Target

$MyReport += Get-HTMLTable ($colQs | Select-Object NextHopDomain, $colourStatus, MessageCount, NextRetryTime)

$MyReport += Get-CustomHeaderClose

}

Write-Host “..getting mailbox database information”

$spaceLog=[System.Diagnostics.EventLog]::GetEventLogs($target) | where {($_.LogDisplayName -eq “Application”)}

$db = @{Name=”database”;Expression={$_.ReplacementStrings[1]}}

$freeMB = @{Name=”MB”;Expression={[int]$_.ReplacementStrings[0]}}

$whiteSpace = $spaceLog.entries | where {($_.TimeWritten -ge
$now.AddDays(-1))} | where {($_.EventID -eq “1221″)} | select
$db,$freeMB

$ws = @{Name=”White Space”;expression={}}

if ($exServer.ServerRole -like “*Mailbox*”)

{

$MyReport += Get-CustomHeader “2″ “Mailbox Stores”

$colMailboxStores = Get-MailboxDatabase -Server $Target -Status | Sort-Object Name

$storeTable = @()

Foreach ($objMailboxStore in $colMailboxStores)

{

[string]$totalUsers = (get-mailbox -database $objMailboxStore).count

[string]$empty = $totalUsers.Length -eq 0

if ($empty -eq ‘True’)

{

[string]$totalUsers = “0″

}

$storeDetails = “” | Select Name, Mounted, “Total Users”, “White Space”, LastFullBackup

$storeDetails.Name = $objMailboxStore.Name

$storeDetails.Mounted = Get-Ink ($objMailboxStore.Mounted)

$storeDetails.”Total Users” = $totalUsers

$storeDetails.”White Space” = (($whitespace | where {$_.database -match $objMailboxStore.Name} | select -last 1).mb)

$storeDetails.LastFullBackup = $objMailboxStore.LastFullBackup

$storeTable += $storeDetails

}

$MyReport += Get-HTMLTable ($storeTable)

$MyReport += Get-CustomHeaderClose

$MyReport += Get-CustomHeader “2″ “MAPI Connectivity”

Write-Host “..performing MAPI connectivity test”

$colMAPIResults = Test-MAPIConnectivity -Server $Target

$MyReport += Get-HTMLTable ($colMAPIResults | select Database, $newResult, $latencyMS, Error)

$MyReport += Get-CustomHeaderClose

$MyReport += Get-CustomHeader “2″ “Mailflow Test”

Write-Host “..performing Mailflow test”

$colMailflowResults = Test-Mailflow -Server $Target

$MyReport += Get-HTMLTable ($colMailflowResults | select TestMailflowResult, $MessageLatencyTime)

$MyReport += Get-CustomHeaderClose

}

Write-Host “..getting Exchange services”

$ListOfServices = (gwmi -computer $Target -query “select * from
win32_service where Name like ‘MSExchange%’ or Name like ‘IIS%’ or Name
like ‘SMTP%’ or Name like ‘POP%’ or Name like ‘W3SVC%’”)

$MyReport += Get-CustomHeader “2″ “Exchange Services”

$Services = @()

Foreach ($Service in $ListOfServices){

$Details = “” | Select Name,Account,”Start Mode”,State,”Expected State”

$Details.Name = $Service.Caption

$Details.Account = $Service.Startname

$Details.”Start Mode” = $Service.StartMode

If ($Service.StartMode -eq “Auto”)

{

if ($Service.State -eq “Stopped”)

{

$Details.State = $Service.State

$Details.”Expected State” = Get-Ink (“Unexpected”)

}

}

If ($Service.StartMode -eq “Auto”)

{

if ($Service.State -eq “Running”)

{

$Details.State = $Service.State

$Details.”Expected State” = Get-Ink (“OK”)

}

}

If ($Service.StartMode -eq “Disabled”)

{

If ($Service.State -eq “Running”)

{

$Details.State = $Service.State

$Details.”Expected State” = Get-Ink (“Unexpected”)

}

}

If ($Service.StartMode -eq “Disabled”)

{

if ($Service.State -eq “Stopped”)

{

$Details.State = $Service.State

$Details.”Expected State” = Get-Ink (“OK”)

}

}

If ($Service.StartMode -eq “Manual”)

{

$Details.State = $Service.State

$Details.”Expected State” = Get-Ink (“OK”)

}

If ($Service.State -eq “Paused”)

{

$Details.State = $Service.State

$Details.”Expected State” = Get-Ink (“OK”)

}

$Services += $Details

}

$MyReport += Get-HTMLTable ($Services)

$MyReport += Get-CustomHeaderClose

$eventLogs=[System.Diagnostics.EventLog]::GetEventLogs($Target) |
where {($_.LogDisplayName -eq “Application”) -OR ($_.LogDisplayName -eq
“System”)}

$warningEvents = @()

$errorEvents = @()

$LogSettings = @()

$MyReport += Get-CustomHTMLClose

$fullReport += $MyReport

$MyReport = $null

}

$Date = Get-Date

$Filename = “.\” + “Exchange_Health” + “_” + $date.Hour + $date.Minute +
“_” + $Date.Day + “-” + $Date.Month + “-” + $Date.Year + “.htm”

$fullReport | out-file -encoding ASCII -filepath $Filename

Write “Audit saved as $Filename”

Invoke-Item $Filename

Advertisements

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: