-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from PowerShell/dev
Release of version 1.4.0.0 of xSqlServer
- Loading branch information
Showing
40 changed files
with
4,154 additions
and
139 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
DSCResources/MSFT_xSQLDatabaseRecoveryModel/MSFT_xSQLDatabaseRecoveryModel.psm1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
$currentPath = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
Write-Debug -Message "CurrentPath: $currentPath" | ||
|
||
# Load Common Code | ||
Import-Module $currentPath\..\..\xSQLServerHelper.psm1 -Verbose:$false -ErrorAction Stop | ||
|
||
function Get-TargetResource | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Collections.Hashtable])] | ||
param | ||
( | ||
[parameter(Mandatory = $true)] | ||
[ValidateSet("Full","Simple","BulkLogged")] | ||
[System.String] | ||
$RecoveryModel = "Full", | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$SqlServerInstance, | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$DatabaseName | ||
) | ||
|
||
$SqlServerInstance = $SqlServerInstance.Replace('\MSSQLSERVER','') | ||
Write-Verbose -Message "Checking Database $DatabaseName recovery mode for $RecoveryModel." -Verbose | ||
|
||
$db = Get-SqlDatabase -ServerInstance $SqlServerInstance -Name $DatabaseName | ||
$value = ($db.RecoveryModel -eq $RecoveryModel) | ||
Write-Verbose -Message "Database $DatabaseName recovery mode comparison $value." -Verbose | ||
|
||
$returnValue = @{ | ||
RecoveryModel = $db.RecoveryModel | ||
SqlServerInstance = $SqlServerInstance | ||
DatabaseName = $DatabaseName | ||
} | ||
|
||
$returnValue | ||
} | ||
|
||
|
||
function Set-TargetResource | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[parameter(Mandatory = $true)] | ||
[ValidateSet("Full","Simple","BulkLogged")] | ||
[System.String] | ||
$RecoveryModel = "Full", | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$SqlServerInstance, | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$DatabaseName | ||
) | ||
|
||
$SqlServerInstance = $SqlServerInstance.Replace('\MSSQLSERVER','') | ||
$db = Get-SqlDatabase -ServerInstance $SqlServerInstance -Name $DatabaseName | ||
Write-Verbose -Message "Database $DatabaseName recovery mode is $db.RecoveryModel." -Verbose | ||
|
||
if($db.RecoveryModel -ne $RecoveryModel) | ||
{ | ||
Write-Verbose -Message "Changing $DatabaseName recovery mode to $RecoveryModel." -Verbose | ||
$db.RecoveryModel = $RecoveryModel; | ||
$db.Alter(); | ||
Write-Verbose -Message "DB $DatabaseName recovery mode is changed to $RecoveryModel." -Verbose | ||
} | ||
|
||
if(!(Test-TargetResource @PSBoundParameters)) | ||
{ | ||
throw New-TerminatingError -ErrorType TestFailedAfterSet -ErrorCategory InvalidResult | ||
} | ||
} | ||
|
||
|
||
function Test-TargetResource | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Boolean])] | ||
param | ||
( | ||
[parameter(Mandatory = $true)] | ||
[ValidateSet("Full","Simple","BulkLogged")] | ||
[System.String] | ||
$RecoveryModel = "Full", | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$SqlServerInstance, | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$DatabaseName | ||
) | ||
$SqlServerInstance = $SqlServerInstance.Replace('\MSSQLSERVER','') | ||
$result = ((Get-TargetResource @PSBoundParameters).RecoveryModel -eq $RecoveryModel) | ||
|
||
$result | ||
} | ||
|
||
|
||
Export-ModuleMember -Function *-TargetResource | ||
|
8 changes: 8 additions & 0 deletions
8
DSCResources/MSFT_xSQLDatabaseRecoveryModel/MSFT_xSQLDatabaseRecoveryModel.schema.mof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
[ClassVersion("1.0"), FriendlyName("xSQLDatabaseRecoveryModel")] | ||
class MSFT_xSQLDatabaseRecoveryModel : OMI_BaseResource | ||
{ | ||
[Key, Description("The SQL database name")] String DatabaseName; | ||
[Required, Description("The SQL server and instance.")] String SqlServerInstance; | ||
[Required, Description("Recovery Model"), ValueMap{"Full","Simple","BulkLogged"}, Values{"Full","Simple","BulkLogged"}] String RecoveryModel; | ||
}; |
165 changes: 165 additions & 0 deletions
165
DSCResources/MSFT_xSQLServerDatabaseOwner/MSFT_xSQLServerDatabaseOwner.psm1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
$currentPath = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
Write-Debug -Message "CurrentPath: $currentPath" | ||
|
||
# Load Common Code | ||
Import-Module $currentPath\..\..\xSQLServerHelper.psm1 -Verbose:$false -ErrorAction Stop | ||
|
||
# DSC resource to manage SQL database roles | ||
|
||
# NOTE: This resource requires WMF5 and PsDscRunAsCredential | ||
|
||
function ConnectSQL | ||
{ | ||
param | ||
( | ||
[System.String] | ||
$SQLServer = $env:COMPUTERNAME, | ||
|
||
[System.String] | ||
$SQLInstanceName = "MSSQLSERVER" | ||
) | ||
|
||
$null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') | ||
|
||
if($SQLInstanceName -eq "MSSQLSERVER") | ||
{ | ||
$ConnectSQL = $SQLServer | ||
} | ||
else | ||
{ | ||
$ConnectSQL = "$SQLServer\$SQLInstanceName" | ||
} | ||
|
||
Write-Verbose "Connecting to SQL $ConnectSQL" | ||
$SQL = New-Object Microsoft.SqlServer.Management.Smo.Server $ConnectSQL | ||
|
||
if($SQL) | ||
{ | ||
Write-Verbose "Connected to SQL $ConnectSQL" | ||
$SQL | ||
} | ||
else | ||
{ | ||
Write-Verbose "Failed connecting to SQL $ConnectSQL" | ||
} | ||
} | ||
|
||
|
||
function Get-TargetResource | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Collections.Hashtable])] | ||
param | ||
( | ||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$Database, | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$Name, | ||
|
||
[System.String] | ||
$SQLServer = $env:COMPUTERNAME, | ||
|
||
[System.String] | ||
$SQLInstanceName = "MSSQLSERVER" | ||
) | ||
|
||
if(!$SQL) | ||
{ | ||
$SQL = ConnectSQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName | ||
} | ||
|
||
if($SQL) | ||
{ | ||
# Check database exists | ||
if(!($SQLDatabase = $SQL.Databases[$Database])) | ||
{ | ||
throw New-TerminatingError -ErrorType NoDatabase -FormatArgs @($Database,$SQLServer,$SQLInstanceName) -ErrorCategory InvalidResult | ||
} | ||
|
||
$Name = $SQLDatabase.Owner | ||
} | ||
else | ||
{ | ||
$Name = $null | ||
} | ||
|
||
$returnValue = @{ | ||
Database = $Database | ||
Name = $Name | ||
SQLServer = $SQLServer | ||
SQLInstanceName = $SQLInstanceName | ||
} | ||
|
||
$returnValue | ||
} | ||
|
||
|
||
function Set-TargetResource | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$Database, | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$Name, | ||
|
||
[System.String] | ||
$SQLServer = $env:COMPUTERNAME, | ||
|
||
[System.String] | ||
$SQLInstanceName = "MSSQLSERVER" | ||
) | ||
|
||
if(!$SQL) | ||
{ | ||
$SQL = ConnectSQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName | ||
} | ||
|
||
if($SQL) | ||
{ | ||
$SQLDatabase = $SQL.Databases[$Database] | ||
$SQLDatabase.SetOwner($Name) | ||
} | ||
|
||
if(!(Test-TargetResource @PSBoundParameters)) | ||
{ | ||
throw New-TerminatingError -ErrorType TestFailedAfterSet -ErrorCategory InvalidResult | ||
} | ||
} | ||
|
||
|
||
function Test-TargetResource | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Boolean])] | ||
param | ||
( | ||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$Database, | ||
|
||
[parameter(Mandatory = $true)] | ||
[System.String] | ||
$Name, | ||
|
||
[System.String] | ||
$SQLServer = $env:COMPUTERNAME, | ||
|
||
[System.String] | ||
$SQLInstanceName = "MSSQLSERVER" | ||
) | ||
|
||
$result = ((Get-TargetResource @PSBoundParameters).Name -eq $Name) | ||
|
||
$result | ||
} | ||
|
||
|
||
Export-ModuleMember -Function *-TargetResource |
8 changes: 8 additions & 0 deletions
8
DSCResources/MSFT_xSQLServerDatabaseOwner/MSFT_xSQLServerDatabaseOwner.schema.mof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ClassVersion("1.0.0.0"), FriendlyName("xSQLServerDatabaseOwner")] | ||
class MSFT_xSQLServerDatabaseOwner : OMI_BaseResource | ||
{ | ||
[Key, Description("The SQL database.")] String Database; | ||
[Required, Description("The name of the SQL login for the owner.")] String Name; | ||
[Write, Description("The SQL Server for the database.")] String SQLServer; | ||
[Write, Description("The SQL instance for the database.")] String SQLInstanceName; | ||
}; |
Oops, something went wrong.