PoshCode Logo PowerShell Code Repository

Get-LocalGroups (modification of post by view diff)
View followups from tojo2000 | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1267"></script>download | new post

I import this as a module because it depends on the other two functions. You could easily dot-source it as well. Requires v2 CTP3 or higher to get the help documentation to work.

  1. function Add-NoteProperty {
  2.   <#
  3. .Synopsis
  4.   Adds a NoteProperty member to an object.
  5. .Description
  6.   This function makes adding a property a lot easier than Add-Member, assuming
  7.   you want to add a NoteProperty, which I find is true about 90% of the time.
  8. .Parameter object
  9.   The object to add the property to.
  10. .Parameter name
  11.   The name of the new property.
  12. .Parameter value
  13.   The object to add as the property.
  14. .Example
  15.   # Create a new custom object and add some properties.
  16.   PS> $custom_obj = New-Object PSObject
  17.   PS> Add-NoteProperty $custom_obj Name 'Custom'
  18.   PS> Add-NoteProperty $custom_obj Value 42
  19. .Example
  20.   # Add a NoteProperty by passing the object to be modified down the pipeline.
  21.   PS> $bunch_of_objects | Add-NoteProperty -name 'meaning_of_life' -value 42
  22. .Notes
  23.   NAME:      Add-NoteProperty
  24.   AUTHOR:    Tim Johnson <tojo2000@tojo200.com>
  25.   FILE:      LocalGroups.psm1
  26. #>
  27.   param([Parameter(Mandatory = $true,
  28.   ValueFromPipeline = $true,
  29.   Position = 0)]
  30.   $object,
  31.   [Parameter(Mandatory = $true,
  32.   Position = 1)]
  33.   [string]$name,
  34.   [Parameter(Mandatory = $true,
  35.   Position = 2)]
  36.   $value)
  37.  
  38.   BEGIN{}
  39.  
  40.   PROCESS{
  41.     $object | Add-Member -MemberType NoteProperty `
  42.     -Name $name `
  43.     -Value $property
  44.   }
  45.  
  46.   END{}
  47. }
  48.  
  49.  
  50. function Get-COMProperty{
  51. <#
  52. .Synopsis
  53.   Gets a property of a __ComObject object.
  54. .Description
  55.   This function calls the InvokeMember static method of the class to get
  56.   properties that aren't directly exposed to PowerShell, such as local group
  57.   members found by calling Members() on a DirectoryServices group object.
  58. .Parameter com_object
  59.   The object to retrieve the property from.
  60. .Parameter property_name
  61.   The name of the property.
  62. .Example
  63.   # Get the names of all members of a group.
  64.   PS> [adsi]$computer = 'WinNT://servername'
  65.   PS> $groups = $groups = $computer.psbase.children |
  66.   >>    ?{$_.psbase.schemaclassname -eq 'group'}
  67.   PS> $groups[0].Members() | %{Get-COMProperty $_ 'Name'}
  68. .Notes
  69.   NAME:      Get-COMProperty
  70.   AUTHOR:    Tim Johnson <tojo2000@tojo200.com>
  71.   FILE:      LocalGroups.psm1
  72. #>
  73.   param([Parameter(Mandatory = $true,
  74.   Position = 1)]
  75.   $com_object,
  76.   [Parameter(Mandatory = $true,
  77.   Position = 2)]
  78.   [string]$property_name)
  79.  
  80.   [string]$property = $com_object.GetType().InvokeMember($property_name,
  81.   'GetProperty',
  82.   $null,
  83.   $com_object,
  84.   $null)
  85.   Write-Output $property
  86. }
  87.  
  88.  
  89. function Get-LocalGroups{
  90. <#
  91. .Synopsis
  92.   Gets a list of objects with information about local groups and their members.
  93. .Description
  94.   This function returns a list of custom PSObjects that are a list of local
  95.   groups on a computer.  Each object has a property called Members that is a
  96.   list of PSObjects representing each member, with Name, Domain, and ADSPath.
  97. .Parameter computername
  98.   The object to retrieve the property from.
  99. .Example
  100.   # Get a list of groups from a server.
  101.   PS> Get-LocalGroups servername
  102. .Notes
  103.   NAME:      Get-LocalGroups
  104.   AUTHOR:    Tim Johnson <tojo2000@tojo200.com>
  105.   FILE:      LocalGroups.psm1
  106. #>
  107.   param([Parameter(Mandatory = $true,
  108.   ValueFromPipeline = $true)]
  109.   [string]$computername = $env:computername)
  110.  
  111.   BEGIN{}
  112.  
  113.   PROCESS{
  114.     $output = @()
  115.     [adsi]$computer = "WinNT://$computername"
  116.     $groups = $computer.psbase.children |
  117.     ?{$_.psbase.schemaclassname -eq 'group'}
  118.  
  119.     foreach ($group in $groups) {
  120.       $members = @()
  121.       $grp_obj = New-Object PSObject
  122.       Add-NoteProperty $grp_obj 'Name' $group.Name.ToString()
  123.       Add-NoteProperty $grp_obj 'aDSPath' $group.aDSPath.ToString()
  124.  
  125.       foreach ($user in $group.Members()){
  126.         $usr_obj = New-Object PSObject
  127.         Add-NoteProperty $usr_obj 'aDSPath' (Get-COMProperty $user 'aDSPath')
  128.         Add-NoteProperty $usr_obj 'Name' (Get-COMProperty $user 'Name')
  129.         $path = $usr_obj.aDSPath.split('/')
  130.  
  131.         if ($path.Count -eq 4){
  132.           Add-NoteProperty $usr_obj 'Domain' $path[2]
  133.         }elseif ($path.Count -eq 5) {
  134.           Add-NoteProperty $usr_obj 'Domain' $path[3]
  135.         }else{
  136.           Add-NoteProperty $usr_obj 'Domain' 'Unknown'
  137.         }
  138.  
  139.         $members += $usr_obj
  140.       }
  141.  
  142.       Add-NoteProperty $grp_obj 'Members' $members
  143.       Write-Output $grp_obj
  144.     }
  145.   }
  146.   END{}
  147. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me