Skip to content

Commit 478d28d

Browse files
authored
Merge pull request #5 from Lombiq/issue/FINI-448
FINI-448: Utility scripts SQL auth
2 parents 144fe13 + ae71e70 commit 478d28d

7 files changed

Lines changed: 88 additions & 17 deletions

File tree

OrchardCore/Reset-OrchardCoreApp/Reset-OrchardCoreApp.psm1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function Reset-OrchardCoreApp
3737

3838
[Parameter(ParameterSetName = "ServerDB")]
3939
[string] $SetupDatabaseName = "OrchardCore",
40+
41+
[Parameter(ParameterSetName = "ServerDB")]
42+
[string] $SetupDatabaseSqlUser = "sa",
43+
44+
[Parameter(ParameterSetName = "ServerDB")]
45+
[string] $SetupDatabaseSqlPassword = $null,
4046

4147
[Parameter(ParameterSetName = "ServerDB")]
4248
[switch] $Force,
@@ -163,7 +169,7 @@ function Reset-OrchardCoreApp
163169

164170
"Using the following database name: `"$SetupDatabaseName`"."
165171

166-
if (New-SqlServerDatabase -SqlServerName $SetupDatabaseServerName -DatabaseName $SetupDatabaseName -Force:$Force.IsPresent -ErrorAction Stop)
172+
if (New-SqlServerDatabase -SqlServerName $SetupDatabaseServerName -DatabaseName $SetupDatabaseName -Force:$Force.IsPresent -ErrorAction Stop -UserName $SetupDatabaseSqlUser -Password $SetupDatabaseSqlPassword)
167173
{
168174
"Database `"$SetupDatabaseServerName\$SetupDatabaseName`" created!"
169175
}
@@ -179,8 +185,17 @@ function Reset-OrchardCoreApp
179185
}
180186
}
181187

188+
$Security = if (-not $SetupDatabaseSqlPassword)
189+
{
190+
"Integrated Security=True"
191+
}
192+
else
193+
{
194+
"User Id=$SetupDatabaseSqlUser;Password=$SetupDatabaseSqlPassword"
195+
}
196+
182197
# MARS is necessary for Orchard.
183-
$SetupDatabaseConnectionString = "Server=$SetupDatabaseServerName;Database=$SetupDatabaseName;Integrated Security=True;MultipleActiveResultSets=True;"
198+
$SetupDatabaseConnectionString = "Server=$SetupDatabaseServerName;Database=$SetupDatabaseName;$Security;MultipleActiveResultSets=True;"
184199
}
185200

186201

Readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ For the Azure-related scripts you'll need the Azure (the new "Az") PowerShell mo
1515

