SCRIPT: IMExIntegration.ps1

Script Name: IMExIntegration.ps1

Script Author: Anderson Patricio, MVP

Original Post: http://msmvps.com/blogs/andersonpatricio/pages/script-imexintegration-ps1-v1.aspx

Log:
v1.0 – Initial script. Main features: backup of the web.config; OWAVirtualDirectory configuration; IISreset afterwards; list of all OCS Front End/Pool servers; adds automatically all fields required for the integration on the web.config

Code:

#
# Script created by Anderson Patricio (http://msmvps.org/blogs/AndersonPatricio)
#
# Syntax:
# IMExIntegration.ps1
#

Function ValidateCerSerialNumber($tmpSN) {
 $tmpSerialNumber = ""
 For ( $count =0; $count -le $vExCert.SerialNumber.length-2; $count=$count+2) {
 $tmpSerialNumber = $tmpSerialNumber + $vExCert.SerialNumber.substring($count,2) + " "
 }
 Return $tmpSerialNumber
}

Function SwapFile(){
 mv web.config web.config.backup.script
 copy-item .\IMBackup\web.config.new web.config
 Get-OWAVirtualDirectory | Set-OWAVirtualDirectory -InstantMessagingType OCS
}

Function BackupFile(){
 If (Test-Path "IMBackup") { write-host "[INFO] IMBackup folder already exists." -ForeGroundColor Yellow } Else { mkdir IMBackup }
 $BackupFileName = "IMBackup\web.config.backup." + (get-date -uformat "%Y - %m - %d - %A - Time - %s")
 Copy-item $DefaultPath"\web.config" $BackupFileName
 If (Test-Path ".\IMBackup\web.config.new") { remove-item ".\IMBackup\web.config.new"; write-host "[INFO] An old file (\IMBackup\web.config.new) created by this script was found on the backup folder. File was deleted to avoid conflict with the current operation." -ForeGroundColor yellow }

}
Function EnableOWA(){
 Get-OWAVirtualDirectory | Set-OWAVirtualDirectory -InstantMessagingType OCS
 start-process C:\windows\system32\IISReset.exe
}

#
# Global variables definition
$DefaultPath = Get-ItemProperty -path hklm:"system\CurrentControlSet\Control\Session Manager\Environment" -name ExchangeInstallPath
$DefaultScriptPath = $DefaultPath.ExchangeInstallPath + "Scripts\Get-UCPool.ps1"
$DefaultPath = $DefaultPath.ExchangeInstallPath + "ClientAccess\OWA"
cd $DefaultPath

BackupFile;
$vOriginalValues = Get-Content .\web.config | where-object { $_ -like "*add key=?IM*" }
$vEXCert = Get-ExchangeCertificate | Where-Object { $_.Services -like "*IIS*" } | select Issuer,Subject,SerialNumber

$vNewSerialNumber = ValidateCerSerialNumber($vEXCert.SerialNumber);

#Define new values for IM integration..

Write-host 'These are the OCS Front End/pool entries that were found by the get-UCPool.ps1 script:' -foregroundcolor yellow
write-host $defaultScriptpath
& $defaultScriptpath
write-host
write-host 'Please, type in the name of your Pool/Front End OCS server: ' -nonewline -foregroundcolor Red
$vOCS = Read-Host

Write-host " Based on your current certificate these are the values that will be used to integrate OCS(IM) and Exchange Server 2010 Outlook Web App" -ForeGroundColor yellow
$vIMPoolName = '                <add key="IMPoolName" value="' + $vOCS + '" />'
$vIMCertificateIssuer = '                <add key="IMCertificateIssuer" value="' + $vEXCert.Issuer +'" />'
$vIMCertificateSerialNumber = '                <add key="IMCertificateSerialNumber" value="' + $vNewSerialNumber.trim() + '" />'
write-host $vIMPoolName
write-host $vIMCertificateIssuer
write-host $vIMCertificateSerialNumber

#
# Creating a new web.config file based on the new values...
#
Get-Content .\web.config | ForEach-Object {
 $vChanges = $false
 if ( $_ -eq $voriginalvalues[0]) { Add-content IMBackup\web.config.new -value $vIMPoolName
        $vChanges = $true
      }
 if ( $_ -eq $voriginalvalues[1]) { Add-content IMBackup\web.config.new -value $vIMCertificateIssuer
        $vChanges = $true
      }
 if ( $_ -eq $voriginalvalues[2]) { Add-content IMBackup\web.config.new -value $vIMCertificateSerialNumber
        $vChanges = $true
      }
 If ( $vChanges -eq $false ) { Add-Content IMBackup\web.config.new -value $_ }
 #write-host $vchanges
}

One thought on “SCRIPT: IMExIntegration.ps1

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.