-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPowerShellTemplate.ps1
More file actions
101 lines (98 loc) · 3.94 KB
/
PowerShellTemplate.ps1
File metadata and controls
101 lines (98 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#
.SYNOPSIS
<Overview of script>
.DESCRIPTION
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None>
.NOTES
Version: 1.0
Author: <Name>
Creation Date: <Date>
Purpose/Change: Initial script development
.EXAMPLE
<Example explanation goes here>
<Example goes here. Repeat this attribute for more than one example>
#>
#---------------------------------------------------------[Script Parameters]------------------------------------------------------
[CmdletBinding()]
Param (
#Script parameters go here
[Parameter(mandatory=$false)] [switch]$UniqLog
,[Parameter(mandatory=$false)] [string]$LogDirectory
,[Parameter(mandatory=$false)] [switch]$NoLog
)
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
#Import Modules & Snap-ins
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Any Global Declarations go here
$scriptName = [IO.Path]::GetFileNameWithoutExtension($($MyInvocation.MyCommand.Name))
if($PSBoundParameters.ContainsKey('LogDirectory')){
Write-Verbose "Log Directory Passed: $LogDirectory"
if ($False -eq $(test-path -PathType Container -path $LogDirectory)){
Write-Verbose "Can't Find Passed Log Directory. Attempting To Create Log Directory"
try {
New-Item -ItemType Directory -Force -Path $LogDirectory
}
catch {
$Exception = $error[0].Exception; $PositionMessage = $error[0].InvocationInfo.PositionMessage ;$ScriptStackTrace = $error[0].ScriptStackTrace
Write-Error "$Exception - $PositionMessage - $ScriptStackTrace"
}
}
}Else{
$LogDirectory = $PWD
}
#Create a unique log file
if($UniqLog){
Write-Verbose "Unique Log switch passed"
$ScriptLog= "$LogDirectory\$ScriptName`_$(Get-Date -format 'yyyyMMddHHmmss').log"
}else {
$ScriptLog= "$LogDirectory\$ScriptName.log"
}
#-----------------------------------------------------------[Functions]------------------------------------------------------------
function Script_Information {
param ()
Write-Verbose "Starting Function: $($MyInvocation.MyCommand)"
Write-Verbose "$(Get-date)`nScript: $PSCommandPath`nScript Root: $($MyInvocation.PSScriptRoot)`nComputer Name: $($env:COMPUTERNAME)`nPowerShell Version: $($PSVersionTable.PSVersion)`nPresent Working Directory: $PWD"
if($False -eq $NoLog){
Write-Verbose "ScriptLog: $ScriptLog"
}
}
#-----------------------------------------------------------[Main]------------------------------------------------------------
Function Main {
Param ()
Begin {
Write-Verbose "Starting Function: $($MyInvocation.MyCommand)"
$StopWatch = [System.diagnostics.stopwatch]::StartNew()
Script_Information
}
Process {
Try {
#code here
}
Catch {
Write-Verbose "Outside catch"
$Exception = $error[0].Exception; $PositionMessage = $error[0].InvocationInfo.PositionMessage ;$ScriptStackTrace = $error[0].ScriptStackTrace
Write-Error "$Exception - $PositionMessage - $ScriptStackTrace"
}
}
End {
If ($?) {
$StopWatch.Stop()
Write-Verbose "Complated Successfully. Elapsed Seconds $($StopWatch.Elapsed.TotalSeconds)"
}
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
#Call main
if($NoLog){
Write-Verbose "No log file switch passed."
Main *>&1
}Else{
Main *>&1 | Tee-Object $ScriptLog
}