Skip to content

Commit 92ea57e

Browse files
committed
Updating Codes
Updating Codes
1 parent f9c8326 commit 92ea57e

File tree

10 files changed

+228
-143
lines changed

10 files changed

+228
-143
lines changed
2.5 KB
Binary file not shown.

Backup-Restore/Backup-Restore.ssmssqlproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
<FullPath>1) Generate - Backup Script.sql</FullPath>
8787
</FileNode>
8888
<FileNode Name="2) Script Out DB Permissions in case of DB Refresh.sql">
89-
<AssociatedConnectionMoniker />
90-
<AssociatedConnSrvName />
89+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPEDB2:True</AssociatedConnectionMoniker>
90+
<AssociatedConnSrvName>TUL1CIPEDB2</AssociatedConnSrvName>
9191
<AssociatedConnUserName />
9292
<FullPath>2) Script Out DB Permissions in case of DB Refresh.sql</FullPath>
9393
</FileNode>
@@ -151,6 +151,12 @@
151151
<AssociatedConnUserName />
152152
<FullPath>Query - Restore History.sql</FullPath>
153153
</FileNode>
154+
<FileNode Name="RevLogin-Script.sql">
155+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPEDB2:True</AssociatedConnectionMoniker>
156+
<AssociatedConnSrvName>TUL1CIPEDB2</AssociatedConnSrvName>
157+
<AssociatedConnUserName />
158+
<FullPath>RevLogin-Script.sql</FullPath>
159+
</FileNode>
154160
</Items>
155161
</LogicalFolder>
156162
<LogicalFolder Name="Miscellaneous" Type="3" Sorted="true">

Backup-Restore/RevLogin-Script.sql

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---/**This login script creates the logins that have the original Security Identifier (SID) and the original password**/
2+
3+
4+
USE master
5+
GO
6+
IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL
7+
DROP PROCEDURE sp_hexadecimal
8+
GO
9+
CREATE PROCEDURE sp_hexadecimal
10+
@binvalue varbinary(256),
11+
@hexvalue varchar (514) OUTPUT
12+
AS
13+
DECLARE @charvalue varchar (514)
14+
DECLARE @i int
15+
DECLARE @length int
16+
DECLARE @hexstring char(16)
17+
SELECT @charvalue = '0x'
18+
SELECT @i = 1
19+
SELECT @length = DATALENGTH (@binvalue)
20+
SELECT @hexstring = '0123456789ABCDEF'
21+
WHILE (@i <= @length)
22+
BEGIN
23+
DECLARE @tempint int
24+
DECLARE @firstint int
25+
DECLARE @secondint int
26+
SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
27+
SELECT @firstint = FLOOR(@tempint/16)
28+
SELECT @secondint = @tempint - (@firstint*16)
29+
SELECT @charvalue = @charvalue +
30+
SUBSTRING(@hexstring, @firstint+1, 1) +
31+
SUBSTRING(@hexstring, @secondint+1, 1)
32+
SELECT @i = @i + 1
33+
END
34+
35+
SELECT @hexvalue = @charvalue
36+
GO
37+
38+
IF OBJECT_ID ('sp_help_revlogin') IS NOT NULL
39+
DROP PROCEDURE sp_help_revlogin
40+
GO
41+
CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS
42+
DECLARE @name sysname
43+
DECLARE @type varchar (1)
44+
DECLARE @hasaccess int
45+
DECLARE @denylogin int
46+
DECLARE @is_disabled int
47+
DECLARE @PWD_varbinary varbinary (256)
48+
DECLARE @PWD_string varchar (514)
49+
DECLARE @SID_varbinary varbinary (85)
50+
DECLARE @SID_string varchar (514)
51+
DECLARE @tmpstr varchar (1024)
52+
DECLARE @is_policy_checked varchar (3)
53+
DECLARE @is_expiration_checked varchar (3)
54+
55+
DECLARE @defaultdb sysname
56+
57+
IF (@login_name IS NULL)
58+
DECLARE login_curs CURSOR FOR
59+
60+
SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
61+
sys.server_principals p LEFT JOIN sys.syslogins l
62+
ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name <> 'sa'
63+
ELSE
64+
DECLARE login_curs CURSOR FOR
65+
66+
67+
SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
68+
sys.server_principals p LEFT JOIN sys.syslogins l
69+
ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name
70+
OPEN login_curs
71+
72+
FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
73+
IF (@@fetch_status = -1)
74+
BEGIN
75+
PRINT 'No login(s) found.'
76+
CLOSE login_curs
77+
DEALLOCATE login_curs
78+
RETURN -1
79+
END
80+
SET @tmpstr = '/* sp_help_revlogin script '
81+
PRINT @tmpstr
82+
SET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */'
83+
PRINT @tmpstr
84+
PRINT ''
85+
WHILE (@@fetch_status <> -1)
86+
BEGIN
87+
IF (@@fetch_status <> -2)
88+
BEGIN
89+
PRINT ''
90+
SET @tmpstr = '-- Login: ' + @name
91+
PRINT @tmpstr
92+
IF (@type IN ( 'G', 'U'))
93+
BEGIN -- NT authenticated account/group
94+
95+
SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']'
96+
END
97+
ELSE BEGIN -- SQL Server authentication
98+
-- obtain password and sid
99+
SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) )
100+
EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT
101+
EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT
102+
103+
-- obtain password policy state
104+
SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
105+
SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
106+
107+
SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']'
108+
109+
IF ( @is_policy_checked IS NOT NULL )
110+
BEGIN
111+
SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked
112+
END
113+
IF ( @is_expiration_checked IS NOT NULL )
114+
BEGIN
115+
SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked
116+
END
117+
END
118+
IF (@denylogin = 1)
119+
BEGIN -- login is denied access
120+
SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name )
121+
END
122+
ELSE IF (@hasaccess = 0)
123+
BEGIN -- login exists but does not have access
124+
SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name )
125+
END
126+
IF (@is_disabled = 1)
127+
BEGIN -- login is disabled
128+
SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE'
129+
END
130+
PRINT @tmpstr
131+
END
132+
133+
FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
134+
END
135+
CLOSE login_curs
136+
DEALLOCATE login_curs
137+
RETURN 0
138+
GO
139+
140+
141+
--Note This script creates two stored procedures in the master database. The two stored procedures are named the sp_hexadecimal stored procedure
142+
143+
144+
--*************************************
145+
--Run the following statement.
146+
147+
EXEC sp_help_revlogin
148+
149+
--The output script that is generated by the sp_help_revlogin stored procedure is the login script.
150+
--This login script creates the logins that have the original Security Identifier (SID) and the original password.
151+
--On server B, start SQL Server Management Studio, and then connect to the instance of SQL Server to which you moved the database.
152+
153+
--***************************************
154+
/*
155+
EXEC sp_change_users_login 'Report'
156+
157+
Exec sp_change_users_login 'auto_fix','loginname'
158+
*/

