-
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 #68 from dcrreynolds/xSQLServerScript
Added xSQLServerScript Resource
- Loading branch information
Showing
5 changed files
with
438 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
DSCResources/MSFT_xSQLServerScript/MSFT_xSQLServerScript.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,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
172
DSCResources/MSFT_xSQLServerScript/MSFT_xSQLServerScript.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,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 | ||
|
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,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 |
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
Oops, something went wrong.