Skip to content

Commit

Permalink
Merge pull request #68 from dcrreynolds/xSQLServerScript
Browse files Browse the repository at this point in the history
Added xSQLServerScript Resource
  • Loading branch information
mbreakey3 authored Aug 22, 2016
2 parents d906654 + c4cbb31 commit d934afa
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

[ClassVersion("1.0.0.0"), FriendlyName("xSQLServerScript")]
class MSFT_xSQLServerScript : OMI_BaseResource
{
[Required, Description("The name of an instance of the Database Engine. For default instances, only specify the computer name. For named instances, use the format ComputerName\\InstanceName")] String ServerInstance;
[Key, Description("Path to SQL file that will perform Set action.")] String SetFilePath;
[Key, Description("Path to SQL file that will perform Get action.")] String GetFilePath;
[Key, Description("Path to SQL file that will perform Test action.")] String TestFilePath;
[Write, EmbeddedInstance("MSFT_Credential"), Description("Credential to be used to run SQL scripts.")] String Credential;
[Write, Description("Creates a sqlcmd scripting variable for use in the sqlcmd script, and sets a value for the variable.")] String Variable[];
[Read, Description("Result of Get action.")] String GetResult[];
};

172 changes: 172 additions & 0 deletions DSCResources/MSFT_xSQLServerScript/MSFT_xSQLServerScript.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
$currentPath = Split-Path -Parent $MyInvocation.MyCommand.Path
Write-Verbose -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)]
[System.String]
$ServerInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SetFilePath,

[Parameter(Mandatory = $true)]
[System.String]
$GetFilePath,

[Parameter(Mandatory = $true)]
[System.String]
$TestFilePath,

[System.Management.Automation.PSCredential]
$Credential,

[System.String[]]
$Variable
)

$result = Invoke-SqlScript -ServerInstance $ServerInstance -SqlScriptPath $GetFilePath `
-Credential $Credential -Variable $Variable -ErrorAction Stop

$getResult = Out-String -InputObject $result

$returnValue = @{
ServerInstance = [System.String] $ServerInstance
SetFilePath = [System.String] $SetFilePath
GetFilePath = [System.String] $GetFilePath
TestFilePath = [System.String] $TestFilePath
Username = [System.Management.Automation.PSCredential] $Credential
Variable = [System.String[]] $Variable
GetResult = [System.String[]] $getresult
}

$returnValue
}

function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ServerInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SetFilePath,

[Parameter(Mandatory = $true)]
[System.String]
$GetFilePath,

[Parameter(Mandatory = $true)]
[System.String]
$TestFilePath,

[System.Management.Automation.PSCredential]
$Credential,

[System.String[]]
$Variable
)

Invoke-SqlScript -ServerInstance $ServerInstance -SqlScriptPath $SetFilePath `
-Credential $Credential -Variable $Variable -ErrorAction Stop
}


function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ServerInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SetFilePath,

[Parameter(Mandatory = $true)]
[System.String]
$GetFilePath,

[Parameter(Mandatory = $true)]
[System.String]
$TestFilePath,

[System.Management.Automation.PSCredential]
$Credential,

[System.String[]]
$Variable
)

try
{
$result = Invoke-SqlScript -ServerInstance $ServerInstance -SqlScriptPath $TestFilePath `
-Credential $Credential -Variable $Variable -ErrorAction Stop

if($result -eq $null)
{
return $true
}
else
{
return $false
}
}
catch [Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException]
{
Write-Verbose $_
return $false
}
}

function Invoke-SqlScript
{
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ServerInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SqlScriptPath,

[System.Management.Automation.PSCredential]
$Credential,

[System.String[]]
$Variable
)

Import-SQLPSModule

if($null -ne $Credential)
{
$null = $PSBoundParameters.Add("Username", $Credential.UserName)
$null = $PSBoundParameters.Add("Password", $Credential.GetNetworkCredential().password)
}

$null = $PSBoundParameters.Remove("Credential")
$null = $PSBoundParameters.Remove("SqlScriptPath")

Invoke-Sqlcmd -InputFile $SqlScriptPath @PSBoundParameters
}

Export-ModuleMember -Function *-TargetResource

28 changes: 28 additions & 0 deletions Examples/SQLServerScript.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
configuration SQLSettings
{
Import-DscResource -ModuleName 'xSQLServer'

Node 'localhost'
{
xSQLServerScript SqlSettings
{
ServerInstance = "$env:COMPUTERNAME\SMA"
SetFilePath = "C:\temp\Set-SQlsettings.sql"
TestFilePath = "C:\temp\Test-SQlsettings.sql"
GetFilePath = "C:\temp\Get-SQlsettings.sql"
Variable = @("FilePath=C:\temp\log\AuditFiles")
}
}
}

$configData = @{
AllNodes = @(
@{
NodeName = 'localhost'
}
)
}

SQLSettings -ConfigurationData $configData

Start-DscConfiguration -Path .\SQLSettings -Wait -Force -Verbose
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **xSQLServerEndpointPermission** Grant or revoke permission on the endpoint.
* **xSQLServerAvailabilityGroupListener** Create or remove an availability group listener.
* **xSQLServerReplication** resource to manage SQL Replication distribution and publishing.
* **xSQLServerScript** resource to extend DSCs Get/Set/Test functionality to T-SQL

### xSQLServerSetup

Expand Down Expand Up @@ -318,11 +319,20 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **UseTrustedConnection**: (Default = $true) Publisher security mode.
* **UninstallWithForce**: (Default = $true) Force flag for uninstall procedure

###xSQLServerScript
* **ServerInstance**: (Required) The name of an instance of the Database Engine. For default instances, only specify the computer name. For named instances, use the format ComputerName\\InstanceName.
* **SetFilePath**: (Key) Path to SQL file that will perform Set action.
* **GetFilePath**: (Key) Path to SQL file that will perform Get action. SQL Queries returned by this function are returned by the Get-DscConfiguration cmdlet with the GetResult parameter.
* **TestFilePath**: (Key) Path to SQL file that will perform Test action. Any Script that does not throw an error and returns null is evaluated to true. Invoke-SqlCmd treats SQL Print statements as verbose text, this will not cause a Test to return false.
* **Credential**: Specifies the credentials for making a SQL Server Authentication connection to an instance of the Database Engine.
* **Variable**: Creates a sqlcmd scripting variable for use in the sqlcmd script, and sets a value for the variable.

## Versions

### Unreleased
* Added resources
- xSQLServerReplication
- xSQLServerScript
* Added tests for resources
- xSQLServerPermission
- xSQLServerEndpointState
Expand All @@ -335,8 +345,9 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
- Fixed the issue when trying to add a static IP to a listener was ignored.
* Fixes in xSQLAOGroupEnsure
- Added parameters to New-ListenerADObject to allow usage of a named instance.

### 1.8.0.0

* Converted appveyor.yml to install Pester from PSGallery instead of from Chocolatey.
* Added Support for SQL Server 2016
* xSQLAOGroupEnsure
Expand Down Expand Up @@ -473,3 +484,4 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
## Examples

Examples for use of this resource can be found with the System Center resources, such as **xSCVMM**, **xSCSMA**, and **xSCOM**.

Loading

0 comments on commit d934afa

Please sign in to comment.