Scrip

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5537

    by alizpak at 2013-02-12 03:31:16

    Hi All,

    I have designed a script and it gives me result what I wanted, However there is a slight problem in the memberof column it brings the details in this format CN=xyz,OU=Abc,OU=UK,OU=User Groups.

    Is there any way I can put some thing in my script which only brings the groups names,

    $users=get-content grp.csv
    Foreach($user in $users)
    {
    Get-QADuser $user | Select-Object DisplayName, SamAccountName, AccountIsDisabled, AccountIsExpired, ParentContainer,
    @{n='memberOf';e={$_.MemberOf -join ';'}} | Sort-Object LastName | Export-Csv -Path test.csv
    }

    by mjolinor at 2013-02-12 06:17:27

    The .memberof property is a list of distinguishednames. One way to get the group name (CN) out of that is to use a regular expression.

    [code2=powershell]$regex = 'CN=(.+?),(?:CN|OU|DC).+'

    $users=get-content grp.csv
    Foreach($user in $users)
    {
    Get-QADuser $user | Select-Object DisplayName, SamAccountName, AccountIsDisabled, AccountIsExpired, ParentContainer,
    @{n='memberOf';e={$_.MemberOf -replace $regex,'$1' -join ';'}} | Sort-Object LastName | Export-Csv -Path test.csv
    }[/code2]

    See: get-help about_regular_expressions for more information.

    by alizpak at 2013-02-12 06:28:03

    Great Stuff it worked

    Thank you soo much!!!

    by alizpak at 2013-02-12 07:59:58

    I have now another problem with this script that only picks up 1 user from the input file it is not picking up other users. could you please advise why?

    $users = get-content "C:\Alitest\usr.csv"
    $regex = 'CN=(.+?),(?:CN|OU|DC).+'
    Foreach($user in $users)
    {
    Get-QADuser $user | Select-Object DisplayName, SamAccountName, PrimarySMTPAddress, AccountIsDisabled, AccountIsExpired, ParentContainer,
    @{n='memberOf';e={$_.MemberOf -replace $regex,'$1' -join ';'}} | Sort-Object LastName | Export-Csv -Path Users.csv
    }

    by mjolinor at 2013-02-12 08:12:25

    If you look closely at your loop, you'll notice your export-csv is inside the loop. That means it's doing an export-csv for each user, each time overwriting the csv created by the last user. The end result is that your csv only contains information for the last user that went through the loop. You need to let the loop accumulate all the objects, and then pipe that to export-csv when it's done:

    [code2=powershell]$users = get-content "C:\Alitest\usr.csv"
    $regex = 'CN=(.+?),(?:CN|OU|DC).+'

    &{
    Foreach($user in $users)
    {
    Get-QADuser $user |
    Select-Object DisplayName, SamAccountName, PrimarySMTPAddress, AccountIsDisabled, AccountIsExpired, ParentContainer,
    @{n='memberOf';e={$_.MemberOf -replace $regex,'$1' -join ';'}}
    }
    } |
    Sort-Object LastName |
    Export-Csv -Path Users.csv[/code2]

    by alizpak at 2013-02-12 08:21:20

    Can you please amend this script as I am still learning powershell?

    by mjolinor at 2013-02-12 08:32:59

    I thought I did.....

    by alizpak at 2013-02-12 08:42:58

    Yes you did... Thank you soo much

You must be logged in to reply to this topic.