Answered Script Review

  • Monday, February 11, 2013 8:36 PM
     
     
    Got a script here that lists local admins group members on a list of servers i provide.  Proplem is it shows Column A server name (which is cool) but Column B Member does not include DOMAIN\username123 it just shows user123 name.  I need it to include the domain like it does when yo manually look at the admin group.  Any ideas on what line or lines i need to add to this script?

    $servers= get-content 'c:\input\test.txt'
    $output = 'c:\output\test.csv'
    $results = @()
    foreach($server in $servers)
    {$admins = @()
    $group =[ADSI]"WinNT://$server/Administrators"
    $members = @($group.psbase.Invoke("Members"))
    $members | foreach { 
       $obj = new-object psobject -Property @{ 
       Server = $Server 
       Admin = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) 
       } 
       $admins += $obj 
       }
    $results += $admins
    }
    $results| Export-csv $Output -NoTypeInformation




    Thanks in advance

All Replies

  • Monday, February 11, 2013 9:10 PM
     
      Has Code
    $servers= get-content 'c:\input\test.txt'
    $output = 'c:\output\test.csv'
    foreach($server in $servers){
         $group =[ADSI]"WinNT://$server/Administrators" 
         $members = @($group.psbase.Invoke("Members")) 
         $members |
              foreach { 
                   new-object psobject -Property @{ 
                                                   Server=$Server 
                                                   Admin=$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) 
                                                  } 
                   }
    } | Export-Csv $output -NoType 
    

    Various errors in your script.

    ¯\_(ツ)_/¯

  • Monday, February 11, 2013 9:13 PM
     
     
    It works, just doesnt include the domain the user account is in
  • Monday, February 11, 2013 9:18 PM
     
     
    An empty pipe element is not allowed.
    At C:\scripts\script2.ps1:11 char:4
    + } | <<<<  Export-Csv $output -NoType
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : EmptyPipeElement
  • Monday, February 11, 2013 9:28 PM
     
     

    Sorry  - I will fix that.  I posted the wrong copy of the script.

    The answer is that there are no domains on local accounts.


    ¯\_(ツ)_/¯

  • Monday, February 11, 2013 9:29 PM
     
      Has Code
    $servers=get-content 'servers.txt'
    $output='test.csv'
    $results=foreach($server in $servers){
    
         [ADSI]"WinNT://$server/Administrators" |
         ForEach-Object{
              new-object psobject -Property @{ 
                                              Server=$Server 
                                              Name=$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
                                             } 
         }
    
    } 
    $results | Export-Csv $output -NoType 
    



    ¯\_(ツ)_/¯


  • Monday, February 11, 2013 9:31 PM
     
     
    right but some of the members of the servers in my list are domain accounts from various domains.  so when it says john doe is in the admins group i wanna know if it's DOMAIN1\John Doe or Domain2\john doe etc
  • Monday, February 11, 2013 9:35 PM
     
     
    I dont see a difference in those two scripts got the same error
  • Monday, February 11, 2013 9:37 PM
     
     

    Sorry - I had to change it once again due to an undocumented bug in PowerShell.  The above code is fixed.

    Actually it was not a bug it was just my thick head.


    ¯\_(ツ)_/¯


  • Monday, February 11, 2013 10:00 PM
     
     
    Results just shows "ADMINISTRATORS","SERVER NAME"  thats it.
    • Edited by GovtTech Monday, February 11, 2013 10:02 PM
    •  
  • Monday, February 11, 2013 10:07 PM
     
     Answered Has Code

    Finally one that works and is copied correctly.  I am sorry - My Windows 8 system has decided to act insane.  I cannot cut and paste correctly.

    $servers=get-content 'servers.txt'
    $output='test.csv'
    $results=foreach($server in $servers){
    
         $group=[ADSI]"WinNT://$server/Administrators"
         $group.psbase.Invoke("Members") |
         ForEach-Object{
              new-object psobject -Property @{ 
                                              Server=$Server 
                                              Name=$_.GetType().InvokeMember("adspath", 'GetProperty', $null, $_, $null)
                                             } 
         }
    
    } 
    $results #| Export-Csv $output -NoType 
    

    I am going to quit now and try and fix this irritating system.

    ¯\_(ツ)_/¯

  • Tuesday, February 12, 2013 2:00 PM
     
     
    The results are good.  As for fine tuning, how can they be auto piped in to a csv file?  And swap the columns if possible so that server is on left and users on right.  Thanks a million
  • Tuesday, February 12, 2013 9:47 PM
     
     

    Also what adjustments need to be made to this script to list the same results of share permissions on the servers in the list.  Instead of whos a member of local admins I need a list of shares per server and who has access on those shares.  THanks a million again and I owe you 2 cokes now :)

  • Tuesday, February 12, 2013 10:04 PM
     
     

    If you have new questions you will need to start a new topic/question.

    Be sure to look in the repository first.


    ¯\_(ツ)_/¯