none
Need push over the edge on Powershell script. RRS feed

  • Question

  • So I have the following script written up:

    Import-Module ActiveDirectory
    Write-Host "Now generating a list of all Active Directory Groups. Please pick the group you need more information about and enter it below."
    Get-ADGroup -filter  | Select displayname | Sort-Object displayname | Export-Csv c:\groups.txt -NoTypeInformation
    Invoke-Item c:\groups.txt
    $groupname = read-host "Enter the Group name you require information about."
    Get-ADGroupMember $groupname | Select Name, samAccountName | Sort-Object Name | Export-CSV c:\groupmembers.txt -NoTypeInformation
    Invoke-Item c:\groupmembers.txt

    It is supposed to generate a text file and open it that displays all of our domain groups(which are in a lot of different OUs).

    So what I am missing is the command to get the script to generate the text file and just display the Group names it finds in the Domain.

    I've searched all over and haven't found what the missing context should be. When I use -filter *, the text file generates, but all it shows is "displayname" at the top and nothing else.

    Is it possible to get this to display?

    Wednesday, August 13, 2014 5:50 PM

Answers

  • Hi,

    Try this:

    Get-ADGroup -Filter * | 
        Select -ExpandProperty Name | 
            Sort |
                Out-GridView -PassThru |
                    Get-ADGroupMember |
                        Select Name,SamAccountName |
                            Sort Name |
                                Export-Csv .\groupMemberships.csv -NoTypeInformation
    
    & .\groupMemberships.csv

    This uses the name property instead of the display name. Also, it doesn't create an intermediate text file, it just uses the gridview to select the desired group.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, August 13, 2014 8:25 PM

All replies

  • Hi,

    The DisplayName property isn't returned by default, you'll need to specify that you want it.

    This is how you can generate a text file of just group display names:

    Get-ADGroup -Filter * -Properties DisplayName | 
        Select -ExpandProperty DisplayName | 
            Sort |
                Out-File .\GroupNames.txt


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, August 13, 2014 5:59 PM
  • Thanks for the explanation. I added that snippet into the script, but when I ran it, got the following mesage:

    Select : Cannot process argument because the value of argument "obj" is null.
    Change the value of argument "obj" to a non-null value.
    At I:\Administration\Scripts\Get Group MembersV1.ps1:4 char:5
    +     Select -ExpandProperty DisplayName |
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgument
       NullException
        + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.Selec
       tObjectCommand

    I'll try to debug it myself, but if you happen to see this, let me know if you come up with the resolution faster then my search.

    Thanks.

    Wednesday, August 13, 2014 8:01 PM
  • Can you please post your entire script (using the 'Insert Code Block' button)?

    Also, what's your final goal?


    EDIT: <snip> See below.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, August 13, 2014 8:05 PM
  • Import-Module ActiveDirectory
    Write-Host "Now generating a list of all Active Directory Groups. Please pick the group you need more information about and enter it below."
    Get-ADGroup -Filter * -Properties DisplayName | 
        Select -ExpandProperty DisplayName | 
            Sort |
                Out-File c:\GroupNames.txt
    $groupname = read-host "Enter the Group name you require information about."
    Get-ADGroupMember $groupname | Select Name, samAccountName | Sort-Object Name | Export-CSV c:\groupmembers.txt -NoTypeInformation
    Invoke-Item c:\groupmembers.txt

    Code is inserted. Final goal is to have a list of all our Domain Groups pop up, then user can type the group name in, and text file will open with all the users of the group.

     

    Wednesday, August 13, 2014 8:10 PM
  • Hi,

    Try this:

    Get-ADGroup -Filter * | 
        Select -ExpandProperty Name | 
            Sort |
                Out-GridView -PassThru |
                    Get-ADGroupMember |
                        Select Name,SamAccountName |
                            Sort Name |
                                Export-Csv .\groupMemberships.csv -NoTypeInformation
    
    & .\groupMemberships.csv

    This uses the name property instead of the display name. Also, it doesn't create an intermediate text file, it just uses the gridview to select the desired group.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, August 13, 2014 8:25 PM
  • That works. Better than my original design was intended. I just need to change up some of the wording in the script, then study what you gave as code and decipher how it works. 

    Thanks a million. 

    Wednesday, August 13, 2014 8:40 PM