Skip to content

Commit

Permalink
Merge pull request #29 from PowerShell/dev
Browse files Browse the repository at this point in the history
Release of version 1.4.0.0 of xSqlServer
  • Loading branch information
KarolKaczmarek committed Feb 3, 2016
2 parents 8eab5f3 + 5b45d64 commit e3eecb9
Show file tree
Hide file tree
Showing 40 changed files with 4,154 additions and 139 deletions.
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

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;
};
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
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;
};
Loading

0 comments on commit e3eecb9

Please sign in to comment.