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 6 replies, has 6 voices, and was last updated by

 
Participant
2 days, 3 hours 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

    Moderator
    Topics: 8
    Replies: 884
    Points: 2,716
    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
    
  • #161445

    Participant
    Topics: 0
    Replies: 11
    Points: 31
    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: 11
    Points: 31
    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: 22
    Replies: 619
    Points: 1,285
    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: 1
    Replies: 32
    Points: 157
    Helping Hand
    Rank: Participant

    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: 966
    Points: 1,769
    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.