[KEERO-96] Written outbound mirroring endpoint initialization code.

This commit is contained in:
Dmitry Korotkov 2013-03-03 01:59:06 -08:00
parent 1903d22b15
commit 6ca53fd844
1 changed files with 123 additions and 0 deletions

View File

@ -2,6 +2,17 @@ Import-Module '.\OptionParser.psm1'
Import-Module '.\SQLServerOptionParsers.psm1'
function New-SQLServer {
<#
.SYNOPSIS
Installs new MS SQL Server instance
.DESCRIPTION
Installs new MS SQL Server instance in unattended mode.
.PARAMETER SetupRoot
MS SQL Server installation files root directory. Normally it is just DVD drive name.
#>
param(
[parameter(Mandatory = $true)]
[string]$SetupRoot
@ -19,6 +30,17 @@ function New-SQLServer {
}
function Remove-SQLServer {
<#
.SYNOPSIS
Uninstalls MS SQL Server instance installed with New-SQLServer cmdlet
.DESCRIPTION
Uninstalls MS SQL Server instance installed with New-SQLServer cmdlet in unattended mode
.PARAMETER SetupRoot
MS SQL Server installation files root directory. Normally it is just DVD drive name.
#>
param(
[parameter(Mandatory = $true)]
[string]$SetupRoot
@ -34,5 +56,106 @@ function Remove-SQLServer {
throw "Installation executable exited with code $("{0:X8}" -f $ExitCode)"
}
}
function Invoke-SQLText {
<#
.SYNOPSIS
Invokes SQL text
.DESCRIPTION
Invokes SQL text
.PARAMETER SQL
SQL Text
.PARAMETER User
SQL Server user name
.PARAMETER SQL
SQL Server user password
#>
param(
[parameter(Mandatory = $true)]
[string]$SQL,
[string]$User = $null,
[string]$Password = $null
)
$Binary = Get-Command "sqlcmd.exe"
$tempFile = [IO.Path]::GetTempFileName()
$tempFile = Get-Item $tempFile
Set-Content -Path $tempFile -Value $SQL
$CommandLine = @('-i', "`"$($tempFile.FullName)`"")
if (($User -ne $null) -and ($User -ne '')) {
$CommandLine = $CommandLine + '-U'
$CommandLine = $CommandLine + $User
$CommandLine = $CommandLine + '-P'
$CommandLine = $CommandLine + $Password
}
Write-Host "Executing: `"$($Binary.Path)`" $($CommandLine -join ' ')"
$process = [System.Diagnostics.Process]::Start($Binary, $CommandLine)
$process.WaitForExit()
$process.Refresh()
$ExitCode = $process.ExitCode
if ($ExitCode -ne 0) {
throw "SQLCMD.EXE returned with exit code $ExitCode"
}
Remove-Item $tempFile
}
function Initialize-MirroringEndpoint {
<#
.SYNOPSIS
Prepares SQL Server for database mirroring
.DESCRIPTION
ToDo: Describe
#>
param(
[parameter(Mandatory = $true)]
[String]$EncryptionPassword,
[parameter(Mandatory = $true)]
[String]$WorkDir
)
$Folder = Get-Item $WorkDir
$H = $Env:COMPUTERNAME -replace '[^A-Za-z0-9_]', ''
$CreateMasteeKey = "USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '$EncryptionPassword';
GO
CREATE CERTIFICATE ${H}_cert WITH SUBJECT = '$H certificate';
GO
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS TCP (
LISTENER_PORT=7024
, LISTENER_IP = ALL
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE ${H}_cert
, ENCRYPTION = REQUIRED ALGORITHM AES
, ROLE = ALL
);
GO
BACKUP CERTIFICATE ${H}_cert TO FILE = '$Folder\certificate.cer';
GO
"
Invoke-SQLText -SQL $CreateMasteeKey
}
Export-ModuleMember -Function New-SQLServer
Export-ModuleMember -Function Remove-SQLServer
Export-ModuleMember -Function Invoke-SQLText
Export-ModuleMember -Function Initialize-MirroringEndpoint