Exchange Server Script CentralComments (0)

This page will contain tons of useful code/cmdlets and scripts!

Comments

You need sign in to comment! Sign up today!

Fix msExchHomeServerComments (0)

Here is a script for fixing msExchHomeServer for all mailboxes with an invalid server (specified when starting the script).

###############################################################################################
#    SCRIPT      :    set-AllmsExchHomeServer
#    WRITTEN BY  :    Andreas Karlsson (CGI NKP)
#    DATE        :    2014-04-25
#    OVERVIEW    :    A script for fixing all Faulty msExhHomeServer-attributes
#             
#    DESCRIPTION :    Make sure to have executionpolicies which allow scripts to be run, ie:
#                :    Run Powershell as admin, set-ExecutionPolicy -RemoteSigned. Run on Exchangeserver
################################################################################################
[CmdletBinding()]
    Param(
        #Specify the (faulty) MB-server
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]                   
        [string]$FaultyMB,
        #Specify if you don't want a logfile
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]                   
        [switch]$NoLog,
         #Specify Path for Log - default is the directory which the script is beeing run from
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]                   
        [string]$LogFilePath,
         #If you only want to Fix 1 user
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]                   
        [string]$MailBoxAliasToFix
 )

if ($LogFilePath) 
{
    if ($LogFilePath.EndsWith("\"))
    {
        $FullLogFilePath = $LogFilePath+"set-AllmsExchHomeServer.log"
    }
    else
    {
        $FullLogFilePath = $LogFilePath+"\set-AllmsExchHomeServer.log"
    }
}
else
{
    $FullLogFilePath = $PSScriptRoot+"\set-AllmsExchHomeServer.log"
}



### Functions
function Write-Log ($Message)
{
    Add-Content -Value "[$(Get-Date -Format ("yyyy-MM-dd hh:mm:ss"))] $Message" -Path $FullLogFilePath
}
function get-Confirmation ($Message="Are you sure you want to continue?") 
{
    $title = "Proceed?" 
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Yes"
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No"
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

    $result = $host.ui.PromptForChoice($title, $Message, $options, 0) 

    switch ($result)
        {
            0 {return $true}
            1 {return $false}
        }

}

### MainScript
if (!(Get-PSSession |  Select -ExpandProperty ConfigurationName) -eq "Microsoft.Exchange")
{
    $MailServer = read-host "Please Provide a MailServer to connect to for PowerShell"
    $ConnectionUri = "http://$MailServer/PowerShell"
    try
    {
        $Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri $ConnectionUri -Authentication Kerberos
        Import-PSSession -Session $Session
    }
    catch
    {
        Write-Warning "Could not connect to: $ConnectionUri"
        break
    }
}

if ($MailBoxAliasToFix)
{
    try
    {
        $MailBox = Get-Mailbox -Identity $MailBoxAliasToFix 
        $RealMailBoxDBName = Get-MailboxDatabase -Identity $MailBox.DataBase | Select -expandproperty Server
        if (get-Confirmation -Message "Current msExchHomeServerName: $($MailBox.servername), New will be: $RealMailBoxDBName")
        {
            Set-Mailbox $MailBox -Database $MailBox.Database -Confirm:$false -Force
            if (!$NoLog){ Write-Log -Message "$($MailBox.Alias) [OLD]: $($MailBox.servername) [NEW]: $RealMailBoxDBName OK" }
            Write-Host "Done!" -ForegroundColor Green
        }
        else
        {
            Write-Host "Aborting.."
        }
    }
    catch
    {
        $_
    }
}
else
{
    $AllMailBoxes = @(Get-Mailbox -ResultSize Unlimited | ? {$_.ServerName -eq $FaultyMB})
    if ($AllMailBoxes.Count -gt 0)
    {
        if (get-Confirmation -Message "Found $($AllMailBoxes.count) MailBoxes with $FaultyMB as msExchHomeServer, fix all?")
        {

            $i = 0
            foreach ($MailBox in $AllMailBoxes)
            {
                $i++            
                Write-Progress -Activity "Fixing MailBoxes" -Status "$($MailBox.alias)" -PercentComplete ($i / $AllMailBoxes.count*100)
                $RealMailBoxDBName = Get-MailboxDatabase -Identity $MailBox.DataBase | Select -expandproperty Server
                Set-Mailbox $MailBox -Database $MailBox.Database -Confirm:$false -Force
                if (!$NoLog){Write-Log -Message "$($MailBox.Alias) [OLD]: $($MailBox.servername) [NEW]: $RealMailBoxDBName OK"}
            }
            Write-Host "All Done" -ForegroundColor Green
        }
        else
        {
            Write-Host "Aborted. $($AllMailBoxes.count) MailBoxes with $FaultyMB as msExchHomeServer still remains"
        }
    }
    else
    {
        Write-Host "All good" -ForegroundColor Green
    }
}

Comments

You need sign in to comment! Sign up today!