InstanceMigration/InstanceMigration.ssmssqlproj

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22
<SqlWorkbenchSqlProject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="InstanceMigration">
33
<Items>
44
<LogicalFolder Name="Connections" Type="2" Sorted="true">
5-
<Items />
5+
<Items>
6+
<ConnectionNode Name="BAN-2ADWIVEDI-L:sa">
7+
<Created>2018-04-25T18:24:42.8791384+05:30</Created>
8+
<Type>SQL</Type>
9+
<Server>BAN-2ADWIVEDI-L</Server>
10+
<UserName>sa</UserName>
11+
<Authentication>SQL</Authentication>
12+
<InitialDB />
13+
<LoginTimeout>30</LoginTimeout>
14+
<ExecutionTimeout>0</ExecutionTimeout>
15+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
16+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
17+
</ConnectionNode>
18+
</Items>
619
</LogicalFolder>
720
<LogicalFolder Name="Queries" Type="0" Sorted="true">
8-
<Items />
21+
<Items>
22+
<FileNode Name="LoginsUsers.sql">
23+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:BAN-2ADWIVEDI-L:False:sa</AssociatedConnectionMoniker>
24+
<AssociatedConnSrvName>BAN-2ADWIVEDI-L</AssociatedConnSrvName>
25+
<AssociatedConnUserName>sa</AssociatedConnUserName>
26+
<FullPath>LoginsUsers.sql</FullPath>
27+
</FileNode>
28+
</Items>
929
</LogicalFolder>
1030
<LogicalFolder Name="Miscellaneous" Type="3" Sorted="true">
1131
<Items>

InstanceMigration/LoginsUsers.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- 01) Script Out User Permissions
2+
Import-Module dbatools;
3+
Export-DbaUser -SqlInstance TUL1CIPXDB18 -Database Logging -User RV_BP_APPUSR
4+
5+
-- 01) Script Out User Permissions
6+
Import-Module dbatools;
7+
Export-DbaUser -SqlInstance TUL1CIPXDB18 -Database Logging -User RV_BP_APPUSR

OlaHallengren.com/OlaHallengren.com.ssmssqlproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<AssociatedConnUserName />
2626
<FullPath>DatabaseBackup.sql</FullPath>
2727
</FileNode>
28+
<FileNode Name="Queries-Used.sql">
29+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPEDB2:True</AssociatedConnectionMoniker>
30+
<AssociatedConnSrvName>TUL1CIPEDB2</AssociatedConnSrvName>
31+
<AssociatedConnUserName />
32+
<FullPath>Queries-Used.sql</FullPath>
33+
</FileNode>
2834
</Items>
2935
</LogicalFolder>
3036
<LogicalFolder Name="Miscellaneous" Type="3" Sorted="true">

OlaHallengren.com/Queries-Used.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
USE DBA
2+
-- OlaHallengren - DatabaseBackup - Configurable @Directory Backup Folder Names
3+
-- Modified By Ajay Dwivedi
4+
5+
-- [backupfile] = ServerName_DatabaseName_BackupType_yyyymmdd_hhmmss.bak
6+
7+
EXEC [dbo].[DatabaseBackup]
8+
@Databases = 'USER_DATABASES',
9+
10+
--@Directory = 'E:\Backup\**DATABASENAME**',
11+
/* Output like 'E:\Backup\DatabaseName\backupfile.bak' */
12+
13+
--@Directory = 'E:\Backup\**DATABASENAME**\**BACKUPTYPE**',
14+
/* Output like 'E:\Backup\DatabaseName\BackupType\backupfile.bak' */
15+
16+
--@Directory = 'E:\Backup',
17+
/* Output like 'E:\Backup\ServerName\DatabaseName\BackupType\backupfile.bak' */
18+
19+
@Directory = 'E:\Backup\*',
20+
/* Output like 'E:\Backup\backupfile.bak' */
21+
22+
@BackupType = 'FULL',
23+
@Compress = 'y'

PowerShell Commands/Common-Commands.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ if (Get-Module -ListAvailable -Name SqlServer) {
3838
Write-Host "Module exists"
3939
} else {
4040
Write-Host "Module does not exist"
41-
}
41+
}
42+
43+
-- 11) Find path of SQLDBATools Module
44+
(Get-Module -ListAvailable SQLDBATools).Path

0 commit comments

Comments
 (0)