Version 1.0.1
This commit is contained in:
@@ -688,6 +688,83 @@ function Get-SQLDatabases {
|
||||
"
|
||||
|
||||
}
|
||||
|
||||
|
||||
############################################################
|
||||
# Initialize-VeeamSQLDatabase
|
||||
############################################################
|
||||
|
||||
function Initialize-VeeamSQLDatabase {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Legt die Veeam-Konfigurationsdatenbank vorab an und berechtigt
|
||||
das standardmäßige Veeam-Dienstkonto LocalSystem als db_owner.
|
||||
#>
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory)][string]$Server,
|
||||
[Parameter(Mandatory)][string]$Instance,
|
||||
[Parameter(Mandatory)][string]$Database
|
||||
)
|
||||
|
||||
if($Database -notmatch '^[A-Za-z0-9_]+$') {
|
||||
throw "Ungültiger SQL-Datenbankname: $Database"
|
||||
}
|
||||
|
||||
# Das integrierte LocalSystem-Konto ist sprachabhängig benannt
|
||||
# (z. B. NT-AUTORITÄT\SYSTEM auf einem deutschen Windows). Die SID ist
|
||||
# dagegen auf allen unterstützten Windows-Systemen identisch.
|
||||
$LocalSystemSid = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-18")
|
||||
$LocalSystemAccount = $LocalSystemSid.Translate([System.Security.Principal.NTAccount]).Value
|
||||
$AccountLiteral = $LocalSystemAccount.Replace("'", "''")
|
||||
$AccountIdentifier = $LocalSystemAccount.Replace("]", "]]" )
|
||||
|
||||
$CreateDatabaseQuery = @"
|
||||
IF SUSER_ID(N'$AccountLiteral') IS NULL
|
||||
BEGIN
|
||||
CREATE LOGIN [$AccountIdentifier] FROM WINDOWS;
|
||||
END;
|
||||
|
||||
IF DB_ID(@DatabaseName) IS NULL
|
||||
BEGIN
|
||||
CREATE DATABASE [$Database];
|
||||
END;
|
||||
"@
|
||||
|
||||
Invoke-SQLQuery `
|
||||
-Server $Server `
|
||||
-Instance $Instance `
|
||||
-Database "master" `
|
||||
-Query $CreateDatabaseQuery `
|
||||
-Parameters @{ DatabaseName = $Database } | Out-Null
|
||||
|
||||
$GrantDatabaseQuery = @"
|
||||
IF DATABASE_PRINCIPAL_ID(N'$AccountLiteral') IS NULL
|
||||
BEGIN
|
||||
CREATE USER [$AccountIdentifier] FOR LOGIN [$AccountIdentifier];
|
||||
END;
|
||||
|
||||
IF IS_ROLEMEMBER(N'db_owner', N'$AccountLiteral') <> 1
|
||||
BEGIN
|
||||
ALTER ROLE [db_owner] ADD MEMBER [$AccountIdentifier];
|
||||
END;
|
||||
"@
|
||||
|
||||
Invoke-SQLQuery `
|
||||
-Server $Server `
|
||||
-Instance $Instance `
|
||||
-Database $Database `
|
||||
-Query $GrantDatabaseQuery | Out-Null
|
||||
|
||||
return [PSCustomObject]@{
|
||||
Server = $Server
|
||||
Instance = $Instance
|
||||
Database = $Database
|
||||
ServiceAccount = $LocalSystemAccount
|
||||
DatabaseRole = "db_owner"
|
||||
Prepared = $true
|
||||
}
|
||||
}
|
||||
#Beginn PART 4
|
||||
############################################################
|
||||
# Test-DatabaseExists
|
||||
@@ -738,7 +815,7 @@ function Test-DatabaseExists {
|
||||
return (
|
||||
|
||||
$null -ne $Result -and
|
||||
$Result.Count -gt 0
|
||||
@($Result).Count -gt 0
|
||||
|
||||
)
|
||||
|
||||
@@ -1021,7 +1098,7 @@ function Test-SQLLoginExists {
|
||||
return (
|
||||
|
||||
$null -ne $Result -and
|
||||
$Result.Count -gt 0
|
||||
@($Result).Count -gt 0
|
||||
|
||||
)
|
||||
|
||||
@@ -1506,6 +1583,7 @@ Export-ModuleMember `
|
||||
New-SQLConnectionString,
|
||||
Test-SQLConnection,
|
||||
Invoke-SQLQuery,
|
||||
Initialize-VeeamSQLDatabase,
|
||||
Get-SQLDatabases,
|
||||
Test-DatabaseExists,
|
||||
New-SQLDatabase,
|
||||
|
||||
Reference in New Issue
Block a user