Cartwheel.TargetAction.psm1

#
# Cartwheel_TargetAction.psm1
#
function Test-Target {
    [OutputType([System.Collections.Hashtable])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet("Present", "Absent")]
        [string]$Ensure,

        [Parameter(Mandatory)]
        [bool]$TestTargetResourceResult,

        [Parameter(Mandatory)]
        [AllowEmptyString()]
        [string]$ItemErr,

        [Parameter(Mandatory)]
        [string]$ItemName
        )


    #our return object. default these for now.
    $info = @{
      "ReturnValue" = $false; #True,False
      "Command" = "Skip"; # Add,Remove,Skip,Error
    }






$modname = $MyInvocation.MyCommand.ModuleName
If ($Ensure -eq "Present")
    {

            If($testTargetResourceResult -and (-not $ItemErr))
           {
                Write-Verbose "$modname : Item ($ItemName) exists. Expected present"
                Write-Verbose "$modname Set-TargetResource : (skipping)"
                $info.ReturnValue = $true
                $info.Command = "Skip"
                return $info
            }
            Else
            {
                if ($ItemErr)
                {
                    Write-Verbose  "$modname : $($ItemErr.exception.message)"
                    $info.ReturnValue = $false
                    $info.Command = "Error"
                    return $info
                }
                Write-Verbose   "$modname : Item ($ItemName) absent. Expected present"
                $info.ReturnValue = $false
                $info.Command = "Add"
                return $info
            }

        }
    Else
    {
        #$Ensure -eq "Absent"
        If($testTargetResourceResult -and (-not $ItemErr))
        {

            Write-Verbose "$modname : Item ($ItemName) exists. Expected absent"
            $info.ReturnValue = $false
            $info.Command = "Remove"
            return $info
        }
        Else
        {
            if ($ItemErr)
            {
                    Write-Verbose  "$modname : $($ItemErr.exception.message)"
                    $info.ReturnValue = $false
                    $info.Command = "Error"
                    return $info
            }
            Write-Verbose  "$modname : Item ($ItemName) absent. Expected absent"
            Write-Verbose  "$modname Set-TargetResource : (skipping)"
            $info.ReturnValue = $true
            $info.Command = "Skip"
            return $info
        }

    }


    }



function Test-TargetItem([string] $ItemPath, [string] $Ensure)
{
    $testTargetResourceResult = Get-Item -path $ItemPath  -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;
    $exists = $false
    if ($testTargetResourceResult){$exists = $true}
    $test = Test-Target $Ensure $exists "" $ItemPath
    return $test
}    

function Test-TargetDirectory([string] $DirectoryPath, [string] $Ensure)
{
    $testTargetResourceResult = Test-Path -PathType Container -path $DirectoryPath  -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;
    $exists = $false
    if ($testTargetResourceResult){$exists = $true}
    $test = Test-Target $Ensure $exists "" $DirectoryPath
    return $test
}