PowerShell: Simple Logging helper

Scenario: How to log all my message with different color on the console and with category into CSV files


Code Snippet

$global:errorCount = 0
$logFolder = "$root\Logs"

function EnsureLogFolder() {
	$command = $MyInvocation.MyCommand.Name
	if(!(Test-Path $logFolder)) {		
		$newFolder = New-Item -ItemType Directory -Path $logFolder -Force
		LogInfo "[$command]: Log folder does not exist, provisioning folder $logFolder..."
	}
}

function LogMessage($msg, $category) {	
	$logFile = "$logFolder\log.csv"
	$mailFile = "$logFolder\mail.csv"

	$Properties = @{		
		"Category" = $category
		Date = (Get-Date)		
		Message = $msg
	}
	
	switch($category) {
		"INFO" { Write-Host $msg }
		"DEBUG" { Write-Host "================ $msg ================" -ForegroundColor Magenta  }
		"SUCCESS" { Write-Host $msg -ForegroundColor Green }
		"WARNING" { Write-Host $msg -ForegroundColor Yellow }
		"ERROR" { Write-Host $msg -ForegroundColor Red }
		"MAIL" { }
	}

	$obj = New-Object psobject -Property $Properties
	if($category -ne "MAIL") {		
		$obj | Select Date, Category, Message | Export-Csv $logFile -NoTypeInformation -Append
	}
	else {
		$obj | Select Date, Message | Export-Csv $mailFile -NoTypeInformation -Append
	}
}

function LogEmail($msg) {
	LogMessage $msg "MAIL"
}

function LogDebug($msg) {
	LogMessage $msg "DEBUG"
}

function LogInfo($msg) {	
	LogMessage $msg "INFO"
}

function LogSuccess($msg) {
	LogMessage $msg "SUCCESS"
}

function LogWarning($msg) {
	LogMessage $msg "WARNING"
}

function LogError($msg) {
	LogMessage $msg "ERROR"
	$global:errorCount++
}
Advertisements

SharePoint Online (Office PnP): Populate default fields in View dynamically

Scenarios: How to add all fields into view dynamically


Code Snippet

Function AddDefaultFieldsToView {
	Param (
		[Parameter(Mandatory=$True)] 
        [string]$ContentTypeName,
		[Parameter(Mandatory=$True)] 
        [string]$ViewName,
		[Parameter(Mandatory=$True)] 
        [string]$ListName
	)

	$view = Get-PnPView -Identity $ViewName -List $ListName
	if($view -eq $null) {
		Write-Host "View $ViewName does not exist in $ListName"
		return
	}

	$ct = Get-PnPContentType -InSiteHierarchy -Identity $ContentTypeName	
	$columns = Get-PnPProperty -ClientObject $ct -Property "Fields"

	Write-Host "Applying default columns to View: '$ViewName' from ContentType: '$ContentTypeName'"
    foreach ($column in $columns | Where-Object { !$_.Hidden } ) {        
		if($view.ViewFields -contains $column.InternalName){
			Write-Host ("'" + $column.Title + "' already exist in View")
			continue
		}

        if ($column.Title -match "Modified By" -or
			$column.Title -match "Created By" -or
			$column.Title -eq "Name" -or
			$column.Title -eq "Content Type" -or
			$column.Title -eq "Title"
		) {
			#system fields				
            continue
        }
		
        $view.ViewFields.Add($column.Title)        
		Write-Host ("Added field '" + $column.Title + "' to '" + $ViewName + "'")
    }

	$view.Update()
	Execute-PnPQuery
	Write-Host "Applied default columns"
}

Entity Framework: Dealing with views with no primary key

In entity framework there was a known issues or views with no primary will returns all the duplicated rows, because it couldn’t identity what’s different in the rows without a primary key.

There is a quick code fix we can apply to that,

Instead of using context.viewTest.toList(), we use the following:

In EF 5:

context.Database.SQLQuery<viewTest>(“Select * from viewTest”);

In EF 4:

context.ExecuteStoreQuery<viewTest>(“Select * from viewTest”);