Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need Help with existing powershell script

Posted on 2014-11-11
10
Medium Priority
?
318 Views
Last Modified: 2014-11-12
Greeting Experts,
I need some help with existing PowerShell script designed to poll Active Directory and get a list of Users and Groups located in the LocalAdminGroup for each server. The Script its self-works with no problem but I need to change the formatting on the output file…..  Instead of putting all of the users/OU Groups to one row I would like to put each users & OU Group to each rule (i.e. one user or ou  to row). I have put an example of the exiting format and new format I am looking for…. Is there somebody out there that can help me with this…    

exmaple 1      Existing formate
PCName1      User1, User2, OU1, OU2
PCName2      User1, User2, OU1, OU3
PCName3      User1, User2, OU1, OU4
PCName4      User1, User2, OU1, OU5
PCName5      User1, User2, OU1, OU6


example 2       New formate
PCName1      User1
PCName1      User2
PCName1      OU1
PCName1      OU2
PCName2      User1
PCName2      User2
PCName2      OU1
PCName2      OU3
PCName3      User1
PCName3      User2
PCName3      OU1
PCName3      OU4






function 

get-localusers { 
        param( 
    [Parameter(Mandatory=$true,valuefrompipeline=$true)] 
    [string]$strComputer) 
    begin {} 
    Process { 
        $adminlist ="" 
        $computer = [ADSI]("WinNT://" + $strComputer + ",computer") 
        $AdminGroup = $computer.psbase.children.find("Administrators") 
        $Adminmembers= $AdminGroup.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}         
        foreach ($admin in $Adminmembers) { $adminlist = $adminlist + $admin + "," }          
        $Computer = New-Object psobject 
        $computer | Add-Member noteproperty ComputerName $strComputer 
        $computer | Add-Member noteproperty Administrators $adminlist   
        Write-Output $computer        
         } 
end {} 
} 
 
Get-QADComputer -OSName "*Windows*Server*" | ForEach-Object {$_.Name}| get-localusers | Export-Csv "C:\LocalAdminGroups_$((get-date).toString('MM-dd-yyyy'))v2.csv"

Open in new window

0
Comment
Question by:amstoots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 1000 total points
ID: 40435912
Try this:
function get-localusers { 
        param( 
    [Parameter(Mandatory=$true,valuefrompipeline=$true)] 
    [string]$strComputer) 
    begin {} 
    Process { 
        $computer = [ADSI]("WinNT://" + $strComputer + ",computer") 
        $AdminGroup = $computer.psbase.children.find("Administrators") 
        $Adminmembers= $AdminGroup.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}         
        foreach ($admin in $Adminmembers)           
        {
        $Computer = New-Object psobject 
        $computer | Add-Member noteproperty ComputerName $strComputer 
        $computer | Add-Member noteproperty Administrators $admin   
        Write-Output $computer        
        }
         } 
end {} 
} 
 
Get-QADComputer -OSName "*Windows*Server*" | ForEach-Object {$_.Name}| get-localusers | Export-Csv "C:\LocalAdminGroups_$((get-date).toString('MM-dd-yyyy'))v2.csv"
                                  

Open in new window

0
 

Author Comment

by:amstoots
ID: 40436439
The script works perfect thanks,  One other quick question.... I have this script to scan for Servers but would like for it look at workstations as well... Is it possible to do this with the existing script....
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 40436983
The last line determines what to process. Just remove the parameter of Get-QADComputer to get up to 1000 machines. Line 21 then looks like:
Get-QADComputer | ForEach-Object {$_.Name}| get-localusers | Export-Csv "C:\LocalAdminGroups_$((get-date).toString('MM-dd-yyyy'))v2.csv"

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:amstoots
ID: 40437644
the suggestion works... but I am looking to scan the entire AD enterprise domain...... Is there a way to update the script for all Devices.... not just the top 1000
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40438050
You know that is a really, really hefty thing to do? Querying a huge amount of machines for local admin account members? You can do that by adding -SizeLimit 0 to Get-QADComputer, but it will take ages to complete. It's really not recommended to do it that way, which needs to connect to each PC found, one by one. And what about those machines not reachable while you run the script?

The usual way to cope with such a task is to run a startup script per GPO. The script will report the local admin members into one shared (troublesome) or per-machine files.
0
 

Author Comment

by:amstoots
ID: 40438248
Ok... I can understand your point.... I will use your idea....
0
 

Author Comment

by:amstoots
ID: 40438255
The Idea worked perfect that you both for your help this problem......  :)
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40438322
Don't you think that point split is unbalanced somewhat? Raheman Mohammed Abdul's modification was a small one, and the add-on questions were all answered by me with something needing more knowledge and experience ...
0
 

Author Comment

by:amstoots
ID: 40438416
Qlemo - you are right and I do apologize... I have requested the Moderators split up the points 50/50 to make it more balanced..... :)
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question