Avatar of Mike
MikeFlag for United States of America

asked on 

Need Help with existing powershell script

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

PowershellVB ScriptScripting Languages

Avatar of undefined
Last Comment
Mike
ASKER CERTIFIED SOLUTION
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mike
Mike
Flag of United States of America image

ASKER

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....
SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Mike
Mike
Flag of United States of America image

ASKER

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of Mike
Mike
Flag of United States of America image

ASKER

Ok... I can understand your point.... I will use your idea....
Avatar of Mike
Mike
Flag of United States of America image

ASKER

The Idea worked perfect that you both for your help this problem......  :)
Avatar of Qlemo
Qlemo
Flag of Germany image

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 ...
Avatar of Mike
Mike
Flag of United States of America image

ASKER

Qlemo - you are right and I do apologize... I have requested the Moderators split up the points 50/50 to make it more balanced..... :)
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo