Welcome to Powergui.org - an open source community for Windows Powershell

PowerGUI.org PowerGUI.org and blogs

Forums » Active Directory and PowerShell

Thread: Primary Group

This question is not answered. Helpful answers available: 2. Answered answers available: 1.


Permlink Replies: 16 - Pages: 2 [ 1 2 | Next ] - Last Post: Aug 27, 2008 2:28 PM by: Dmitry Sotnikov
SeanF12

Posts: 8
Registered: 7/27/08
Primary Group
Posted: Jul 27, 2008 2:47 PM
 
  Click to reply to this thread Reply

Does anyone know if there is a way using Powershell to list the primary group (not the ID but the actual group name) for each user in AD?

Thanks



Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Primary Group
Posted: Jul 28, 2008 1:05 AM   in response to: SeanF12
 
  Click to reply to this thread Reply

C:\>get-qaduser  | select name, @{n="PrimaryGroup";e={get-qadgroup $_.PrimaryGroupSid.ToString()}}


Shay Levy


Posts: 1,919
Registered: 1/31/08
Re: Primary Group
Posted: Jul 28, 2008 1:42 AM   in response to: SeanF12
 
  Click to reply to this thread Reply


Here's a (slow) version with where-object. I tried to use an ldap filter but it gives me an error, Andrey? :


foreach($user in Get-QADUser){
  $user | select name, @{n="PrimaryGroup";e={ (Get-QADGroup | where {$_.PrimaryGroupToken -eq $user.PrimaryGroupId}).Name}}
}




PS > Get-QADGroup -ldapFilter '(PrimaryGroupToken=513)'
Get-QADGroup : Inappropriate matching.
At line:1 char:13
+ Get-QADGroup <<<<   -ldapFilter '(PrimaryGroupToken=513)'




Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar
Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Primary Group
Posted: Jul 28, 2008 1:54 AM   in response to: Shay Levy
 
  Click to reply to this thread Reply

PrimaryGroupToken is computed attribute. You can't use in LDAP search against directory.


Shay Levy


Posts: 1,919
Registered: 1/31/08
Re: Primary Group
Posted: Jul 28, 2008 2:03 AM   in response to: Andrey Moiseev ...
 
  Click to reply to this thread Reply

You have a typo in your command:

PrimaryGroupSid

Should be:

PrimaryGroupId




Although when I run it I get a blank PrimaryGroup coulmn:


Name PrimaryGroup
---- ------------
User1 {}
User2 {}
(...)




Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar
Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Primary Group
Posted: Jul 28, 2008 2:10 AM   in response to: Shay Levy
 
  Click to reply to this thread Reply

It's not a typo. PrimaryGroupSid is computed property, that returns SID of primary group. It is computed from domain SID and primary group id.


Shay Levy


Posts: 1,919
Registered: 1/31/08
Re: Primary Group
Posted: Jul 28, 2008 2:21 AM   in response to: Andrey Moiseev ...
 
  Click to reply to this thread Reply

Oh... I run the original one and got this:


PS > get-qaduser  | select name, @{n="PrimaryGroup";e={get-qadgroup $_.PrimaryGroupSid.ToString()}}

Select-Object : You cannot call a method on a null-valued expression.
At line:1 char:22
+ get-qaduser  | select <<<<  name, @{n="PrimaryGroup";e={get-qadgroup $_.PrimaryGroupSid.ToString()}}

Name                                                        PrimaryGroup
----                                                        ------------
User1
Select-Object : You cannot call a method on a null-valued expression.
At line:1 char:22
+ get-qaduser  | select <<<<  name, @{n="PrimaryGroup";e={get-qadgroup $_.PrimaryGroupSid.ToString()}}

User2
Select-Object : You cannot call a method on a null-valued expression.
At line:1 char:22

(...)



Also, I can't find the PrimaryGroupSid on my user account:

PS > (Get-QADUser ShayL).PrimaryGroupSid
# gives nothing




PS > Get-QADUser ShayL -IncludeAllProperties | fl pri*


primaryInternationalISDNNumber :
primaryTelexNumber             :
PrimaryGroupId                 : 513




Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar
Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Primary Group
Posted: Jul 28, 2008 2:41 AM   in response to: Shay Levy
 
  Click to reply to this thread Reply

Oops! It's my bad. This feature will be introduced only in next version. Fixed solution:

get-qaduser  | select name, @{n="PrimaryGroup";e={get-qadgroup "$($_.Domain.Sid)-$($_.PrimaryGroupId)"}}



Shay Levy


Posts: 1,919
Registered: 1/31/08
Re: Primary Group
Posted: Jul 28, 2008 2:52 AM   in response to: Andrey Moiseev ...
 
  Click to reply to this thread Reply


It gives a blank coulmn again, the Sid member is not present on my account.


Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar
Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Primary Group
Posted: Jul 28, 2008 3:17 AM   in response to: Shay Levy
 
  Click to reply to this thread Reply

Again version mess. Sorry. I should test my examples on release version before publishing. Here is final command. It have been tested on my 1.1 installation :)

get-qaduser  | select name, @{n="PrimaryGroup";e={get-qadgroup "$($_.Sid.AccountDomainSid)-$($_.PrimaryGroupId)"}}
Message was edited by: Andrey Moiseev (Quest)


Shay Levy


Posts: 1,919
Registered: 1/31/08
Re: Primary Group
Posted: Jul 28, 2008 3:49 AM   in response to: Andrey Moiseev ...
 
  Click to reply to this thread Reply


Thanks Andrey, it works if I remove the user part in $_.User.Sid (e.g $_.Sid).

Sean, to get the group name instead of its DN:

Get-QADUser  | select name, @{n="PrimaryGroup";e={(get-qadgroup "$($_.Sid.AccountDomainSid)-$($_.PrimaryGroupId)").name}}




Shay Levy [MVP]
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar
Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Primary Group
Posted: Jul 28, 2008 3:55 AM   in response to: Shay Levy
 
  Click to reply to this thread Reply

I've corrected my post. Now it was a typo. :)


SeanF12

Posts: 8
Registered: 7/27/08
Re: Primary Group
Posted: Jul 28, 2008 5:14 AM   in response to: SeanF12
 
  Click to reply to this thread Reply

Fantastic!  Thanks guys, worked like a charm



chris b

Posts: 2
Registered: 8/26/08
Re: Primary Group
Posted: Aug 26, 2008 12:56 PM   in response to: SeanF12
 
  Click to reply to this thread Reply

when i run the command in powergui 1.5 I only get the name column. Forgive my ignorance I am new to Scripting.



Dmitry Sotnikov


Posts: 1,151
Registered: 12/1/06
Re: Primary Group
Posted: Aug 26, 2008 1:32 PM   in response to: chris b
 
  Click to reply to this thread Reply

Chris,

What do you mean by "run the command in powergui"?

You created a script node with the code? Can you right-click the column header and see if you can add the PrimaryGroup column as well?

Dmitry


Legend
MVP: 2501 + pts
Guru: 2001 - 2500 pts
Expert: 751 - 2000 pts
Enthusiast: 31 - 750 pts
Novice: 0 - 30 pts
Moderators
Helpful answer (5 pts)
Answered (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums