locked
Powershell Script to obtain current members of a Group RRS feed

  • Question

  • Good morning.  Sorry for my English.

    I would like to change the answer obtained by a script.

    Today the scrip that I have, give me the following response:

    Server 1               Group 1                User  1 User 2 User  3

    Server 2               Group 1                User  1 User 2 User  3

    I need the answer is something like:

    Server 1               Group 1                              User  1

    Server 1               Group 1                              User  2 

    Server 1               Group 1                              User  3

    Server 2               Group 1                              User  1

    Server 2               Group 1                              User  2 

    Server 2               Group 1                              User  3 ...Etc

    What change I had to do in the script below to that he gave me this level of response? 

    Import-Module ActiveDirectory

    $Result = @()

    foreach($server in (gc .\servers.txt)){

    $computer = [ADSI](”WinNT://” + $server + “,computer”)

    $Group = $computer.psbase.children.find(”Remote Desktop Users”)

    $a=getAdmins

    function getAdmins

    {$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')

    $members}

    $Result += Write-Output "SERVER: $server $a"

    }

    $Result > C:\Groups\RemoteDesktopUsers.txt

    Grateful.

    Tuesday, February 11, 2014 10:20 AM

Answers

  • $servers=Get-Content servers.txt
    foreach($server in $servers){
         $group=[ADSI]"WinNT://$server/Remote Desktop Users,group"
         $group.psbase.invoke('Members') | 
              ForEach-Object{
                  $props=@{
                       Server=$server
                       Group=$group.Name[0]
                       User=$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
                  }
                  New-Object PsObject -Property $props
               }  
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 11, 2014 11:06 AM
    • Marked as answer by JulioCastroSilva Tuesday, February 11, 2014 11:11 AM
    Tuesday, February 11, 2014 11:05 AM
  • Help export-csv -full


    ¯\_(ツ)_/¯

    Tuesday, February 11, 2014 4:14 PM

All replies

  • #1 - you are not using Active Directory so you do not need to load it. WinNT is not AD.  It is local account management.

    Get-WmiObject Win32_Group -filter 'Name="Remote Desktop Users"' -computer server01

    Your code as posted does not work.  It is not a possible syntax for PowerShell.


    ¯\_(ツ)_/¯

    Tuesday, February 11, 2014 10:54 AM
  • Hi,

    So, there is a way from a script to obtain from a list of servers in a *.txt  the users and groups from the groups Remote Desktop Users or Domain Admins?

    I need the answer is something like:

    Server 1               Group 1                              User  1

    Server 1               Group 1                              User  2 

    Server 1               Group 1                              User  3

    Server 2               Group 1                              User  1

    Server 2               Group 1                              User  2 

    Server 2               Group 1                              User  3 ...Etc

    Grateful.

    Tuesday, February 11, 2014 11:03 AM
  • $servers=Get-Content servers.txt
    foreach($server in $servers){
         $group=[ADSI]"WinNT://$server/Remote Desktop Users,group"
         $group.psbase.invoke('Members') | 
              ForEach-Object{
                  $props=@{
                       Server=$server
                       Group=$group.Name[0]
                       User=$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
                  }
                  New-Object PsObject -Property $props
               }  
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 11, 2014 11:06 AM
    • Marked as answer by JulioCastroSilva Tuesday, February 11, 2014 11:11 AM
    Tuesday, February 11, 2014 11:05 AM
  • Sorry to bother you again, but how i export the aswer to csv?

    Tuesday, February 11, 2014 11:41 AM
  • Help export-csv -full


    ¯\_(ツ)_/¯

    Tuesday, February 11, 2014 4:14 PM