Monday, September 8, 2014

SharePoint 2013 - Powershell deployment simple log / logging function

Function:

$dp0 =  [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)

Function write-log($date,$EventType,$EventMesage,$functionName,$Filename)
{  
    $dp0 = $(get-location)
    $date = get-date
    $index= "$dp0".LastIndexOf("\")
    $logloc="$dp0".Substring(0,$index)
   
    $EventMesages = $EventMesage -replace ",", "."

     $LogFilePath = "$dp0\ScriptExecLogs.csv"
   # Write-Host $LogFilePath
    $EventMesages = $EventMesage -replace ",", "."
   
    if($EventType -eq $null){
        $EventType="Event type not Specified"
    }
    if($EventMesage -eq $null){
        $EventMessage = "NA"
    }
    if($Filename -eq $null){
        $Filename = "No File Provided"
    }
    if($functionName -eq $null){
        $functionName = "Function name not provided"
    }
   
   
   
    Add-content -Path  $LogFilePath -value  "$date,$EventType,$EventMesages,$functionName,$Filename"
}

Function call:

1.write-log $date "message"  "PowerShell Snapin added successfully"   "GlobalCall" "SiteColumn"


2.catch
        {
             write-log $date "error" $_.Exception.Message ("on"+$_.Name+": field creation") "createContentType" "SiteContentType"
        }

No comments:

Post a Comment