|
| 1 | +Function TestObjectIsStringGUID(ByRef boolResult, ByVal objToTest) |
| 2 | + 'region FunctionMetadata ####################################################### |
| 3 | + ' Safely determines if the specified object is a string representation of a |
| 4 | + ' globally-unique identifier (GUID) |
| 5 | + ' |
| 6 | + ' This function takes two positional arguments: |
| 7 | + ' If the test was successful, the first argument (boolResult) contains a boolean |
| 8 | + ' value: True when the specified object is a string representation of a GUID, |
| 9 | + ' False otherwise |
| 10 | + ' |
| 11 | + ' The second argument (objToTest) is the object to be tested to determine if it is |
| 12 | + ' a string representation of a GUID |
| 13 | + ' |
| 14 | + ' The function returns 0 if the test was successful, a negative integer otherwise. |
| 15 | + ' |
| 16 | + ' Example 1: |
| 17 | + ' objToTest = "8589D070-619D-41FB-9D93-D6F50F6AD99D" |
| 18 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 19 | + ' If intReturnCode = 0 Then |
| 20 | + ' ' boolResult is equal to True |
| 21 | + ' End If |
| 22 | + ' |
| 23 | + ' Example 2: |
| 24 | + ' objToTest = "{8589D070-619D-41FB-9D93-D6F50F6AD99D}" |
| 25 | + ' boolResult = TestObjectIsStringGUID(objToTest) |
| 26 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 27 | + ' If intReturnCode = 0 Then |
| 28 | + ' ' boolResult is equal to True |
| 29 | + ' End If |
| 30 | + ' |
| 31 | + ' Example 3: |
| 32 | + ' objToTest = "(8589D070-619D-41FB-9D93-D6F50F6AD99D)" |
| 33 | + ' boolResult = TestObjectIsStringGUID(objToTest) |
| 34 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 35 | + ' If intReturnCode = 0 Then |
| 36 | + ' ' boolResult is equal to True |
| 37 | + ' End If |
| 38 | + ' |
| 39 | + ' Example 4: |
| 40 | + ' objToTest = "{8589D070-619D-41FB-9D93-D6F50F6AD99D)" |
| 41 | + ' boolResult = TestObjectIsStringGUID(objToTest) |
| 42 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 43 | + ' If intReturnCode = 0 Then |
| 44 | + ' ' boolResult is equal to False (mismatched brackets around GUID) |
| 45 | + ' End If |
| 46 | + ' |
| 47 | + ' Example 5: |
| 48 | + ' objToTest = "8589D070619D41FB9D93D6F50F6AD99D" |
| 49 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 50 | + ' If intReturnCode = 0 Then |
| 51 | + ' ' boolResult is equal to True |
| 52 | + ' End If |
| 53 | + ' |
| 54 | + ' Example 6: |
| 55 | + ' objToTest = "8589D070619D41FB9D93D6F50F6AD99DE" |
| 56 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 57 | + ' If intReturnCode = 0 Then |
| 58 | + ' ' boolResult is equal to False (too many characters) |
| 59 | + ' End If |
| 60 | + ' |
| 61 | + ' Example 7: |
| 62 | + ' objToTest = "8589D070619D41FB9D93G6F50F6AD99D" |
| 63 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 64 | + ' If intReturnCode = 0 Then |
| 65 | + ' ' boolResult is equal to False (invalid character) |
| 66 | + ' End If |
| 67 | + ' |
| 68 | + ' Example 8: |
| 69 | + ' objToTest = "a4a43a35-d139-480d-a9fd-6f032257c7aa" |
| 70 | + ' intReturnCode = TestObjectIsStringGUID(boolResult, objToTest) |
| 71 | + ' If intReturnCode = 0 Then |
| 72 | + ' ' boolResult is equal to True (lowercase letters are valid) |
| 73 | + ' End If |
| 74 | + ' |
| 75 | + ' Version: 1.0.20230423.0 |
| 76 | + 'endregion FunctionMetadata ####################################################### |
| 77 | + |
| 78 | + 'region License ################################################################ |
| 79 | + ' Copyright 2023 Frank Lesniak |
| 80 | + ' |
| 81 | + ' Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 82 | + ' this software and associated documentation files (the "Software"), to deal in the |
| 83 | + ' Software without restriction, including without limitation the rights to use, |
| 84 | + ' copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 85 | + ' Software, and to permit persons to whom the Software is furnished to do so, |
| 86 | + ' subject to the following conditions: |
| 87 | + ' |
| 88 | + ' The above copyright notice and this permission notice shall be included in all |
| 89 | + ' copies or substantial portions of the Software. |
| 90 | + ' |
| 91 | + ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 92 | + ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 93 | + ' FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 94 | + ' COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 95 | + ' AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 96 | + ' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 97 | + 'endregion License ################################################################ |
| 98 | + |
| 99 | + 'region DownloadLocationNotice ################################################# |
| 100 | + ' The most up-to-date version of this script can be found on the author's GitHub |
| 101 | + ' repository at https://github.com/franklesniak/sysadmin-accelerator |
| 102 | + 'endregion DownloadLocationNotice ################################################# |
| 103 | + |
| 104 | + 'region DependsOn ############################################################## |
| 105 | + ' TestObjectIsStringContainingData() |
| 106 | + 'endregion DependsOn ############################################################## |
| 107 | + |
| 108 | + Dim intFunctionReturn |
| 109 | + Dim objRegExp |
| 110 | + Dim boolTestResult |
| 111 | + Dim boolTestResultToReturn |
| 112 | + |
| 113 | + intFunctionReturn = 0 |
| 114 | + |
| 115 | + If TestObjectIsStringContainingData(objToTest) = False Then |
| 116 | + intFunctionReturn = -1 |
| 117 | + Else |
| 118 | + ' objToTest is a string containing data |
| 119 | + On Error Resume Next |
| 120 | + Set objRegExp = New RegExp |
| 121 | + If Err Then |
| 122 | + ' RegExp object could not be created |
| 123 | + Err.Clear |
| 124 | + On Error Goto 0 |
| 125 | + intFunctionReturn = -2 |
| 126 | + Else |
| 127 | + ' RegExp object was created successfully |
| 128 | + ' Check to see if the string is a GUID wrapped in curly braces |
| 129 | + objRegExp.Pattern = "^{[A-Fa-f0-9]{8}[-]?(?:[A-Fa-f0-9]{4}[-]?){3}[A-Fa-f0-9]{12}}$" |
| 130 | + boolTestResult = objRegExp.Test(objToTest) |
| 131 | + If Err Then |
| 132 | + ' RegExp object failed to test objToTest |
| 133 | + Err.Clear |
| 134 | + On Error Goto 0 |
| 135 | + intFunctionReturn = -3 |
| 136 | + Else |
| 137 | + ' RegExp object was used to test objToTest successfully |
| 138 | + On Error Goto 0 |
| 139 | + If boolTestResult = True Then |
| 140 | + ' objToTest is a string representation of a GUID |
| 141 | + boolTestResultToReturn = True |
| 142 | + Else |
| 143 | + ' String was not a GUID wrapped in curly braces |
| 144 | + ' Check to see if string is a GUID wrapped in parentheses |
| 145 | + On Error Resume Next |
| 146 | + objRegExp.Pattern = "^\([A-Fa-f0-9]{8}[-]?(?:[A-Fa-f0-9]{4}[-]?){3}[A-Fa-f0-9]{12}\)$" |
| 147 | + boolTestResult = objRegExp.Test(objToTest) |
| 148 | + If Err Then |
| 149 | + ' RegExp object failed to test objToTest |
| 150 | + Err.Clear |
| 151 | + On Error Goto 0 |
| 152 | + intFunctionReturn = -4 |
| 153 | + Else |
| 154 | + ' RegExp object was used to test objToTest successfully |
| 155 | + On Error Goto 0 |
| 156 | + If boolTestResult = True Then |
| 157 | + ' objToTest is a string representation of a GUID |
| 158 | + boolTestResultToReturn = True |
| 159 | + Else |
| 160 | + ' String was not a GUID wrapped in curly braces or |
| 161 | + ' parentheses |
| 162 | + ' Check to see if string is a GUID without any |
| 163 | + ' wrapping |
| 164 | + On Error Resume Next |
| 165 | + objRegExp.Pattern = "^[A-Fa-f0-9]{8}[-]?(?:[A-Fa-f0-9]{4}[-]?){3}[A-Fa-f0-9]{12}$" |
| 166 | + boolTestResult = objRegExp.Test(objToTest) |
| 167 | + If Err Then |
| 168 | + ' RegExp object failed to test objToTest |
| 169 | + Err.Clear |
| 170 | + On Error Goto 0 |
| 171 | + intFunctionReturn = -5 |
| 172 | + Else |
| 173 | + ' RegExp object was used to test objToTest successfully |
| 174 | + On Error Goto 0 |
| 175 | + If boolTestResult = True Then |
| 176 | + ' objToTest is a string representation of a GUID |
| 177 | + boolTestResultToReturn = True |
| 178 | + Else |
| 179 | + ' String was not a GUID wrapped in curly braces, |
| 180 | + ' parentheses, or without any wrapping |
| 181 | + boolTestResultToReturn = False |
| 182 | + End If |
| 183 | + End If |
| 184 | + End If |
| 185 | + End If |
| 186 | + End If |
| 187 | + End If |
| 188 | + End If |
| 189 | + End If |
| 190 | + |
| 191 | + If intFunctionReturn = 0 Then |
| 192 | + boolResult = boolTestResultToReturn |
| 193 | + End If |
| 194 | + |
| 195 | + TestObjectIsStringGUID = intFunctionReturn |
| 196 | +End Function |
0 commit comments