December 27, 2012

Powershell - Creating log files for installation package

Use case:
It is general practice that we deliver wsp and powershell script to install it.
it is good practice to log whatever we show in screen to file so that it will be easy for sharepoint admin to send the result of installation.

 # Set Execution Policy 
Set-ExecutionPolicy unrestricted
$date     = Get-Date
#read parameters from command line
$CurrentDir= Split-Path -parent $MyInvocation.MyCommand.Definition 
$timeStamp = (Get-Date).ToString("yyyyMMddhhmmss") 
$logFile = $CurrentDir+ "\install_" + $timeStamp + ".log"
start-transcript -path $logFile -noclobber 
#add this at end of script
#this is to show stop-transcript 
Remove-PsSnapin Microsoft.SharePoint.PowerShell    
$CurrentDir will get installation folder. Start-Transcript is powershell command to copy whatever message printed to console to file provided in parameter.
You have to place your installation script between start and stop transcript commands and everytime you execute the script, it will generate a separate log file that will have all details.

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment