functions/Export-SqlLogin.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
Function Export-SqlLogin
{
<#
.SYNOPSIS
Exports Windows and SQL Logins to a T-SQL file. Export includes login, SID, password, default database, default language, server permissions, server roles, db permissions, db roles.
 
.DESCRIPTION
Exports Windows and SQL Logins to a T-SQL file. Export includes login, SID, password, default database, default language, server permissions, server roles, db permissions, db roles.
 
THIS CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
 
.PARAMETER SqlServer
The SQL Server instance name. SQL Server 2000 and above supported.
 
.PARAMETER FilePath
The file to write to.
 
.PARAMETER NoClobber
Do not overwrite file
  
.PARAMETER Append
Append to file
  
.PARAMETER Exclude
Excludes specified logins. This list is auto-populated for tab completion.
 
.PARAMETER Login
Migrates ONLY specified logins. This list is auto-populated for tab completion. Multiple logins allowed.
 
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
 
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
 
SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.
 
.PARAMETER NoJobs
Doesnot export the Jobs
 
.PARAMETER NoDatabases
oes not export the databases
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
 
.PARAMETER Force
Drops and recreates the login if it exists
  
.NOTES
Author: Chrissy LeMaire (@cl), netnerds.net
 
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/>.
 
.LINK
https://dbatools.io/Export-SqlLogin
 
.EXAMPLE
Export-SqlLogin -SqlServer sql2005 -FilePath C:\temp\sql2005-logins.sql
 
Exports SQL for the logins in server "sql2005" and writes them to the file "C:\temp\sql2005-logins.sql"
 
.EXAMPLE
Export-SqlLogin -SqlServer sqlserver2014a -Exclude realcajun -SqlCredential $scred -FilePath C:\temp\logins.sql -Append
 
Authenticates to sqlserver2014a using SQL Authentication. Exports all logins except for realcajun to C:\temp\logins.sql, and appends to the file if it exists. If not, the file will be created.
 
.EXAMPLE
Export-SqlLogin -SqlServer sqlserver2014a -Login realcajun, netnerds -FilePath C:\temp\logins.sql
 
Exports ONLY logins netnerds and realcajun fron sqlsever2014a to the file C:\temp\logins.sql
 
.NOTES
Author: Chrissy LeMaire (@cl), netnerds.net
 
.LINK
https://dbatools.io/Export-SqlLogin
 
#>


[CmdletBinding(SupportsShouldProcess = $true)]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlInstance")]
[string]$SqlServer,
[Alias("OutFile", "Path","FileName")]
[string]$FilePath,
[object]$SqlCredential,
[Alias("NoOverwrite")]
[switch]$NoClobber,
[switch]$Append,
[switch]$NoDatabases,
[switch]$NoJobs
)

DynamicParam
{
if ($sqlserver)
{
$dbparams = Get-ParamSqlDatabases -SqlServer $sqlserver -SqlCredential $SqlCredential
$allparams = Get-ParamSqlLogins -SqlServer $sqlserver -SqlCredential $SqlCredential
$null = $allparams.Add("Databases", $dbparams.Databases)
return $allparams
}
}

BEGIN
{
if ($FilePath.Length -gt 0)
{
if ($FilePath -notlike "*\*") { $FilePath = ".\$filepath" }
$directory = Split-Path $FilePath
$exists = Test-Path $directory

if ($exists -eq $false)
{
throw "Parent directory $directory does not exist"
}

Write-Output "--Attempting to connect to SQL Servers.."
}


$sourceserver = Connect-SqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential

$source = $sourceserver.DomainInstanceName

# Convert from RuntimeDefinedParameter object to regular array
$Logins = $psboundparameters.Logins
$Exclude = $psboundparameters.Exclude

$outsql = @()

}

