-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGet-ExampleFunction.ps1
149 lines (102 loc) · 3.95 KB
/
Get-ExampleFunction.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function Get-ExampleFunction {
# ALWAYS write comment-based help (see example below)
# https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/about_Comment_Based_Help
<#
.SYNOPSIS
Describe function.
.DESCRIPTION
Describe function in more detail.
.EXAMPLE
Give an example of how to use it.
.EXAMPLE
Give another example of how to use it.
.PARAMETER ComputerName
Describe each parameter.
.PARAMETER AnotherParameter
Describe each parameter.
.INPUTS
System.Object
.OUTPUTS
System.Object
.NOTES
#######################################################################################
Author: State of Oregon, OSCIO, ESO, Cybersecurity Assessment Team
Version: 1.0
#######################################################################################
License: https://unlicense.org/UNLICENSE
#######################################################################################
.LINK
https://github.com/oregon-eso-cyber-assessments
.FUNCTIONALITY
Explain the intended use of the function.
#>
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias('PSComputerName','DNSHostName','CN','Hostname')]
[string[]]
$ComputerName,
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string]
$AnotherParameter = 'DefaultValueOfAnotherParameter'
) #param
#
# Terminate parenthesis, brackets, and braces with a meaningful comment.
# It makes your code more readable and helps editors with code folding
#
begin {
#
# Set up to do the stuff
# Maybe set-up some static global variables
#
if (-not $PSBoundParameters.ContainsKey('ComputerName')) {
$ComputerName = $env:COMPUTERNAME
} #if
# Define local helper functions
function Get-EpochTimeStamp {
$Epoch = Get-Date -Date '01/01/1970'
$TimeNow = Get-Date
$TimeNowUtc = [system.timezoneinfo]::ConvertTime($TimeNow,[system.timezoneinfo]::UTC)
$EpochSpan = New-TimeSpan -Start $Epoch -End $TimeNowUtc
$EpochTimeStamp = [math]::Round($EpochSpan.TotalSeconds).ToString()
$EpochTimeStamp
} #function Get-EpochTimeStamp
# Package local functions to pass to remote sessions
$GetEpochTimeStampFunction = "function Get-EpochTimeStamp { ${function:Get-EpochTimeStamp} }"
$StartTime = Get-EpochTimeStamp
Write-Verbose -Message "$StartTime"
} #begin
process {
#
# Do the stuff
#
$ComputerName | ForEach-Object {
$EachComputer = $_
Write-Verbose -Message "$EachComputer"
if ($EachComputer -match $env:COMPUTERNAME) {
# Running locally
Get-EpochTimeStamp
} else {
# Running remotely
Invoke-Command -ComputerName $EachComputer -ScriptBlock {
param (
$NewAudioNotificationFunction,
$AnotherParameter
) #param
# Defining the function in the remote session that was
# passed in by the $GetEpochTimeStampFunction parameter.
. ([ScriptBlock]::Create($GetEpochTimeStampFunction))
Get-EpochTimeStamp
Write-Verbose -Message "$AnotherParameter"
} -ArgumentList $GetEpochTimeStampFunction,$AnotherParameter
} #if
} #ForEach-Object
} #process
end {
#
# Clean up from doing stuff
#
$EndTime = Get-EpochTimeStamp
Write-Verbose -Message "$EndTime"
} #end
} #function Get-ExampleFunction