Pull Computer objects from AD group with Powershell

Hi EE

I have the script below that pulls the members of the AD groups listed in the groups.txt file , but it does not pull computer objects .. can someone help me modify it so it also does ?

Import-Module Activedirectory
[array]$Members=$null
GC groups.txt | % {
$Group = Get-ADGroup $_  -ErrorAction SilentlyContinue
If ($Group){
$members += Get-ADGroup $Group.Name -Properties Members |  
                Select-Object -ExpandProperty Members |  
                Get-ADObject -properties Samaccountname |  
                ?{$_.ObjectClass -eq "user"}  | Get-aduser -Properties * |
        Select @{L='GroupName';e={$Group.Name}},Name,Samaccountname,Enabled,Mail,LastLogonDate
 }
}
$Members | Select * | Export-Csv Groupinfo_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation
LVL 2
MilesLoganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brad GrouxSenior Manager (Wintel Engineering)Commented:
I'd utilize Get-ADUser to query results for users and Get-ADComputer to query results for computers.

Get-ADUser -Filter * -Properties * | Select-Object -Property Name,MemberOf,PrimaryGroup | Sort-Object -Property Name

Open in new window

0
becraigCommented:
Brad is perfectly correct this can be so much easier using get-aduser and get-adcomputer where memberof $group

anywho to do what you need you will also have to add a where clause:
?{$_.ObjectClass -eq "computer"}

I can whip a quick script to do both in one for you if you want.
0
MilesLoganAuthor Commented:
thanks guys .. so I can add ?{$_.ObjectClass -eq "computer"} to my existing script ?

Id like to keep the existing script since I use that to pull from groups that have more then 10k objects.
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

becraigCommented:
You will have to add some extra logic then, since you have the statement piping into get-aduser etc.

However yes that will get you what you need.


you can open a new question if you need someone to rework the script for you.
0
SubsunCommented:
Are you trying to pull both Users and computer objects? If yes.. Following is the slightly modified version of the code (untested).. which will export all members.. Check and let me know if you find any issues..
Import-Module Activedirectory
[array]$Members=$null
GC groups.txt | % {
$Group = Get-ADGroup $_  -ErrorAction SilentlyContinue
If ($Group){
$members += Get-ADGroup $Group.Name -Properties Members |  
  Select-Object -ExpandProperty Members |  
  Get-ADObject -properties * | %{
	$obj = $_
      Switch ($_.ObjectClass){
	   User {$obj | Get-aduser -Pr * | Select @{L='GroupName';e={$Group.Name}},Name,Samaccountname,Enabled,Mail,LastLogonDate,ObjectClass}
	 computer {$obj | Get-adcomputer -Pr * | Select @{L='GroupName';e={$Group.Name}},Name,Samaccountname,Enabled,Mail,LastLogonDate,ObjectClass}
	default {$obj | Select @{L='GroupName';e={$Group.Name}},Name,Samaccountname,Enabled,Mail,LastLogonDate,ObjectClass}
	}
    }
 }
}
$Members | Select * | Export-Csv Groupinfo_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Thank you Subsun !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.