Tuesday, September 27, 2011

Using RegEx to Validate FIM Service GUIDs

Sometimes it is useful to validate a GUID before using it to script against FIM.  I’ve been using the regex pattern below in PowerShell scripts, and parameter validation scripts.

001
002
003
004
005
006
007
008
009
010

###
### Regex pattern to test a UUID from FIM
###

$regExPatternForFimUuid = "^(urn:uuid:){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"

### Test 1 - should return TRUE when GUID is prepended with 'urn:uuid:'
'urn:uuid:fea6a1cc-0ee3-4aa6-aba7-ad339d6cab5f' -match $regExPatternForFimUuid

### Test 2 - should return TRUE when GUID is not prepended with 'urn:uuid:'
'fea6a1cc-0ee3-4aa6-aba7-ad339d6cab5f' -match $regExPatternForFimUuid

No comments: