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
|
Function Split-ADGroupName{
param(
[string] $InputObject,
[string] $Prefixe,
[string] $Suffixe
)
#Echappe les métacaractéres d'une chaîne considérée comme ancre dand une regex
$EscapedPrefix=[Regex]::Escape($Prefixe)
$EscapedSuffix=[Regex]::Escape($Suffixe)
#Partie recherchée
$Pattern='(?<GroupName>.{3})'
#Construit la regex
$RegEx='{0}{1}{2}' -F $EscapedPrefix,$Pattern,$EscapedSuffix
if ($InputObject -match $RegEx)
{
Write-Verbose "Split-ADGroupName : '$InputObject' match '$Regex'"
$Matches.GroupName
}
else { Write-Verbose "Split-ADGroupName : '$InputObject' not match '$Regex'"}
}#Split-ADGroupName
$VerbosePreference='Continue'
$s='toto_123_titi'
Split-ADGroupName $S
Split-ADGroupName $S -Prefixe 'toto_' -Suffixe '_titi'
$s='toto_12_titi'
Split-ADGroupName $S -Prefixe 'toto_' -Suffixe '_titi'
$s='toto'
Split-ADGroupName $S -Prefixe 'toto_' -Suffixe '_titi'
$s='toto.123.titi'
Split-ADGroupName $S -Prefixe 'toto.' -Suffixe '.titi'
$VerbosePreference='SilentlyContinue' |
Partager