-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowershell_Script_Template.ps1
81 lines (63 loc) · 2.88 KB
/
Powershell_Script_Template.ps1
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
#Requires -Version 2.0
<#
.SYNOPSIS
Synopsis for the script
.DESCRIPTION
Description for the script
.LINK
Project home: https://script.project.com
.NOTES
Author:
Version: 1.0.0
This script is designed to be called from PowerShell.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $false, HelpMessage = "Whether should show prompt for errors")]
[Switch]$PromptOnError = $false
)
# Turn on Strict Mode to help catch syntax-related errors.
# This must come after a script's/function's param section.
# Forces a Function to be the first non-comment code to appear in a PowerShell Module.
Set-StrictMode -Version Latest
#==========================================================
# Define any necessary global variables, such as file paths.
#==========================================================
# Gets the script file name, without extension.
$THIS_SCRIPT_NAME = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Definition)
# Get the directory that this script is in.
$THIS_SCRIPT_DIRECTORY_PATH = Split-Path $script:MyInvocation.MyCommand.Path
#==========================================================
# Define functions used by the script.
#==========================================================
# Catch any exceptions Thrown, display the error message, wait for input if appropriate, and then stop the script.
Trap [Exception] {
$errorMessage = $_
Write-Host "An error occurred while running $($THIS_SCRIPT_NAME) script:`n$errorMessage`n" -Foreground Red
If ($PromptOnError) {
Write-Host "Press any key to continue ..."
$userInput = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
}
Break;
}
# PowerShell v2.0 compatible version of [String]::IsNullOrWhitespace.
Function Test-StringIsNullOrWhitespace([String]$string) {
Return [String]::IsNullOrWhiteSpace($string)
}
#==========================================================
# Perform the script tasks.
#==========================================================
# Display the time that this script started running.
$scriptStartTime = Get-Date
Write-Verbose "$($THIS_SCRIPT_NAME) script started running at $($scriptStartTime.TimeOfDay.ToString())."
# Display the version of PowerShell being used to run the script, as this can help solve some problems that are hard to reproduce on other machines.
Write-Verbose "Using PowerShell Version: $($PSVersionTable.PSVersion.ToString())."
Try {
# SCRIPT ACTIONS
} Finally {
Write-Verbose "Performing any required $($THIS_SCRIPT_NAME) script cleanup..."
}
# Display the time that this script finished running, and how long it took to run.
$scriptFinishTime = Get-Date
$scriptElapsedTimeInSeconds = ($scriptFinishTime - $scriptStartTime).TotalSeconds.ToString()
Write-Verbose "$($THIS_SCRIPT_NAME) script finished running at $($scriptFinishTime.TimeOfDay.ToString()). Completed in $scriptElapsedTimeInSeconds seconds."