About Me

SharePoint Architect with over 17 years of IT Experience in various roles as SharePoint Architect, Administrator, Technical Architect, IT Analyst, Application Developer, CRM Consultant, BI Developer, Microsoft Solution Architect in Client/Server, Web and Enterprise CRM Applications in E-Commerce, Financial, Healthcare, Insurance, Telecom, Outsourcing and Technology Services.
MCITP (Pro): Microsoft Certified IT Professional: SharePoint 2010 Administrator.
MCTS: Microsoft Certified Technology Specialist, SharePoint 2010 Configuration.

Tuesday, October 25, 2011

Run PowerShell Script from Task Scheduler

If you schedule the following script with appropriate changes for Log file paths, emails, mailservers, will monitor SharePoint Logging Levels and sends an email if verbose is enabled in environment.

Set your default logging levels to monitor.
RunMonitorSPDiagLogging.bat

powershell.exe -command C:\PowerShell\MonitorSPDiagLogging\MonitorSPDiagLogging.ps1

MonitorSPDiagLogging.ps1

 
Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy Unrestricted
#
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
#
$SetTraceSeverity="Unexpected"
$SetEventSeverity="Error"
#
$blnVerbose = "False"
#
$destfile = "C:\PowerShell\MonitorSPDiagLogging\MonitorSPDiagLoggingOut.log"
#
$SPDiagLog = Get-SPLogLevel | Select Area, Name, TraceSeverity, EventSeverity
#Get-SPLogLevel | Select Area, Name, TraceSeverity, DefaultTraceSeverity, EventSeverity, DefaultEventSeverity
foreach ( $obj in $SPDiagLog )
{
#$strLine=""
#$strLine = [string]::join($obj.Name, $obj.Area)
#$strLine = [string]$obj.Name + [string]$obj.Area
#Write-Host $obj.TraceSeverity $obj.DefaultTraceSeverity
#

if(($obj.TraceSeverity -eq "Verbose") -or ($obj.EventSeverity -eq "Verbose") -or ($obj.EventSeverity -eq "Information"))
{
      if($obj.TraceSeverity -ne "VerboseEx")
      {
            #"verboseEx-False So Verbose=TRUE"
            $blnVerbose = "True"
           
      }
}

#
#$strLine5 = $strLine + $blnTrace + $blnEvent;
#$strLine5
}
#
if($blnVerbose -eq "True")
{
      "Processing email... - one of service is in stopped mode.. "
      $emailFrom = "SharePointSupport@unisys.com"
      $emailTo = "User@Domain.com,User2@Domain"
      #$emailTo = "User@Domain.com"
      $subject = "Alert: Verify Diagnostic Logging Levels:" + $env:computername

      $body = @"
      Alert: Verify Diagnostic Logging Levels in $env:computername
      -------------------------------------------------------------

      Found verbose enabled for Trace or Verbose/Information Enabled for Event Logging
      for some of objects in Diagnostic Logging.

      Set logging to default using following command.

      Use (PowerShell)
      Set-SPLogLevel -TraceSeverity Unexpected -EventSeverity Error

      Don't use (Stsadm)
      stsadm -o setlogginglevel -tracelevel Unexpected -windowslogginglevel Error)

      "@
      $smtpServer = "mailrelay.na.uis.yourcorp.com"
      $smtp = new-object Net.Mail.SmtpClient($smtpServer)
      $smtp.Send($emailFrom, $emailTo, $subject, $body)
      #$smtp.Dispose()
      $strTimestamp =  [string](Get-Date -format "yyyy-MM-dd_hh-mm-ss")
      $strTimestamp
      $strTimestamp + " : Email Processed" | Out-File "MonitorSPDiagLogging-ps1-Error.log" -append
      #$host.SetShouldExit(5000)
}

No comments:

Post a Comment