Friday, May 23, 2014

Monitoring Exchange ActiveSync connection for specific users

To prevent a situation where the company managers have problems with reading mail from mobile devices, it is desirable to know about problems in advance. This can be done for example in the following script, which sends e-mail the last known good synchronization and the difference with the current time. If the time difference is greater than 3:00 script highlighted.
In this script, you need to replace the following values
"MyManager", "MyDirector" - Display Name from Active Directory
$msg.From, $msg.To.add, $SMTPServer - Your SMTP parameters

Download monitor_active_sync.ps1

# Load Exchange commands
if (!(Get-Command Get-ExchangeServer -ErrorAction SilentlyContinue))
{
    if (Test-Path "C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1")
    {
        . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
        Connect-ExchangeServer -auto
    }
    elseif (Test-Path "C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1")
    {
        Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin
        .'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'
    }
    else
    {
        throw "Exchange Management Shell cannot be loaded"
    }
}

$DeltaTime = $DateNow = $MobileDevices = @()

$DateNow = Get-date
$MobileDevices = Get-ActiveSyncDevice | where { $_.identity -match "MyManager" -or $_.identity -match "MyDirector" } | Sort-Object identity | Get-ActiveSyncDeviceStatistics

$msg = new-object Net.Mail.MailMessage
$msg.Body = "<html> 
<body> 
<font size=""1"" face=""Arial,sans-serif""> 
<h3 align=""center"">Notice about ActiveSync</h3> 
</font><table border=""0"" cellpadding=""3"" style=""font-size:8pt;font-family:Arial,sans-serif""> 
<tr bgcolor=""#009900""> 
<th><font color=""#ffffff"">Identity:</font></th> 
<th><font color=""#ffffff"">DeviceUserAgent:</font></th> 
<th><font color=""#ffffff"">LastSuccessSync GMT:</font></th> 
<th><font color=""#ffffff"">DeltaTime:</font></th> 
</tr>"

Foreach ($MobileDevice in $MobileDevices)
{
    write-host $MobileDevice.Identity "User Agent: " $MobileDevice.DeviceUserAgent "Success Sync GMT: "  $MobileDevice.LastSuccessSync
    if ($MobileDevice.LastSuccessSync)
    {
        $DeltaTime = new-timespan -start ($DateNow.ToUniversalTime()) -end ($MobileDevice.LastSuccessSync)
        write-host "Last sync delta time" $DeltaTime
    }
    else { write-host "No sync" }
    if ($DeltaTime -le '-3:00:00.0000000') { write-host "DeltaTime great 3 hour" }
    $msg.Body += "<tr bgcolor=""#dddddd""><th>$($MobileDevice.Identity)</th>
<th>$($MobileDevice.DeviceUserAgent)</th>
<th>$($MobileDevice.LastSuccessSync)</th>"
    if ($DeltaTime -le '-3:00:00.0000000') { $msg.Body += "<th><b><span style=""color: red;"">$($DeltaTime)</span></b></th>
</tr>"
    }
    else { $msg.Body += "<th>$($DeltaTime)</th>
</tr>"
    }
}

$msg.Body += "</table>
</body></html>"

$msg.Body | Out-File .\report.html
$msg.IsBodyHTML = $true
$msg.From = "FromEmail@YourDomain"
$msg.To.add("YourEmail@YourDomain")
$msg.Subject = "Notice about ActiveSync"
$SMTPServer = "YourEmailServer"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($Msg)

How quickly free up disk space with transaction logs Exchange

If you have a critical situation occurred and an urgent need to release the disc with transaction logs, you can cheat Exchange server as if you made ​​a backup. To do this, follow these steps:

1. Example: Create a folder on drive "C:\Scripts"
2. "C:\Scripts" folder create a file "VSSClean.txt" with content (letters of your disks should be corrected):
# DiskShadow Script File to Fix VSS Writers (PLEASE Do NOT REMOVE)

begin backup
add volume D:
add volume E:
create
end backup
list shadows all
delete shadows all
# End of script

3. "C:\Scripts" folder create a file "VSSClean.cmd" with content:
diskshadow -s C:\Scripts\VSSClean.txt

4. Finally execute the file VSSClean.cmd

A good practice is to create a task on schedule to check the disk is full with logs. For example, the script below checks powershell disc name (in my case it "TLOGS") and if the space is less than 50 gigabytes script is run to remove the logs.

$disks = Get-WmiObject win32_logicaldisk | Where-Object { $_.drivetype -eq 3 }
foreach ($disk in $disks)
{
    $a1 = [math]::Round(($disk.FreeSpace/1073741824), 0)
    if ($disk.VolumeName -match "TLOGS" -and $a1 -le 50)
    {
        start-process C:\Scripts\VSSClean.cmd
    }
}

Download monitor_tlogs.ps1
Download VSSClean.cmd
Download VSSClean.txt

Friday, May 9, 2014

Error "The type initializer for 'Microsoft.Exchange.Diagnostics.ExTraceConfiguration' threw an exception."

After installing KB2506143 "Windows Management Framework 3.0" having problems using Powershell scripts for Microsoft Exchange 2010 with the following error "The type initializer for 'Microsoft.Exchange.Diagnostics.ExTraceConfiguration' threw an exception.". Windows offers a search solution on the Internet, but found nothing. Therefore, we must do the following:
1. Reinstall Exchange Manage Tools
2. In batch scripts to add the parameter "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -version 2.0"
3. Rename or delete folders in profile "%userprofile%\AppData\Roaming\Microsoft\Exchange\RemotePowerShell"