Saturday, May 12, 2012

SharePoint Incoming Email Configuration - PowerShell

PowerShell is a strong windows shell scripting, which is widely used in SharePoint 2010. Here I am sharing small and useful scripts we use for SharePoint Farm administration.

Requirement:
Configure Incoming Email settings in SharePoint farm.


Script:

# *******************************************************************
## Function - Configure Incoming Email settings in SharePoint farm
## Author - Deepak Solanki
## Checks to ensure that Microsoft.SharePoint.Powershell is loaded, if not, adding pssnapin
## Configure Incoming Email settings in SharePoint farm
# ********************************************************************
#Add SharePoint Add-ins
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue 
# if snapin is not installed then use this method
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


Write-Host "Script started to Configure Incoming Email." 


  #Variables to get config values    
 [boolean]$Enabled = $true;
 [boolean]$UseAutomaticSettings = $false;
 [boolean]$UseDirectoryManagementService = $true;
[boolean]$RemoteDirectoryManagementService = $false;
$ServerAddress = "EXCHANGE.DOMAIN.COM";
[boolean]$DLsRequireAuthenticatedSenders = $true;
[boolean]$DistributionGroupsEnabled = $true;
$ServerDisplayAddress = "sharepoint.company.com";
$DropFolder = "c:\inetpub\mailroot\drop";

#Test the drop folder location exists before proceeding with any changes
$dropLocationTest = Get-Item $DropFolder -ErrorAction SilentlyContinue
if ($dropLocationTest -eq $null)
{
Throw "The drop folder location $DropFolder does not exist - please create the path and try the script again."
}

    #Configuring Incoming E-mail Settings
    try
    {
$type = "Microsoft SharePoint Foundation Incoming E-Mail"
$svcinstance = Get-SPServiceInstance | where { $_.TypeName -eq $type}
$inmail = $svcinstance.Service


if ($inmail -ne $null)
        {
Write-Log "Configuring Incoming E-mail Settings."            
#Enable sites on this server to receive e-mail
                $inmail.Enabled = $Enabled
            
#Automatic Settings mode
                $inmail.UseAutomaticSettings = $UseAutomaticSettings
            
#Use the SharePoint Directory Management Service to create distribution groups
                $inmail.UseDirectoryManagementService = $UseDirectoryManagementService
            
#Use remote: Directory Management Service
                $inmail.RemoteDirectoryManagementService = $RemoteDirectoryManagementService
            
#SMTP mail server for incoming mail
                $inmail.ServerAddress = $ServerAddress            

#Accept messages from authenticated users only
                $inmail.DLsRequireAuthenticatedSenders = $DLsRequireAuthenticatedSenders
            
#Allow creation of distribution groups from SharePoint sites
                $inmail.DistributionGroupsEnabled = $DistributionGroupsEnabled
            
#E-mail server display address
                $inmail.ServerDisplayAddress = $ServerDisplayAddress
            
#E-mail drop folder
                $inmail.DropFolder = $DropFolder;  

$inmail.Update();
Write-Host "Incoming E-mail Settings completed."
        } 
    }
    #Report if there is a problem setting Incoming Email
    catch { Write-Host "There was a problem setting Incoming Email: $_" }

SharePoint Diagnostic Configuration - PowerShell

PowerShell is a strong windows shell scripting, which is widely used in SharePoint 2010. Here I am sharing small and useful scripts we use for SharePoint Farm administration.

Requirement:
Setting SharePoint Diagnostic and logging configuration.


Script:

# *******************************************************************
## Function - Configure Diagnostic Setting in SharePoint Farm
## Author - Deepak Solanki
## Checks to ensure that Microsoft.SharePoint.Powershell is loaded, if not, adding pssnapin
## Check log file location exists before proceeding with changes
## Reset all log levels to default
## Change Trace Log settings
# ********************************************************************


#Add SharePoint Add-ins
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue 
# if snapin is not installed then use this method
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


Write-Log "Script started to Configure SharePoint Diagnostic settings."  
#Reset all log levels to default
Write-Host "Resetting all log levels to default."
Clear-SPLogLevel
Write-Host "Reset Completed."


#Declare variable for config values
$LogLocation = "E:\Logs"
$DaysToKeepLogs ="14"
[boolean]$LogMaxDiskSpaceUsageEnabled = [Boolean]::Parse("true")
$LogDiskSpaceUsageGB = "10"

#Test the log file location exists before proceeding with any changes
$logLocationTest = Get-Item $LogLocation -ErrorAction SilentlyContinue
if ($logLocationTest -eq $null)
{
Throw "The log file location $LogLocation does not exist - please create the path and try the script again."
}
#Trace Log settings
Write-Host  "Starting SharePoint Diagnostic configuration."
try
{
    Set-SPDiagnosticConfig -LogLocation $LogLocation `
                        -DaysToKeepLogs $DaysToKeepLogs `
                        -LogMaxDiskSpaceUsageEnabled:$LogMaxDiskSpaceUsageEnabled `
                        -LogDiskSpaceUsageGB $LogDiskSpaceUsageGB
}
#Report if there is a problem stopping the service instance
catch {  Write-Host  "There was a problem in diagnostic setting: $_" }
Write-Host  "Diagnostic settings changed successfully."