PROCESS
{
if ($pipelogin.Length -gt 0)
{
$Source = $pipelogin[0].parent.name
$logins = $pipelogin.name
}

foreach ($sourcelogin in $sourceserver.logins)
{
$username = $sourcelogin.name

if ($Logins -ne $null -and $Logins -notcontains $username) { continue }
if ($Exclude -contains $username -or $username.StartsWith("##") -or $username -eq 'sa')
{
Write-Output "--Skipping $username"
continue
}

$servername = $sourceserver

$userbase = ($username.Split("\")[0]).ToLower()
if ($servername -eq $userbase -or $username.StartsWith("NT "))
{
If ($Pscmdlet.ShouldProcess("console", "Stating $username is skipped because it is a local machine name."))
{
Write-Output "--$username is skipped because it is a local machine name."
}
continue
}

If ($Pscmdlet.ShouldProcess("Outfile", "Adding T-SQL for login $username"))
{
if ($FilePath.Length -gt 0)
{
Write-Output "--Exporting $username"
}

$outsql += "use master"
# Getting some attributes
$defaultdb = $sourcelogin.DefaultDatabase
$language = $sourcelogin.Language

if ($sourcelogin.PasswordPolicyEnforced -eq $false)
{
$checkpolicy = "OFF"
}
else
{
$checkpolicy = "ON"
}

if (!$sourcelogin.PasswordExpirationEnabled)
{
$checkexpiration = "OFF"
}
else
{
$checkexpiration = "ON"
}

# Attempt to script out SQL Login
if ($sourcelogin.LoginType -eq "SqlLogin")
{
$sourceloginname = $sourcelogin.name

switch ($sourceserver.versionMajor)
{
0 { $sql = "SELECT convert(varbinary(256),password) as hashedpass FROM master.dbo.syslogins WHERE loginname='$sourceloginname'" }
8 { $sql = "SELECT convert(varbinary(256),password) as hashedpass FROM dbo.syslogins WHERE name='$sourceloginname'" }
9 { $sql = "SELECT convert(varbinary(256),password_hash) as hashedpass FROM sys.sql_logins where name='$sourceloginname'" }
default
{
$sql = "SELECT CAST(CONVERT(varchar(256), CAST(LOGINPROPERTY(name,'PasswordHash')
         AS varbinary (256)), 1) AS nvarchar(max)) as hashedpass FROM sys.server_principals
         WHERE principal_id = $($sourcelogin.id)"

}
}

try
{
$hashedpass = $sourceserver.ConnectionContext.ExecuteScalar($sql)
}
catch
{
$hashedpassdt = $sourceserver.databases['master'].ExecuteWithResults($sql)
$hashedpass = $hashedpassdt.Tables[0].Rows[0].Item(0)
}

if ($hashedpass.gettype().name -ne "String")
{
$passtring = "0x"; $hashedpass | % { $passtring += ("{0:X}" -f $_).PadLeft(2, "0") }
$hashedpass = $passtring
}

$sid = "0x"; $sourcelogin.sid | ForEach-Object { $sid += ("{0:X}" -f $_).PadLeft(2, "0") }
$outsql += "IF NOT EXISTS (SELECT loginname from master.dbo.syslogins where name = '$username')
CREATE LOGIN [$username] WITH PASSWORD = $hashedpass HASHED, SID = $sid, DEFAULT_DATABASE = [$defaultdb], CHECK_POLICY = $checkpolicy, CHECK_EXPIRATION = $checkexpiration, DEFAULT_LANGUAGE = [$language]"

}

# Attempt to script out Windows User
elseif ($sourcelogin.LoginType -eq "WindowsUser" -or $sourcelogin.LoginType -eq "WindowsGroup")
{
$outsql += "IF NOT EXISTS (SELECT loginname from master.dbo.syslogins where name = '$username')
CREATE LOGIN [$username] FROM WINDOWS WITH DEFAULT_DATABASE = [$defaultdb], DEFAULT_LANGUAGE = [$language]"

}

# This script does not currently support certificate mapped or asymmetric key users.
else
{
Write-Warning "$($sourcelogin.LoginType) logins not supported. $($sourcelogin.name) skipped."
continue
}

if ($sourcelogin.IsDisabled)
{
$outsql += "ALTER LOGIN [$username] DISABLE"
}
if ($sourcelogin.DenyWindowsLogin)
{
$outsql += "DENY CONNECT SQL TO [$username]"
}
}

# Server Roles: sysadmin, bulklogin, etc
foreach ($role in $sourceserver.roles)
{
$rolename = $role.name

# SMO changed over time
try { $rolemembers = $role.EnumMemberNames() }
catch { $rolemembers = $role.EnumServerRoleMembers() }

if ($rolemembers -contains $username)
{
$outsql += "ALTER SERVER ROLE [$rolename] ADD MEMBER [$username]"
}
}

if ($NoJobs -eq $false)
{
$ownedjobs = $sourceserver.JobServer.Jobs | Where { $_.OwnerLoginName -eq $username }

foreach ($ownedjob in $ownedjobs)
{
$outsql += "use msdb"
$jobname = $ownedjob.name
$outsql += "EXEC msdb.dbo.sp_update_job @job_name=N'$ownedjob', @owner_login_name=N'$username'"
}
}

if ($sourceserver.versionMajor -ge 9)
{
# These operations are only supported by SQL Server 2005 and above.
# Securables: Connect SQL, View any database, Administer Bulk Operations, etc.

$perms = $sourceserver.EnumServerPermissions($username)
$outsql += "use master"
foreach ($perm in $perms)
{
$permstate = $perm.permissionstate
$permtype = $perm.PermissionType
$grantor = $perm.grantor

if ($permstate -eq "GrantWithGrant")
{
$grantwithgrant = "WITH GRANT OPTION"
$permstate = "grant"
}
else
{
$grantwithgrant = $null
}

$outsql += "$permstate $permtype TO [$username] $grantwithgrant AS [$grantor]"
}

# Credential mapping. Credential removal not currently supported for Syncs.
$logincredentials = $sourceserver.credentials | Where-Object { $_.Identity -eq $sourcelogin.name }
foreach ($credential in $logincredentials)
{
$credentialname = $credential.name
$outsql += "Print '$username is associated with the $credentialname credential'"
}
}

if ($NoDatabases -eq $false)
{
$databases = $sourcelogin.EnumDatabaseMappings() 
# Adding database mappings and securables
foreach ($db in $databases)
{
$dbname = $db.dbname
$sourcedb = $sourceserver.databases[$dbname]
$dbusername = $db.username
$dblogin = $db.loginName

$outsql += "use [$dbname]"
try
{
$sql = $sourceserver.databases[$dbname].users[$dbusername].script()
$outsql += $sql
}
catch
{
Write-Warning "User cannot be found in selected database"
}

# Skipping updating dbowner

# Database Roles: db_owner, db_datareader, etc
foreach ($role in $sourcedb.roles)
{
if ($role.EnumMembers() -contains $username)
{
$rolename = $role.name
$outsql += "ALTER ROLE [$rolename] ADD MEMBER [$username]"
}
}

# Connect, Alter Any Assembly, etc
$perms = $sourcedb.EnumDatabasePermissions($username)
foreach ($perm in $perms)
{
$permstate = $perm.permissionstate
$permtype = $perm.PermissionType
$grantor = $perm.grantor

if ($permstate -eq "GrantWithGrant")
{
$grantwithgrant = "WITH GRANT OPTION"
$permstate = "grant"
}
else
{
$grantwithgrant = $null
}

$outsql += "$permstate $permtype TO [$username] $grantwithgrant AS [$grantor]"
}
}
}
}
}

END
{
$sql = $sql | Where-Object { $_ -notlike "CREATE USER [dbo] FOR LOGIN * WITH DEFAULT_SCHEMA=[dbo]" }

$sql = $outsql -join "`r`nGO`r`n"

if ($FilePath.Length -gt 0)
{
$sql | Out-File -FilePath $FilePath -Append:$Append -NoClobber:$NoClobber
}
else
{
return $sql
}

If ($Pscmdlet.ShouldProcess("console", "Showing final message"))
{
Write-Output "--SQL Login export to $FilePath complete"
$sourceserver.ConnectionContext.Disconnect()
}
}
}