functions/Find-DbaLoginInGroup.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
182
183
184
function Find-DbaLoginInGroup
{
<#
.SYNOPSIS
Finds Logins in Active Directory groups that have logins on the SQL Instance.
 
.DESCRIPTION
Outputs all the active directory groups members for a server, or limits it to find a specific AD user in the groups
  
.NOTES
Original Author: Stephen Bennett, https://sqlnotesfromtheunderground.wordpress.com/
Original Author: Simone Bizzotto, @niphlod
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.PARAMETER SqlInstance
SQLServer name or SMO object representing the SQL Server to connect to. This can be a
collection and recieve pipeline input.
 
.PARAMETER SqlCredential
PSCredential object to connect under. If not specified, currend Windows login will be used.
 
.PARAMETER Login
Find all AD Groups used on the instance that an individual login is a member of.
  
.LINK
https://dbatools.io/Find-DbaLoginInGroup
 
.EXAMPLE
Find-DbaLoginInGroup -SqlInstance DEV01 -Login "MyDomain\Stephen.Bennett"
 
Returns all active directory groups with logins on Sql Instance DEV01 that contain the AD user Stephen.Bennett.
 
.EXAMPLE
Find-DbaLoginInGroup -SqlInstance DEV01
 
Returns all active directory users within all windows AD groups that have logins on the instance.
 
.EXAMPLE
Find-DbaLoginInGroup -SqlInstance DEV01 | Where-Object Login -like '*stephen*'
 
Returns all active directory users within all windows AD groups that have logins on the instance whose login contains 'stephen'
 
#>

[CmdletBinding()]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlServer")]
[string[]]$SqlInstance,
[System.Management.Automation.PSCredential]$SqlCredential,
[string[]]$Login
)
begin
{
try
{
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
catch
{
Write-warning "Failed to load Assembly needed"
break
}

function Get-AllLogins
{
param
(
[string]$ADGroup,
[string[]]$discard
)
begin
{
$output = @()
}
process
{
try
{
$domain = $AdGroup.Split("\")[0]
$ads = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('Domain', $domain)
[string]$groupName = $AdGroup
$group = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($ads, $groupName);
$subgroups = @()
foreach ($member in $group.Members)
{
$memberDomain = $member.distinguishedname -Split "," | Where-Object { $_ -like "DC=*" } | Select-Object -first 1 | ForEach-Object { $_.ToUpper() -replace "DC=", '' }
if ($member.StructuralObjectClass -eq "group")
{
$fullName = $memberDomain + "\" + $member.SamAccountName
if ($fullName -in $discard)
{
Write-Verbose "skipping $fullName, already enumerated"
Continue
}
else
{
$subgroups += $fullName
}
}
else
{
$output += [PSCustomObject]@{
SqlInstance = $server.Name
InstanceName = $server.ServiceName
ComputerName = $server.NetName
Login = $memberDomain + "\" + $member.SamAccountName
MemberOf = $AdGroup
}
}
}
}
catch
{
Write-Warning "Failed to connect to Group: $member."
}
$discard += $ADGroup
foreach ($gr in $subgroups)
{
if ($gr -notin $discard)
{
$discard += $gr
Write-Verbose "Recursing Looking at $gr"
Get-AllLogins -ADGroup $gr -discard $discard
}
}
}
end
{
$output
}
}
}

PROCESS
{
foreach ($Instance in $SqlInstance)
{
try
{
Write-Verbose "Connecting to $Instance"
$server = Connect-SqlServer -SqlServer $Instance -SqlCredential $sqlcredential
}
catch
{
Write-Warning "Failed to connect to: $Instance"
continue
}

$AdGroups = $server.Logins | Where-Object { $_.LoginType -eq "WindowsGroup" -and $_.Name -ne "BUILTIN\Administrators" -and $_.Name -notlike "*NT SERVICE*" }

foreach ($AdGroup in $AdGroups)
{
Write-Verbose "Looking at Group: $AdGroup"
$ADGroupOut += Get-AllLogins $AdGroup.Name
}

if (-not $Login)
{
$res = $ADGroupOut
}
else
{
$res = $ADGroupOut | Where-Object { $Login -contains $_.Login }
if ($res.Length -eq 0)
{
Write-Warning "No logins matching $($Login -join ',') found connecting to $server"
continue
}
}
Select-DefaultView -InputObject $res -Property SqlInstance, Login, MemberOf
}
}
}