-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test-ValidSID.ps1
191 lines (170 loc) · 9.57 KB
/
Test-ValidSID.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function Test-ValidSID {
#region FunctionHeader #########################################################
# This function tests whether the supplied arugment is a security identifier (SID).
# If the supplied argument is a SID, the function returns $true. If the supplied
# argument is not a SID, the function returns $false.
#
# One positional argument is required: an object to be evaluated to determine
# whether it is a SID
#
# The function returns $true if the supplied object is a SID; $false otherwise
#
# Example usage:
# $boolResult = Test-ValidSID 'S-1-5-21-1234567890-1234567890-1234567890-1000'
#
# Version: 2.0.20230719.0
#endregion FunctionHeader #########################################################
#region License ################################################################
# Copyright (c) 2023 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion License ################################################################
#region Acknowledgements #######################################################
# Thanks to Friedrich Weinmann who suggested an alternative way to test for a SID:
# https://twitter.com/FredWeinmann/status/1675513443615404032?s=20
# retrieved on 2023-07-19
#endregion Acknowledgements #######################################################
#region FunctionsToSupportErrorHandling ########################################
function Get-ReferenceToLastError {
#region FunctionHeader #####################################################
# Function returns $null if no errors on on the $error stack;
# Otherwise, function returns a reference (memory pointer) to the last error
# that occurred.
#
# Version: 1.0.20230709.0
#endregion FunctionHeader #####################################################
#region License ############################################################
# Copyright (c) 2023 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ############################################################
#region DownloadLocationNotice #############################################
# The most up-to-date version of this script can be found on the author's
# GitHub repository at https://github.com/franklesniak/PowerShell_Resources
#endregion DownloadLocationNotice #############################################
if ($error.Count -gt 0) {
[ref]($error[0])
} else {
$null
}
}
function Test-ErrorOccurred {
#region FunctionHeader #####################################################
# Function accepts two positional arguments:
#
# The first argument is a reference (memory pointer) to the last error that had
# occurred prior to calling the command in question - that is, the command that
# we want to test to see if an error occurred.
#
# The second argument is a reference to the last error that had occurred as-of
# the completion of the command in question
#
# Function returns $true if it appears that an error occurred; $false otherwise
#
# Version: 1.0.20230709.0
#endregion FunctionHeader #####################################################
#region License ############################################################
# Copyright (c) 2023 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ############################################################
#region DownloadLocationNotice #################################################
# The most up-to-date version of this script can be found on the author's GitHub
# repository at https://github.com/franklesniak/PowerShell_Resources
#endregion DownloadLocationNotice #################################################
# TO-DO: Validate input
$boolErrorOccurred = $false
if (($null -ne ($args[0])) -and ($null -ne ($args[1]))) {
# Both not $null
if ((($args[0]).Value) -ne (($args[1]).Value)) {
$boolErrorOccurred = $true
}
} else {
# One is $null, or both are $null
# NOTE: ($args[0]) could be non-null, while ($args[1])
# could be null if $error was cleared; this does not indicate an error.
# So:
# If both are null, no error
# If ($args[0]) is null and ($args[1]) is non-null, error
# If ($args[0]) is non-null and ($args[1]) is null, no error
if (($null -eq ($args[0])) -and ($null -ne ($args[1]))) {
$boolErrorOccurred
}
}
$boolErrorOccurred
}
#endregion FunctionsToSupportErrorHandling ########################################
trap {
# Intentionally left empty to prevent terminating errors from halting
# processing
}
$objSID = $null
# Retrieve the newest error on the stack prior to running the command to determine
# if the object is a SID
$refLastKnownError = Get-ReferenceToLastError
# Store current error preference; we will restore it after we call the command
$actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
# Set ErrorActionPreference to SilentlyContinue; this will suppress error output.
# Terminating errors will not output anything, kick to the empty trap statement and
# then continue on. Likewise, non-terminating errors will also not output anything,
# but they do not kick to the trap statement; they simply continue on.
$global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
# Run the command to determine if the object is a SID
$objSID = ($args[0]) -as [System.Security.Principal.SecurityIdentifier]
# Restore the former error preference
$global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
# Retrieve the newest error on the error stack
$refNewestCurrentError = Get-ReferenceToLastError
if (Test-ErrorOccurred $refLastKnownError $refNewestCurrentError) {
return $false
} else {
return ($null -ne $objSID)
}
}