locked
Help with RegEx RRS feed

  • Question

  • Greetings!

    I have a code which writes the following output to a CSV-file:

    "abc\eva","abc.com/HOSTING/xyz/greger@xyz.se"
    "abc\fredric","abc.com/HOSTING/xyz/invoice@xyz.se"
    "abc\patrick","abc.com/HOSTING/xyz/invoice@xyz.se"

    And I would now like to RegEx/Split it to the following output:

    "patrick","invoice@xyz.se"

    I've tried to accomplish it with $variable.Split("\") and with some RegEx, but I don't know how to accomplish to remove both \ and / ?

    Thanks in advance! 

    Monday, February 17, 2014 7:06 AM

Answers

  • I gave you the answer mostly.

    Here is a simpler approach:

    $FullAccess="abc\eva","abc.com/HOSTING/xyz/greger@xyz.se"

    $a=$FullAccess[0].Split('\')[1]

    $b=$FullAccess.Split('/')[-1]


    ¯\_(ツ)_/¯

    • Marked as answer by smekfis Monday, February 17, 2014 8:24 AM
    Monday, February 17, 2014 8:13 AM

All replies

  • This?

    if("abc.com/HOSTING/xyz/greger@xyz.se" -match '(?<x>@.*)'){$matches['x']}


    ¯\_(ツ)_/¯

    Monday, February 17, 2014 7:21 AM
  • Thanks, but not exactly. The code that generate the output is:

    foreach ($mailbox in $mailboxes)

    {

    get-mailbox $mailbox.UserPrincipalName | Get-ADPermission | ? {($_.ExtendedRights -like "Send-As") -and $_.User -notlike "NT AUTHORITY\SELF" -and !$_.IsInherited} | Select User,Identity -OutVariable $SendAs get-mailbox $mailbox.Identity | Get-MailboxPermission | ? {($_.AccessRights -eq "FullAccess") -and !($_.IsInherited)} | Select User, Identity -OutVariable $FullAccess

    }


    The output from $FullAccess is:

    "abc\eva","abc.com/HOSTING/xyz/greger@xyz.se"

    but I want it to be:

    "patrick","invoice@xyz.se"

    I just need some direction on how to accomplish it, it's so frustrating when you don't know which method to use..

    Monday, February 17, 2014 7:46 AM
  • Your question and examples make no technical sense.

    Sorry.  There is no relationship between the two parts in the example.

    You also need to spend some time learning the basics of PowerShell. 

    How would you expect to change abc/eva to Patrick?  Why are you using the mailbox identity to change a name.  It is a technical no go.


    ¯\_(ツ)_/¯

    Monday, February 17, 2014 7:55 AM
  • Wops, guess I will blame that one on the early morning over here *grabs another cup of coffee*.

    Lets take it again,

    The output from $FullAccess is:

    "abc\eva","abc.com/HOSTING/xyz/greger@xyz.se"

    but I want it to be:

    "eva","greger@xyz.se"

    Explanation: I want to remove everything before "\" and between the name to the last "/".

    The pattern is always "something\name","something.com/HOSTING/something/email@something.com"

    So the output I would like it to be is:

    "name","email@something.com"

    Does it makes sense now?

    The reason why I use Identify is to be able to extract the correct information regarding access to a mailbox, as far as I know. And as I said, I don't want to change the name ;) .

    Thanks in advance!





    • Edited by smekfis Monday, February 17, 2014 8:07 AM
    Monday, February 17, 2014 8:04 AM
  • I gave you the answer mostly.

    Here is a simpler approach:

    $FullAccess="abc\eva","abc.com/HOSTING/xyz/greger@xyz.se"

    $a=$FullAccess[0].Split('\')[1]

    $b=$FullAccess.Split('/')[-1]


    ¯\_(ツ)_/¯

    • Marked as answer by smekfis Monday, February 17, 2014 8:24 AM
    Monday, February 17, 2014 8:13 AM
  • I gave you the answer mostly.

    Here is a simpler approach:

    $FullAccess="abc\eva","abc.com/HOSTING/xyz/greger@xyz.se"

    $a=$FullAccess[0].Split('\')[1]

    $b=$FullAccess.Split('/')[-1]


    ¯\_(ツ)_/¯

    I tried a similar way before posting, but with the [1] and [-1] it makes more sense, even thought it doesn't work 100% I'm on the road again.

    And I don't see the point in being harsh, I've read other threads here and your attitude is rude, even when you come with a great answer you're begin rude.

    At last, God loves you! And thanks for the help, even if you could try to be more friendly :) .

    Monday, February 17, 2014 8:24 AM
  • The code you posted shows a lack of familiarity with the fundamentals. Posting first to a file is a waste of effort and a result of a poor understanding of the basics.

    The suggestion to learn the basics is intended to help you reduce the effort you are putting into this.

    I suggest that what you are trying to do can be done more easily if you learn to fully analyze the problem and bring the best tools to the party.  If you don't know the basic tools you cannot do that.

    This is awkward and likely unnecessary:

    Get-mailbox $mailbox.Identity | Get-MailboxPermission | ? {($_.AccessRights -eq "FullAccess") -and !($_.IsInherited)} | Select User, Identity -OutVariable $FullAccess

    This is more natural and more  flexible:

    et-mailbox $mailbox.Identity | Get-MailboxPermission | ? {($_.AccessRights -eq "FullAccess") -and !($_.IsInherited)} | Select User, Identity -OutVariable $FullAccess

    $outvar+=get-mailbox $mailbox.Identity | 
         Get-MailboxPermission | 
         ? {($_.AccessRights -eq "FullAccess") -and !($_.IsInherited)} | 
         Select User, Identity

    Do you understand why?


    ¯\_(ツ)_/¯

    Monday, February 17, 2014 8:33 AM
  • This is one way to get things from other objects that can be useful which is why we carry around the Identity.

    get-mailbox $mailbox.Identity | 
         Get-MailboxPermission | 
         ? {($_.AccessRights -eq "FullAccess") -and !($_.IsInherited)} | 
         %{Get-AdUser $_.Identity.Distinguishedname -properties *}|
         Select name, mail
    

    Inside the loop these things can be stuffed into a custom object and generate a consistent result. THeimportant thingis to avoid strings. Use objects and keep objects intact.

    Since I have no idea what you are trying to accomplish I cannot point you at a more native solution. You can be reasonably certain that anything you need to do administratively has already been planned for in Exchange and Active Directory.  Asking a more complete question might get you a simple answer that would show you more about how these things are designed.


    ¯\_(ツ)_/¯

    Monday, February 17, 2014 8:49 AM
  • Thanks jrv, I do see why that would be more appropriate now! And thanks for being kind ;) .

    God bless you!

    Monday, February 17, 2014 8:51 AM
  • Here is an even more complete approach to cascading values although I would do it in reverse but that is harder to code.

    $results=foreach ($mailbox in $mailboxes){
        get-mailbox $mailbox.UserPrincipalName | 
            Get-ADPermission | 
            ? {($_.ExtendedRights -like 'Send-As') -and $_.User -notlike "NT AUTHORITY\SELF" -and !$_.IsInherited} | 
            select  @{N='type';E={'SendAs'}}, Identity, Mail, Name
    
        get-mailbox $mailbox.Identity | 
            Get-MailboxPermission | 
            ? {($_.AccessRights -eq 'FullAccess') -and !($_.IsInherited)} | 
            select  @{N='type';E={'FullAccess'}}, Identity, Mail, Name
    } 
    $results | %{
            $user=Get-AdUser $_.Identity -Properties mail
            $_.Mail=$user.mail
            $_.Name=$user.Samaccountname
        }

    Notice the tag field and the dummy fields which make adding the values after the fact easier.

    These are all patterns that are possible because of the design of PowerShell.  This cannot be done in standard scripting such as VBScript.  (Actually it can but it is much harder to do.


    ¯\_(ツ)_/¯





    • Edited by jrv Monday, February 17, 2014 9:10 AM
    Monday, February 17, 2014 9:06 AM
  • And here would be the most efficient way using the pipeline instead of a loop.

    $mailboxes |
        %{
        get-mailbox $_.UserPrincipalName | 
            Get-ADPermission | 
            ? {($_.ExtendedRights -like 'Send-As') -and $_.User -notlike "NT AUTHORITY\SELF" -and !$_.IsInherited} | 
            select  @{N='type';E={'FullAccess'}}, @{N='Identity';E={$_.Identity.DistinguishedName}}, Mail, Name
    
        get-mailbox $_.Identity | 
            Get-MailboxPermission | 
            ? {($_.AccessRights -eq 'FullAccess') -and !($_.IsInherited)} | 
            select  @{N='type';E={'FullAccess'}}, @{N='Identity';E={$_.Identity.DistinguishedName}}, Mail, Name
    } | %{
            $user=Get-AdUser $_.Identity -Properties mail
            $_.Mail=$user.mail
            $_.Name=$user.Samaccountname
            $_
        }
    


    ¯\_(ツ)_/¯

    Monday, February 17, 2014 9:18 AM