Try this:
Get-ADGroupMember -Identity "Group Name" -recursive |
Foreach-object {
If ($_.ObjectClass -eq 'computer') {
Get-ADComputer -identity $_ -Properties Name, Description |
sort name |
Format-Table -AutoSize -Property name,description
}
Else {
Get-ADUser -identity $_ -Properties Name, SamAccountName |
sort SamAccountName |
Format-Table -AutoSize -Property name,SamAccountName
}
}
DAMIEN SOLODOW
Senior Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE
________________________________
From: [email protected] [[email protected]] on behalf
of Mote, Todd [[email protected]]
Sent: Thursday, March 24, 2016 7:58 PM
To: '[email protected]'
Subject: [mssms] RE: OT - PowerShell help?
you can’t pipe to IF
if you want to pipe use foreach-object after the pipe, though I’m not sure you
can else that..
Get-ADGroupMember -Identity "Group Name" -recursive |
ForEach-Object($_.ObjectClass -eq 'computer') {Get-ADComputer -Properties Name,
Description | sort name | Format-Table -AutoSize -Property name,description}
I’m not sure how to build the else like this.
I would assign a variable to the group members and put the whole thing in a for
each.
$members=Get-ADGroupMember -Identity "Group Name" -recursive
ForEach ($member in $members) {
If ($member.ObjectClass -eq 'computer') {
Get-ADComputer -Properties Name, Description |
sort name |
Format-Table -AutoSize -Property name,description
}
Else {
Get-ADUser -Properties Name, SamAccountName |
sort SamAccountName |
Format-Table -AutoSize -Property name,SamAccountName
}
}
From: [email protected] [mailto:[email protected]] On
Behalf Of Murray, Mike
Sent: Thursday, March 24, 2016 6:49 PM
To: [email protected]
Subject: [mssms] RE: OT - PowerShell help?
One more for you if you don’t mind. I’d like to view both users and computers
from this security group. When I run this, I get the error below (and one for
“Else”):
If : The term 'If' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was
included,
verify that the path is correct and try again.
At line:3 char:1
+ If ($_.ObjectClass -eq 'computer') {
Get-ADGroupMember -Identity "Group Name" -recursive |
If ($_.ObjectClass -eq 'computer') {
Get-ADComputer -Properties Name, Description |
sort name |
Format-Table -AutoSize -Property name,description
}
Else {
Get-ADUser -Properties Name, SamAccountName |
sort SamAccountName |
Format-Table -AutoSize -Property name,SamAccountName
}
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Damien Solodow
Sent: Thursday, March 24, 2016 4:19 PM
To: [email protected]<mailto:[email protected]>
Subject: [mssms] RE: OT - PowerShell help?
Try this instead:
Get-ADGroupMember -Identity "group name" -recursive |
Where {$_.ObjectClass -eq 'computer'} |
Get-ADComputer -Properties Name, Description |
sort name |
Format-Table -AutoSize -Property name,description
Put you where before your sort; will speed things up and reduce memory
consumption. :)
And you can use format-table to filter the properties and save a step.
DAMIEN SOLODOW
Senior Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE
________________________________
From: [email protected]<mailto:[email protected]>
[[email protected]] on behalf of BURKEY Evan
[[email protected]]
Sent: Thursday, March 24, 2016 7:12 PM
To: [email protected]<mailto:[email protected]>
Subject: [mssms] RE: OT - PowerShell help?
Try adding Format-Table –autosize like so:
Get-ADGroupMember -Identity "Group Name" -recursive | sort name |
Where {$_.ObjectClass -eq 'computer'} |
Get-ADComputer -Properties Name, Description |
Select Name, Description |
Format-Table -AutoSize
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Murray, Mike
Sent: Thursday, March 24, 2016 4:00 PM
To: [email protected]<mailto:[email protected]>
Subject: [mssms] OT - PowerShell help?
Hola,
I’m running the script below to list computers within an AD security group (the
group in question has members of multiple other security groups). It works, but
it cuts off the name if it’s more than 6 characters in length. Annoying! Any
idea why?
Get-ADGroupMember -Identity "Group Name" -recursive | sort name |
Where {$_.ObjectClass -eq 'computer'} |
Get-ADComputer -Properties Name, Description |
Select Name, Description
Example:
[cid:[email protected]]
Best Regards,
Mike Murray
Desktop Management Coordinator - IT Support Services
California State University, Chico
530.898.4357
[email protected]<mailto:[email protected]>