Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created April 28, 2020 06:33
Show Gist options
  • Save mark05e/d26c0f03956d4783373a4aff8440927b to your computer and use it in GitHub Desktop.
Save mark05e/d26c0f03956d4783373a4aff8440927b to your computer and use it in GitHub Desktop.
My powershell boilerplate
<#
.NOTES
===========================================================================
Author: Mark
Created on: 2020-04-28
Organization: XXXXXX
Filename: MyPowershellBoilerplate.ps1
===========================================================================
.DESCRIPTION
My powershell boilerplate
#CHANGE HISTORY
2020-04-28 : Initial Version
#>
#Requires -Version 5
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
#region Variables
$scriptDir = Split-Path -LiteralPath $PSCommandPath
$startingLoc = Get-Location
Set-Location $scriptDir
$startingDir = [System.Environment]::CurrentDirectory
[System.Environment]::CurrentDirectory = $scriptDir
$trace_path = ".\TraceLogs\"
#endregion
#region Transcripts
# Get Execution time
$startDate = Get-Date -format 'yyyy-MM-dd'
$startTime = Get-Date -format 'HH:mm'
# Start Transcript
$fileToWrite = (Resolve-Path -Path $trace_path).Path + $startDate + '.log'
Start-Transcript -Path $fileToWrite -Append | Out-Null
Write-Output -InputObject "`n########## Starting script $($PSCommandPath) on [$($env:computername)] at $($startDate) $($startTime) ##########"
#endregion
try
{
# TODO: Insert script here.
}
finally
{
Set-Location $startingLoc
[System.Environment]::CurrentDirectory = $startingDir
Write-Output "Done. Elapsed time: $($stopwatch.Elapsed)"
Stop-Transcript
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment