DSCResources/Cartwheel_xSqlServer/Cartwheel_xSqlServer.psm1

#
# Cartwheel_xBlank.psm1
# xBlank is a simplified boilerplate template for new modules
#
function WaitUntilServices($searchString, $status)
{
    # Get all services where DisplayName matches $searchString and loop through each of them.
    foreach($service in (Get-Service -Name $searchString))
    {
        # Wait for the service to reach the $status or a maximum of 30 seconds
        $service.WaitForStatus($status, '00:00:30')
    }
}


function Test-TargetResource 
{
    param 
    (    
    
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstanceName, 

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstallMediaLocation, 

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$SysAdminAccts, 

        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"
     
    )


  


# gather our installed sql server instances
$Instances = Get-SqlInstance
$testTargetResourceResult = $Instances | ? {$_ -eq $InstanceName}
if ($testTargetResourceResult) {$testTarget = $true} else {$testTarget = $false}
$test = Test-Target $Ensure $testTarget $ItemErr $InstanceName
return $test.ReturnValue






}






# The Set-TargetResource function is used to create, delete or configure on the target machine.
function Set-TargetResource 
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param 
    (       
    
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstanceName, 

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstallMediaLocation, 

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$SysAdminAccts, 

        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"

    )

  




$modname = $MyInvocation.MyCommand.ModuleName
$Instances = Get-SqlInstance
$testTargetResourceResult = $Instances | ? {$_.Name -match "$InstanceName$"}
if ($testTargetResourceResult) {$testTarget = $true} else {$testTarget = $false}
$test = Test-Target $Ensure $testTarget $ItemErr $InstanceName


if ($test.Command -eq "Add") {
        Add-SqlInstance -FEATURES "SQLENGINE" -INSTANCENAME $InstanceName -SQLSYSADMINAccounts $SysAdminAccts -MediaLocation $InstallMediaLocation
        Write-Verbose  "$modname : Item ($InstanceName) in wrong state... added."
    }
    if ($test.Command -eq "Remove") {
        Remove-SqlInstance -FEATURES "SQLENGINE" -INSTANCENAME $InstanceName -MediaLocation $InstallMediaLocation
        Write-Verbose  "$modname : Item ($InstanceName) in wrong state... removed."
    }
    if ($test.Command -eq "Skip") {
        if($testTargetResourceResult.State -eq "Stopped") {
            Start-Service -Name $testTargetResourceResult.Name
            WaitUntilServices $testTargetResourceResult.Name "Running"
            Write-Verbose  "$modname : Item ($InstanceName) service is stopped. Starting Service."
        }
        Write-Verbose  "$modname : Item ($InstanceName) in correct state."
    }
    if ($test.Command -eq "Error") {
        Write-Verbose  "$modname : Item ($InstanceName) had errors."
    }

return $test.ReturnValue

    







}







function Get-TargetResource 
{
    param 
    (    
    
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstanceName, 

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstallMediaLocation,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$SysAdminAccts, 

        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"
     
    )


    Write-Verbose  "$modname : Running Test for ($InstanceName)."
    Test-TargetResource $InstanceName $InstallMediaLocation $SysAdminAccts $Ensure



}


Export-ModuleMember -Function *-TargetResource