Private/Get-RegexMatch.ps1
function Get-RegexMatch { [CmdletBinding()] Param ( [Parameter(Mandatory=$True,Position=0,ParameterSetName='RxString')] [String]$RegexString, [Parameter(Mandatory=$True,Position=0,ParameterSetName='Rx')] [regex]$Regex, [Parameter(Mandatory=$True,Position=1)] [string]$StringToEval, [Parameter(Mandatory=$False)] [string]$ReturnGroupName, [Parameter(Mandatory=$False)] [int]$ReturnGroupNumber, [Parameter(Mandatory=$False)] $VariableToUpdate, [Parameter(Mandatory=$False)] [string]$ObjectProperty, [Parameter(Mandatory=$False)] [string]$LoopName ) $VerbosePrefix = "Get-RegexMatch: " if ($RegexString) { $Regex = [Regex] $RegexString } if ($ReturnGroupName) { $ReturnGroup = $ReturnGroupName } if ($ReturnGroupNumber) { $ReturnGroup = $ReturnGroupNumber } $Match = $Regex.Match($StringToEval) if ($Match.Success) { #Write-Verbose "$VerbosePrefix Matched: $($Match.Value)" if ($ReturnGroup) { #Write-Verbose "$VerbosePrefix ReturnGroup" switch ($ReturnGroup.Gettype().Name) { "Int32" { $ReturnValue = $Match.Groups[$ReturnGroup].Value.Trim() } "String" { $ReturnValue = $Match.Groups["$ReturnGroup"].Value.Trim() } default { Throw "ReturnGroup type invalid" } } if ($VariableToUpdate) { if ($VariableToUpdate.Value.$ObjectProperty) { #Property already set on Variable continue $LoopName } else { $VariableToUpdate.Value.$ObjectProperty = $ReturnValue Write-Verbose "$ObjectProperty`: $ReturnValue" } continue $LoopName } else { return $ReturnValue } } else { return $Match } } else { if ($ObjectToUpdate) { return # No Match } else { return $false } } } |