How to filter, split the results of a string?

Welcome Forums General PowerShell Q&A How to filter, split the results of a string?

This topic contains 7 replies, has 6 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #160790

    Participant
    Topics: 1
    Replies: 0
    Points: 11
    Rank: Member

    Howdy folks, I need some help with this script:

    $domains = Get-AcceptedDomain
    
    Get-Content C:\Temp\Rules\MBX.txt|
    ForEach-Object {
    Write-Host "Checking $_ rules..." -ForegroundColor Yellow
    Get-InboxRule -mailbox $_ -ErrorAction SilentlyContinue |
    Where-Object{
    $_.ForwardTo -match "EX:/" -or
    $_.RedirectTo -match "EX:/" -or
    $_.ForwardAsAttachmentTo -match "EX:/" -and
    $domains.DomainName -ne $domain
    }
    } |
    Select MailboxOwnerId,Name,Forwardto,RedirectTo,ForwardAsAttachmentTo |
    Export-CSV C:\temp\Rules\Out_MBX-.csv -NoTypeInformation
    

    The results come in this format:

    MailboxOwnerId Name ForwardTo RedirectTo ForwardAsAttachmentTo
    Homer Simpson Springfield Rule Marge Simpson [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=1111-Marge Simpson "Bart Simpson" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=11112-Bart Simpson] "Lisa Simpsons" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=111113-Lisa Simpsons]

    Is there a way to get the Names in the " " only and comma deliminter if there are more than 1 records?

    So that the result would be:

    ForwardTo
    Marge Simpson; Bart Simpson, Lisa Simpson

    Thanks, M

  • #160835

    Senior Moderator
    Topics: 8
    Replies: 1036
    Points: 3,415
    Helping Hand
    Rank: Community Hero

    not a regexecpert, but you can try this.

    Select MailboxOwnerId,Name,@E={$_.ForwardTo -match '"([a-zA-Z]+)"';$Matches[1]};L='Forwardto'},RedirectTo,ForwardAsAttachmentTo
    
  • #160860

    Participant
    Topics: 2
    Replies: 54
    Points: 276
    Helping Hand
    Rank: Contributor

    A couple of questions that need to be answered before you can apply regex successfully to this:

    1. Will the names you want to find always be in the format "First Last"?
    2. Will the names ever contain punctuation or special characters like hyphens?

    If (1)Yes and (2)No, then you can apply regex matching as kvprasoon recommends, but with the following changes to the regex string:

    "`"[a-zA-Z]+ [a-zA-Z]+`""

    This will match only strings that have the form "Alpha Alpha".
    The backtick (`) escapes the double quotes so that they can be matched as characters and not interpreted by PowerShell. The [a-zA-Z]+ pattern matches any group of upper or lower case letters with at least one character, and it is repeated with a space in the middle so that the match will include the space between the first and last name.

    If you need to account for any non-alpha characters, or the inclusion of middle initials or any variation from the "First Last" format, the regex will need to be either more complex or more general.

  • #161445

    Participant
    Topics: 0
    Replies: 13
    Points: 42
    Helping Hand
    Rank: Member

    lol, Although not a regular approach, but my script does the job ūüėÄ

    $x = '"Marge Simpson" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=1111-Marge Simpson "Bart Simpson" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=11112-Bart Simpson] "Lisa Simpsons" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=111113-Lisa Simpsons]'
    $y = $x|%{$_.split('"')}
    for ($i = 1; $i -lt $y.Length; $i++)
    { 
    $y[$i]
    $i=$i+1
    }
    Marge Simpson
    Bart Simpson
    Lisa Simpsons

     

  • #161448

    Participant
    Topics: 0
    Replies: 13
    Points: 42
    Helping Hand
    Rank: Member

    ūüėÄ

     

    $x = '"Marge Simpson" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=1111-Marge Simpson "Bart Simpson" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=11112-Bart Simpson] "Lisa Simpsons" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=111113-Lisa Simpsons]'
    $y = $x|%{$_.split('"')}
    $z= $null
    for ($i = 1; $i -lt $y.Length; $i++)
    { 
    $z += $y[$i] + ","
    $i=$i+1
    }
    Marge Simpson,Bart Simpson,Lisa Simpsons,
  • #161454
    js

    Participant
    Topics: 24
    Replies: 675
    Points: 1,602
    Helping Hand
    Rank: Community Hero

    Note that in powershell -or and -and have EQUAL precedence, which is very unusual in a programming language.

    PS /Users/js> $true -or $true -and $false  
    False
    PS /Users/js> $true -or ($true -and $false)
    True
    
  • #161537

    Participant
    Topics: 2
    Replies: 54
    Points: 276
    Helping Hand
    Rank: Contributor

    There are a couple things you need to determine in order to do the filtering properly:

    1. Will all names always be in the format "First Last"? (e.g. no middle initials)
    2. Will the names ever contain special characters? (e.g. punctuation, hyphenation)

    If (1) Yes and (2) No then you should apply regex as kvprasoon suggests, but with this change to the regex string:

    "`"[a-zA-Z]+ [a-zA-Z]+`""

    This will match any string that is in the form of two words of only alpha characters containing at least one character and either upper or lower case, separated by a space and surrounded by double quotes, e.g.

    "Alpha Beta"

    Note the backtick (`) placed before the double quotes – it is used to force PowerShell to interpret the double quotes literally as characters to be matched rather than how PowerShell normally parses double quotes.
    This will not match strings that are not in double quotes, or single names/words in double quotes, or any strings containing non-alpha characters. If you need to account for special characters or other formats, the regex will need to be either more generalized or more complex. For instance, if you want to also match names that may or may not have middle initials the regex would look like:

    "`"[a-zA-Z]+ [a-zA-Z]. ?[a-zA-Z]+`""

    This will match strings that look like

    "Alpha Beta"

    or

    "Alpha B. Gamma"

    Specifically, the regex matches a double quote mark followed by one or more alpha characters of either case, followed by a space, maybe followed by a single alpha character, a period and a space (or maybe not), followed by one or more alpha characters of either case, followed by a double quote mark.

  • #161648

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    You mean this...

    $UserMailData = @'
    MailboxOwnerId  Name  ForwardTo  RedirectTo  ForwardAsAttachmentTo  
    Homer Simpson  Springfield Rule  Marge Simpson [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=1111-Marge Simpson "Bart Simpson" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=11112-Bart Simpson] "Lisa Simpsons" [EX:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=111113-Lisa Simpsons]  
    '@ 
    [Regex]::Matches($UserMailData,'(")(\\?.)*?\1').Value -join ','
    
    # Results
    
    "Bart Simpson","Lisa Simpsons"

     

You must be logged in to reply to this topic.