1616
To be able to import SQL Server export files (bacpac files) you'll need to import the *SqlServer\Import-BacpacToSqlServer\Import-BacpacToSqlServer.reg* registry script. Then you'll be able to right click on bacpac files and select "Import to SQL Server with PowerShell", or simply double-click on them. This needs the DAC Framework so if you get a "No SQL Package executable found for importing the database!" error then install it from [here](https://docs.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage-download?view=sql-server-ver15).
1717

18+
If you are working on a remote database without SQL Server locally installed (eg. Azure or the [Docker container](https://hub.docker.com/_/microsoft-mssql-server)) you have to install "PowerShell Extensions for Microsoft SQL Server 2012". Despite the name it works with newer versions as well. See [this tutorial](https://sqlpadawan.com/2018/08/01/how-to-install-sql-server-sqlps-powershell-module/).
19+
20+
As of writing this document [SQL Server in Docker doesn't support Windows authentication](https://github.com/microsoft/mssql-docker/issues/165). You can use the commands that support SQL authentication (eg. `Reset-OrchardCoreApp`) by providing a user name and password with the appropriate arguments.
21+
1822

1923
## Installing the PowerShell modules
2024

@@ -42,7 +46,7 @@ On what the different scripts do specifically and how to use them take a look at
4246
- ExportLastCommitToGit: Exports the files changed in the last commit of a hg repo to a git repo as a patch.
4347
- SqlServer: A lot of scripts for common SQL Server tasks.
4448
- Get-DefaultSqlServerName: Gets the name of the default local SQL Server instance.
45-
- Import-BacpacToSqlServer: Imports a .bacpac file to a database on a local SQL Server instance. With the attached Registry file you can also add an Explorer context menu shortcut to it.
49+
- Import-BacpacToSqlServer: Imports a .bacpac file to a database on a local SQL Server instance. With the attached Registry file you can also add an Explorer context menu shortcut to it. Use the Docker version if you need that, but make sure to edit the `-ConnectionString` value first!
4650
- New-SqlServerDatabase: Creates a new database on the given SQL Server instance, after dropping it first if it already exists.
4751
- Test-SqlServer: Tests the connection to a local SQL Server instance.
4852
- Test-SqlServerDatabase: Checks whether the given database exists in a local SQL Server instance.
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.Synopsis
3+
Creates a new SQL Server connection object.
4+
#>
5+
6+
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
7+
8+
function New-SqlServerConnection
9+
{
10+
[CmdletBinding()]
11+
Param
12+
(
13+
[Parameter(Mandatory = $true)]
14+
[string] $ServerName,
15+
16+
[string] $UserName = $null,
17+
18+
[string] $Password = $null
19+
)
20+
21+
Process
22+
{
23+
if (-not $UserName -or -not $Password)
24+
{
25+
New-Object -TypeName Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList $ServerName
26+
}
27+
else
28+
{
29+
New-Object -TypeName Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList $ServerName, $UserName, $Password
30+
}
31+
}
32+
}

SqlServer/New-SqlServerDatabase/New-SqlServerDatabase.psm1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ function New-SqlServerDatabase
1919
[Parameter(Mandatory = $true)]
2020
[string] $DatabaseName,
2121

22-
[switch] $Force
22+
[switch] $Force,
23+
24+
[string] $UserName = $null,
25+
26+
[string] $Password = $null
2327
)
2428

2529
Process
2630
{
27-
if (!(Test-SqlServer $SqlServerName))
28-
{
29-
throw ("Could not find SQL Server at `"$SqlServerName`"!")
30-
}
31-
32-
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $SqlServerName
31+
$serverConnection = New-SqlServerConnection $SqlServerName $UserName $Password
32+
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $serverConnection
3333

34-
if (Test-SqlServerDatabase -SqlServerName $SqlServerName -DatabaseName $DatabaseName)
34+
if (Test-SqlServerDatabase -SqlServerName $SqlServerName -DatabaseName $DatabaseName -UserName $UserName -Password $Password)
3535
{
3636
if ($Force.IsPresent)
3737
{

SqlServer/Test-SqlServer/Test-SqlServer.psm1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ function Test-SqlServer
1414
Param
1515
(
1616
[Parameter(Mandatory = $true)]
17-
[string] $ServerName
17+
[string] $ServerName,
18+
19+
[string] $UserName = $null,
20+
21+
[string] $Password = $null
1822
)
1923

2024
Process
2125
{
22-
return (New-Object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName).InstanceName -ne $null
26+
$Connection = New-SqlServerConnection $ServerName $UserName $Password
27+
28+
return (New-Object ("Microsoft.SqlServer.Management.Smo.Server") $Connection).InstanceName -ne $null
2329
}
2430
}

SqlServer/Test-SqlServerDatabase/Test-SqlServerDatabase.psm1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,31 @@ function Test-SqlServerDatabase
1717
[string] $SqlServerName,
1818

1919
[Parameter(Mandatory = $true)]
20-
[string] $DatabaseName
20+
[string] $DatabaseName,
21+
22+
[string] $UserName = $null,
23+
24+
[string] $Password = $null
2125
)
2226

2327
Process
2428
{
25-
if (!(Test-SqlServer $SqlServerName))
29+
if (!(Test-SqlServer $SqlServerName $UserName $Password))
2630
{
2731
throw ("Could not find SQL Server at `"$SqlServerName`"!")
2832
}
2933

30-
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $SqlServerName
31-
return ($server.Databases | Where-Object { $PSItem.Name -eq $DatabaseName }) -ne $null
34+
$server = New-SqlServerConnection $SqlServerName $UserName $Password
35+
$server.Connect()
36+
37+
# This works even for remote servers when $server.Databases returns empty.
38+
$databases = New-Object "System.Collections.Generic.HashSet[string]"
39+
$reader = $server.ExecuteReader("SELECT name FROM sys.databases")
40+
while ($reader.Read()) { $databases.Add($reader.GetString(0).ToUpperInvariant()) | Out-Null }
41+
42+
$reader.Close()
43+
$server.Disconnect()
44+
45+
return $databases.Contains($DatabaseName.ToUpperInvariant())
3246
}
3347
}

0 commit comments

Comments
 (0)