This article is the fourth one in my series of Sitecore Powershell Script Examples.
You can get all my posts on Sitecore Powershell Extensions Example Scripts here.
Sitecore Powershell Extensions a.k.a SPE is one of my favorite modules for Sitecore. You can automate/build tools for most tasks you face as a Sitecore Developer. You can read more about SPE here.
The scripts that I have posted below can be pasted and directly executed on the Powershell ISE provided by SPE.
Please modify the variables as per your requirement before executing them.
$LogsPath = Join-Path -Path $SitecoreDataFolder -ChildPath "ConsoleLog.txt"
# Your entire code block should be inside the $() below
# The Write-Host commands you use inside this code block will be written to a file
# The name of the file will be Consolelog.txt as per this example
# It can be changed as required in the first line above
$(
# Write-Host logs as below can be used inside the code block to print to the file
Write-Host "Processing Item $($Item.Name), Item ID : $($Item.ID)"
Write-Host "Old Value: $($Item["FieldName"])"
) *>&1 > "$LogsPath"
# An alert to download the log file will be displayed with the below command
Send-File -Path $LogsPath
# As the file is of no longer use for us, it will be deleted with the below command
Remove-Item $LogsPath
You can also use the Write-Log command to write your logs to the SPE log file directly.
With this command, you can have your logs stored directly on the server.
You can read more about the Write-Log command here.
Write-Log "Logging to SPE logs"
Thanks to Michael West for suggesting the Write-Log command. :)
Please do let me know in the comments if you feel a particular use case is missing and you need it in the article.
Feedback and Suggestions are welcome!
Happy Sitecoring!!