Saturday, May 12, 2012

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."


No comments:

Post a Comment