DSCResources/xSPTimerJob/xSPTimerJob.psm1

$SpPath = "C:\Program Files\WindowsPowerShell\Modules\SharePointDSC\Modules\SharePointDsc.Util\SharePointDsc.Util.psm1"
if (Test-path $SpPath -ErrorAction SilentlyContinue)
{Import-Module -Name $SpPath}
else 
{
    $script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
    $script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules'
    $script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DSCR_MIMSettings.Util'
    Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'Invoke-SPDscCommand.psm1')  
}


function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $TimerJob,

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $InstallAccount,

        [Parameter(Mandatory = $true)]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )

    Write-Verbose -Message "TimerJob Status for $TimerJob"
    $result = Invoke-SPDscCommand -Credential $InstallAccount `
        -Arguments $PSBoundParameters `
        -ScriptBlock {
        $params = $args[0]
        $Timer = Get-SPTimerJob -identity $params.TimerJob
        if ($Timer.IsDisabled -eq $true)
        {
            return @{
                Ensure                = "Absent"
                InstallAccount        = $params.InstallAccount
                TimerJob              = $Timer.JobDisplayName
                IsDisabled            = $Timer.IsDisabled
            }
            Write-Verbose -Message "Current TimerJob Status for $($params.TimerJob) is set to $($Timer.IsDisabled)"
        }
        else
        {
             return @{
                Ensure                = "Present"
                InstallAccount        = $params.InstallAccount
                TimerJob              = $Timer.JobDisplayName
                IsDisabled            = $Timer.IsDisabled
            }
            Write-Verbose -Message "Current TimerJob Status for $($params.TimerJob) is set to $($Timer.IsDisabled)"           
        }
    }
    return $result
}


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

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $InstallAccount,

        [Parameter(Mandatory = $true)]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )
    $PSBoundParameters.Ensure = $Ensure
    $currentValues = Get-TargetResource @PSBoundParameters
    if (($CurrentValues.IsDisabled -eq $false) -and ( $PSBoundParameters.Ensure -eq "Absent"))
    {
        Write-Verbose -Message ("Ensure, is set to ""Absent"" and IsDisabled is set to $($CurrentValues.IsDisabled). TimerJob Not in Desired State, Changing to ($true)")
        Invoke-SPDscCommand -Credential $InstallAccount `
            -Arguments $PSBoundParameters `
            -ScriptBlock {
                $params = $args[0]
                Get-SPTimerJob -identity $params.TimerJob | disable-SPTimerJob  
        }
        return Write-Verbose "SPTimerJob set to $($CurrentValues.IsDisabled), Completed."
    }
    else
    {
        Write-Verbose -Message ("Ensure, is set to ""Present"" and IsDisabled is set to $($CurrentValues.IsDisabled). TimerJob Not in Desired State, Changing to ($false)")
        Invoke-SPDscCommand -Credential $InstallAccount `
            -Arguments $PSBoundParameters `
            -ScriptBlock {
                $params = $args[0]
                Get-SPTimerJob -identity $params.TimerJob | Enable-SPTimerJob  
        }
        return Write-Verbose "SPTimerJob set to $($CurrentValues.IsDisabled), Completed."
    }
}


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

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $InstallAccount,

        [Parameter(Mandatory = $true)]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )

    $CurrentValues = Get-TargetResource @PSBoundParameters

    Write-Verbose -Message "Current Values: $($CurrentValues.isDisabled)"
    Write-Verbose -Message "Target Values: $(if ( $PSBoundParameters.Ensure -eq "Absent"){$true}else{$false})"

    $result =  invoke-SPDscCommand -ScriptBlock {
        $params = $args[0]
        $Timer = Get-SPTimerJob -identity $params.TimerJob
        if ($Timer.IsDisabled -eq $CurrentValues.IsDisabled){$true}Else{$false}
    }
    Write-Verbose -Message "Test-TargetResource returned $result"

    return $result
}


Export-ModuleMember -Function *-TargetResource