#run script with "casrepair.ps1 -ErrorAction "Continue" to see error messages
#$script:ErrorActionPreference = "SilentlyCOntinue"
# Set environmental and global variables variables
$LocalServerName = hostname
$ActiveSyncMailboxName = "Windows SBS Mobile Mailbox Policy" + " " + $LocalServerName
$OABVDir = $LocalServerName + "\OAB (SBS Web Applications)"
$OAB = Get-OfflineAddressBook | Select-Object -Property Name
$strDomainDNS = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
$DomainAdmins = $strDomainDNS + "\Domain Admins"
$OrgName = Get-OrganizationConfig | Select-Object -Property DistinguishedName
$DefaultExchangeCertificate = "CN=" + $LocalServerName
######################################################################
#
# BackupSettings
#
#
# Create backups in case we need to revert any changes made by the script
#
######################################################################
function BackupSettings{
# first backup IIS
cd $env:windir\system32\inetsrv
.\appcmd add backup
# export AD config
$mytime = get-date
$filenum = $mytime.ToFileTime()
$filename = "$($env:temp)\httpbackup_$($filenum).ldf"
$root = [ADSI]"LDAP://rootDSE"
$objConfNamingContext = [ADSI]"LDAP://$($root.configurationNamingContext)"
$strFilter = "(ObjectClass=msExchProtocolCfgHTTPContainer)"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objConfNamingContext
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = @("name","distinguishedName")
# use out-null to supress the 0, 1, etc output
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)| out-null}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objItem = $objResult.Properties
ldifde -m -f $filename -d $objItem.distinguishedname
}
# backup applicationhost.config manually
copy -force $env:windir\system32\inetsrv\config\applicationhost.config $env:temp\applicationhost.config_$filenum
}
######################################################################
#
# RemoveDirectoriesUsingPowerShell
#
# First try to use Powershell to gracefully remove the existing
# vdirs from the Default Web Site and
#
######################################################################
function RemoveDirectoriesUsingPowerShell{
#remove certificates
Get-ExchangeCertificate | Where { $_.Subject -eq "$DefaultExchangeCertificate" } | ForEach { Remove-ExchangeCertificate -Thumbprint $_.Thumbprint }
# clean up any owa virtual directories
Get-OWAVirtualDirectory | Remove-OWAVirtualDirectory -Confirm:$False
# delete OWS
Get-WebServicesVirtualDirectory | Remove-WebServicesVirtualDirectory -Confirm:$false
# remove activesync, oab, UM, autodiscovery
Get-ActiveSyncVirtualDirectory | Remove-ActiveSyncVirtualDirectory -Confirm:$false
Get-OabVirtualDirectory | Remove-OabVirtualDirectory -Force:$true -Confirm:$false
Get-UMVirtualDirectory | Remove-UMVirtualDirectory -Confirm:$false
Get-AutodiscoverVirtualDirectory | Remove-AutodiscoverVirtualDirectory -Confirm:$false
}
######################################################################
#
# RemoveDirectoriesUsingAppcmd
#
# Use appcmd to try to remove any lingering objects that PowerShell
# wasn't able to remove
#
######################################################################
function RemoveDirectoriesUsingAppcmd{
# clean up any lingering objects, we'll try via appcmd first, then manually edit
cd $env:windir\system32\inetsrv
.\appcmd delete app /app.name:"SBS Web Applications/owa" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Exadmin" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Exchange" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Exchweb" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Public" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/EWS/bin" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/EWS" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Microsoft-Server-ActiveSync" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/UnifiedMessaging/bin" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/UnifiedMessaging" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Autodiscover/bin" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Autodiscover/help" | out-null
.\appcmd delete app /app.name:"SBS Web Applications/Autodiscover" | out-null
.\appcmd delete vdir /vdir.name:"SBS Web Applications/OAB" | out-null
}
######################################################################
#
# RemoveEntriesFromApplicationHost
#
# Finally, if PowerShell and appcmd weren't able to delete the vdirs
# we'll remove them from the applicationhost.config
#
######################################################################
function RemoveEntriesFromApplicationHost{
$sites = @("owa","ews","UnifiedMessaging","Autodiscover","Exadmin","Exchange","Public","Exchange ActiveSync","Microsoft-Server-Activesync","Exchweb","oab","OAB")
# load xml file in to memory
$applicationHost = New-Object XML
$applicationHost.Load("c:\windows\system32\inetsrv\config\applicationhost.config")
#default web site and sbs web applications site objects
$dws = $applicationHost.Configuration."system.applicationHost".sites.site | Where-Object { $_.name -eq 'Default Web Site' }
$swa = $applicationHost.Configuration."system.applicationHost".sites.site | Where-Object { $_.name -eq 'SBS Web Applications' }
## delete keys values under <customMetadata>
$keys = $applicationHost.Configuration."system.applicationHost".customMetadata
# remove paths
foreach($site in $sites)
{
foreach ($key in $keys.key) { if ($key.path -match $site ){$keys.RemoveChild($key)} }
# clean up SBS Web Applications first
foreach ($app in $swa.application) { if ($app.path -match $site) {$swa.RemoveChild($app) } }
# Get any remnants on the Default Web Site
foreach ($app in $dws.application) { if ($app.path -match $site) {$dws.RemoveChild($app) } }
#clean up locations (web sites)
foreach ($loc in $applicationHost.configuration.location) { if ($loc.path -match $site) { $applicationHost.configuration.RemoveChild($loc) }}
}
$applicationHost.Save("c:\windows\system32\inetsrv\config\applicationhost.config")
iisreset /noforce
}
######################################################################
#
# RemoveEntriesFromAD
#
# Remove lingering AD objects from CN=HTTP,CN=Protocol
# CN=HTTP,CN=Protocols,CN=MARKSTANSBS,CN=Servers,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrativ
# eGroups,CN=First Organization,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=markstancom,DC=loca
#
######################################################################
function RemoveEntriesFromAD {
# TODO - limit to only server itself
$strFilter = "(&(ObjectClass=msExchVirtualDirectory)(!(name=*rpc*)))"
$root = [ADSI]"LDAP://rootDSE"
$objConfNamingContext = [ADSI]"LDAP://$($root.configurationNamingContext)"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objConfNamingContext
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = @("name","distinguishedName")
# use out-null to supress the 0, 1, etc output
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)| out-null}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objItem = $objResult.Properties
$owadir=[ADSI]"LDAP://$($objItem.distinguishedname)"
$owadir.psbase.deleteTree()
write-host "Deleting $($objItem.name)"
}
}
######################################################################
#
# RecreateDirectories
#
# Creates OWA vdirs using the same settings as SBS default install
#
######################################################################
function RecreateDirectories{
#create new vdirs
New-OWAVirtualDirectory -WebsiteName "SBS Web Applications" -OwaVersion "Exchange2007" -ExternalAuthenticationMethods Fba
Set-OWAVirtualDirectory -InternalUrl "https://sites/owa/" -ClientAuthCleanupLevel "Low" -LogonFormat "UserName" -DefaultDomain $strDomainDNS -Identity "Owa (SBS Web Applications)"
New-OWAVirtualDirectory -WebsiteName "SBS Web Applications" -OwaVersion "Exchange2003or2000" -VirtualDirectoryType "Exadmin" -ExternalAuthenticationMethods Fba
New-OWAVirtualDirectory -WebsiteName "SBS Web Applications" -OwaVersion "Exchange2003or2000" -VirtualDirectoryType "Mailboxes" -ExternalAuthenticationMethods Fba
New-OWAVirtualDirectory -WebsiteName "SBS Web Applications" -OwaVersion "Exchange2003or2000" -VirtualDirectoryType "Exchweb" -ExternalAuthenticationMethods Fba
New-OWAVirtualDirectory -WebsiteName "SBS Web Applications" -OwaVersion "Exchange2003or2000" -VirtualDirectoryType "PublicFolders" -ExternalAuthenticationMethods Fba
New-WebServicesVirtualDirectory -WebsiteName "SBS Web Applications" -InternalUrl "https://Sites/EWS/Exchange.asmx" -basicauthentication 1 -windowsauthentication 1
New-ActiveSyncVirtualDirectory -WebsiteName "SBS Web Applications" -InternalUrl "https://Sites/Microsoft-Server-ActiveSync" -ExternalAuthenticationMethods Basic -InternalAuthenticationMethods Basic
New-OabVirtualDirectory -WebsiteName "SBS Web Applications" -InternalUrl "https://Sites/OAB"
Set-OabVirtualDirectory -PollInterval "30" -Identity "oab (sbs web applications)"
New-UMVirtualDirectory -WebsiteName "SBS Web Applications" -InternalUrl "https://Sites/UnifiedMessaging/Service.asmx"
New-AutodiscoverVirtualDirectory -WebsiteName "SBS Web Applications" -InternalUrl "https://Sites/Autodiscover/Autodiscover.xml" -BasicAuthentication 1 -WindowsAuthentication 1
#configure vdirs
Set-ClientAccessServer -Identity $LocalServerName -AutoDiscoverServiceInternalUri "https://sites/Autodiscover/Autodiscover.xml"
Set-OfflineAddressBook $OAB.Name -VirtualDirectories $OABVDir -Versions Version2,Version3,Version4 -PublicFolderDistributionEnabled:$True
iisreset /noforce
# configure vdirs with SBS defaults
cd $env:windir\system32\inetsrv
.\appcmd.exe unlock config "-section:system.webserver/security/authentication/windowsauthentication"
.\appcmd.exe set config "SBS Web Applications/ews" "-section:windowsAuthentication" "-useKernelMode:False" /commit:apphost
.\appcmd.exe set config "SBS Web Applications/AutoDiscover" "-section:windowsAuthentication" "-useKernelMode:False" /commit:apphost
.\appcmd.exe set config "SBS Web Applications/oab" "-section:windowsAuthentication" "-useKernelMode:False" /commit:apphost
.\appcmd.exe set site "Default Web Site" /Bindings:"http/*:80:"
.\appcmd.exe start site "Default Web Site"
.\appcmd.exe start site "SBS Web Applications"
}
######################################################################
#
# Begin main logic flow here
#
######################################################################
######################################################################
BackupSettings
RemoveDirectoriesUsingPowerShell
RemoveDirectoriesUsingAppcmd
RemoveEntriesFromApplicationHost
RemoveEntriesFromAD
RecreateDirectories
######################################################################
######################################################################