Snippet Name: Template
Tags: PowerShell
Created Date: 2020-06-09 15:29:04
Last Modified Date: 2020-06-09 15:29:04
— Template
Dependencies:
DBA Notes:
Sample Output:
^###################################################################################################
^## Purpose:
^###################################################################################################
Clear-Host
$Error.Clear()
^#region Prerequisites
^###################################################################################################
^## Prerequisites (Load Modules and installs)
^###################################################################################################
^#endregion
^#region Load References
^###################################################################################################
^##Load References
^###################################################################################################
^#endregion
^#region Local Functions
^###################################################################################################
^## Local Functions
^###################################################################################################
function die
{
Write-Error -Message "Error: $($args[0])"
exit 1
}
function verifySuccess
{
if (!$?)
{
die "$($args[0])"
}
}
^#endregion
^#region Variables
^###################################################################################################
^## Variables
^###################################################################################################
^# Define SQL Server instance and UNC path to backup files
$SqlInstance = "localhost" # Change if needed
$BackupRoot = "\\10.0.0.62\SQL_Share\stormserver\StormServer"
$ConnectionString = "Server=$SqlInstance;Integrated Security=True;TrustServerCertificate=True"
^#endregion
^#region Automatic Logging - Start
^###################################################################################################
^## Automatic Logging
^###################################################################################################
$LogFolder = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
$JustTheFileName = [System.IO.Path]::GetFileName($MyInvocation.MyCommand.Path)
$LogFile = [System.IO.Path]::Combine($LogFolder,$JustTheFileName.Replace('.ps1','') +'_'+ (Get-Date).ToString('yyyy-MM-dd') + '.txt')
try
{
Start-Transcript -Path $LogFile -ErrorAction SilentlyContinue
}
catch
{
Stop-Transcript
Start-Transcript -Path $LogFile
}
$StartTime = Get-Date
^#endregion Automatic Logging
^#region Houskeeping
^###################################################################################################
^## Housekeeping
^###################################################################################################
^# Load SQL Server SMO
Import-Module SqlServer
^#endregion Housekeeping
^#region The Work
^###################################################################################################
^## The Work
^###################################################################################################
^#endregion The Work
^#region End Of Logging
^###################################################################################################
^## End Of Logging The Work tail of all Work: review log
^###################################################################################################
^##
$EndTime = (Get-Date)
$ts = New-TimeSpan -Start $StartTime -End $EndTime
$MyElapsedTime = 'Elapsed Time hh:mm:ss:: ' + ($ts.Hours).ToString('00') + ':'+ ($ts.Minutes).ToString('00') + ':' + ($ts.Seconds).ToString('00')
Write-Host -Object $MyElapsedTime -ForegroundColor Yellow
Stop-Transcript
Invoke-Item $LogFile
^#endregion End Of Logging
^#region TheOutput
^###################################################################################################
^## The Output
^###################################################################################################
^#endregion
verifySuccess $MyInvocation.MyCommand.Name + ' Failed'