Private/Get-ALHADGroupMemberHT.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
<#PSScriptInfo
.VERSION 1.0.0 .GUID 8855ce9f-551a-44e2-a4e6-1c11c4e52c1d .AUTHOR Dieter Koch .COMPANYNAME .COPYRIGHT (c) 2021-2023 Dieter Koch .TAGS .LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE .PROJECTURI https://github.com/admins-little-helper/ALH .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES 1.0.0 Initial release #> <# .DESCRIPTION Contains a function to search for groups in Active Directory using System.DirectoryServices.DirectorySearcher instead of the Active Directory PowerShell module. .LINK https://github.com/admins-little-helper/ALH #> Function Get-ALHADGroupMemberHT { <# .SYNOPSIS Searches Active Directory for groups and returns results in a hash table. .DESCRIPTION Searches Active Directory for groups and returns results in a hash table. This function uses System.DirectoryServices.DirectorySearcher instead of the Active Directory PowerShell module. .PARAMETER SearchBase Name of the organizational unit to start searching recursively for groups. If not specified, the entire domain will be searched. .PARAMETER Identity Name of the group to search for. Wildcards supported. If not specified, * is used. .EXAMPLE Get-ALHADGroupMemberHT -SearchBase 'OU=Groups1,DC=contoso,DC=com','OU=Groups2,DC=contoso,DC=com' -Verbose Find all groups in two different organizational units and show verbose messages. .EXAMPLE Get-ALHADGroupMemberHT Find all groups in the domain. .EXAMPLE "OU=Groups,OU=Organization,DC=domain,DC=tld", "OU=Distribution Lists,OU=Organization,DC=domain,DC=tld" | Get-ALHADGroupMemberHT Pipe OU distinguished names to search in to function. .EXAMPLE Get-ALHADGroupMemberHT -Identity "group_*" Find all groups where name is staring with 'group_' in the entire domain. .INPUTS System.String for parameter 'SearchBase' .OUTPUTS System.DirectoryServices.SearchResult .NOTES Author: Dieter Koch Email: diko@admins-little-helper.de .LINK https://github.com/admins-little-helper/ALH/blob/main/Help/Get-ALHADGroupMemberHT.txt #> [CmdLetBinding()] [OutputType([System.DirectoryServices.SearchResult])] param ( [Parameter(ValueFromPipeline)] [String[]]$SearchBase, [ValidateNotNullOrEmpty()] [String]$Identity = "*" ) begin { Write-Verbose -Message "Defining LDAP filter..." if ($Identity -ne "*") { $Filter = '(&(objectCategory=group)' + "(name=$Identity))" } else { $Filter = '(objectCategory=group)' } if ( [string]::IsNullOrEmpty($SearchBase)) { try { $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() Write-Verbose "Searching for groups in entire AD domain '$($Domain.Name)'" $SearchBase = ($Domain.GetDirectoryEntry()).DistinguishedName } catch { $_ break } } Write-Verbose -Message "Using LDAPFilter: $Filter" # Define re-usable script block for the AD search $RunSearch = { $Searcher = New-Object System.DirectoryServices.DirectorySearcher $Searcher.SearchRoot = $SearchRoot $Searcher.PageSize = 200 $Searcher.SearchScope = 'subtree' $Searcher.PropertiesToLoad.Add('distinguishedName') > $null $Searcher.PropertiesToLoad.Add('member') > $null $Searcher.Filter = $Filter $Searcher.FindAll() } } process { foreach ($OU in $SearchBase) { try { Write-Verbose "Searching for groups in OU '$OU'" $SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$OU") $Result = & $RunSearch Write-Verbose -Message "# Groups found: $($Result.Count)" $Result } catch { throw "Error searching OU '$OU': $_" } } } end { Write-Verbose -Message "Search finished" } } #region EndOfScript <# ################################################################################ ################################################################################ # # ______ _ __ _____ _ _ # | ____| | | / _| / ____| (_) | | # | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_ # | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __| # | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_ # |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__| # | | # |_| ################################################################################ ################################################################################ # created with help of http://patorjk.com/software/taag/ #> #endregion |