PembrokePSqman.psm1

Write-Verbose 'Importing from [C:\projects\pembrokepsqman\PembrokePSqman\private]'
# .\PembrokePSqman\private\Get-ActiveWmanTaskSet.ps1
function Get-ActiveWmanTaskSet {
    <#
    .DESCRIPTION
        This function will gather the cancelled tasks for the specified TableName.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .PARAMETER WmanId
        A WmanId is Required.
    .EXAMPLE
        Get-ActiveWmanTaskSet -RestServer localhost -TableName tasks -WmanId 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][int]$WmanId,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            # Checking for tasks that are (Assigned(7),Running(8),Cancelled(10),Staged(14))
            Write-LogLevel -Message "Checking for tasks that are (Assigned(7),Running(8),Cancelled(10),Staged(14)) in table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            $TableName = $TableName.ToLower()
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$TableName" + "?&filter[]=STATUS_ID,eq,7&filter[]=STATUS_ID,eq,8&filter[]=STATUS_ID,eq,10&filter[]=STATUS_ID,eq,14&transform=1&satisfy=any"
            Write-LogLevel -Message "The Url is $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $ActiveWmanTasks = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-ActiveWmanTaskSet: $ErrorMessage $FailedItem"
        }
        $ActiveWmanTasks
    } else {
        Throw "Get-ActiveWmanTaskSet: Unable to reach Rest server $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-AvailableWmanSet.ps1
function Get-AvailableWmanSet {
    <#
    .DESCRIPTION
        This function will gather available Workflow Managers that are of a specified type.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER Wman_Type
        A Wman_Type is required.
    .EXAMPLE
        Get-AvailableWmanSet -RestServer localhost -Wman_Type 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][int]$Wman_Type
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-LogLevel -Message "Gathering available Workflow Managers with Type: $Wman_Type" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/workflow_manager?filter[]=status_id,eq,2&filter[]=WORKFLOW_MANAGER_TYPE_ID,eq,$Wman_Type&transform=1"
            Write-LogLevel -Message "the URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $WmanStatusData = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-AvailableWmanSet: $ErrorMessage $FailedItem"
        }
        $WmanStatusData
    } else {
        Throw "Get-AvailableWmanSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-AvailableWmanType.ps1
function Get-AvailableWmanType {
    <#
    .DESCRIPTION
        This function will get the Workflow Manager Type based on the specified task type.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TaskTypeId
        A TaskTypeId is required.
    .EXAMPLE
        Get-AvailableWmanType -RestServer localhost -TaskTypeId 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][int]$TaskTypeId
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-LogLevel -Message "Gathering Available Wman type based on task type: $TaskTypeId" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/wman_task_types" + "?filter[]=TASK_TYPE_ID,eq,$TaskTypeId&filter[]=STATUS_ID,eq,11&transform=1"
            Write-LogLevel -Message "the URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $WmanTypeData = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-AvailableWmanType: $ErrorMessage $FailedItem"
        }
        $WmanTypeData
    } else {
        Throw "Get-AvailableWmanType: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-CancelledTaskSet.ps1
function Get-CancelledTaskSet {
    <#
    .DESCRIPTION
        This function will gather the cancelled tasks for the specified TableName.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Get-CancelledTaskSet -RestServer localhost -TableName tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            Write-LogLevel -Message "Gathering cancelled tasks from table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$TableName" + "?filter=STATUS_ID,eq,10&transform=1"
            Write-LogLevel -Message "the URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $CancelledTasks = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-CancelledTaskSet: $ErrorMessage $FailedItem"
        }
        $CancelledTasks
    } else {
        Throw "Get-CancelledTaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-QmanStatus.ps1
function Get-QmanStatus {
    <#
    .DESCRIPTION
        This function will gather Status information from PembrokePS web/rest for a Queue_Manager
    .PARAMETER ComponentId
        An ID is required.
    .PARAMETER RestServer
        A Rest Server is required.
    .EXAMPLE
        Get-QmanStatus -ComponentId 1 -RestServer localhost
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][int]$ComponentId,
        [Parameter(Mandatory=$true)][string]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-LogLevel -Message "Getting the Queue_Manager via 'Get-ComponentStatus" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            $ComponentStatusData = (Get-ComponentStatus -ComponentType Queue_Manager -ComponentId $ComponentId -RestServer $RestServer).queue_manager
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-QmanStatus: $ErrorMessage $FailedItem"
        }
        $ComponentStatusData
    } else {
        Throw "Get-QmanStatus: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-QmanTableName.ps1
function Get-QmanTableName {
    <#
    .DESCRIPTION
        This function will gather The tableName for the Queue Manager type.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER Type_ID
        A Queue Manager Type_ID is required.
    .EXAMPLE
        Get-QmanTableName -RestServer localhost -Type_ID 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][int]$Type_ID
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-LogLevel -Message "Getting the TableName from: queue_Manager_type table." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/queue_manager_type/$Type_ID"
            Write-LogLevel -Message "the URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $TableName = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-QmanTableName: $ErrorMessage $FailedItem"
        }
        $TableName
    } else {
        Throw "Get-QmanTableName: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-QueuedTaskSet.ps1
function Get-QueuedTaskSet {
    <#
    .DESCRIPTION
        This function will gather the Queued tasks for the specified TableName.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Get-QueuedTaskSet -RestServer localhost -TableName tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            Write-LogLevel -Message "Gathering Queued tasks from table: $TableName." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$TableName" + "?filter=STATUS_ID,eq,6&transform=1"
            Write-LogLevel -Message "the URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $SubmittedTasks = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-QueuedTaskSet: $ErrorMessage $FailedItem"
        }
        $SubmittedTasks
    } else {
        Throw "Get-QueuedTaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Get-SubmittedTaskSet.ps1
function Get-SubmittedTaskSet {
    <#
    .DESCRIPTION
        This function will gather The submitted tasks for the specified TableName.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Get-SubmittedTaskSet -RestServer localhost -TableName tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            Write-LogLevel -Message "Gathering Submitted tasks for table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$TableName" + "?filter=STATUS_ID,eq,5&transform=1"
            Write-LogLevel -Message "the URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $SubmittedTasks = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-SubmittedTaskSet: $ErrorMessage $FailedItem"
        }
        $SubmittedTasks
    } else {
        Throw "Get-SubmittedTaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Invoke-AbortCancelledTaskSet.ps1
function Invoke-AbortCancelledTaskSet {
    <#
    .DESCRIPTION
        This function will Set any cancelled task to aborted.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Invoke-AbortCancelledTaskSet -RestServer localhost -TableName tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([Int])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            # Get a list of Submitted tasks
            $TableName = $TableName.ToLower()
            $CancelledTasks = (Get-CancelledTaskSet -RestServer $RestServer -TableName $TableName).$TableName
            $CancelledTasksCount = ($CancelledTasks | Measure-Object).count
            Write-LogLevel -Message "Aborting: $CancelledTasksCount, in table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel info
            if($CancelledTasksCount -gt 0) {
                foreach($Task in $CancelledTasks){
                    # Foreach task, set it to Complete/Aborted.
                    $TaskId = $Task.ID
                    $body = @{STATUS_ID = "9"
                                RESULT_ID = "5"
                                WORKFLOW_MANAGER_ID = "9999"
                            }
                    Write-LogLevel -Message "Aborting task: $TaskId, table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
                    $RestReturn = Invoke-UpdateTaskTable -RestServer $RestServer -TableName $TableName -TaskID $TaskId -Body $body
                }
            } else {
                # No tasks to queue
            }
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-AbortCancelledTaskSet: $ErrorMessage $FailedItem"
        }
        $RestReturn
    } else {
        Throw "Invoke-AbortCancelledTaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Invoke-AssignTask.ps1
function Invoke-AssignTask {
    <#
    .DESCRIPTION
        This function will Assign a task to a specific WorkFlow Manager.
    .PARAMETER TaskId
        A TaskId is required.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER WmanId
        A WmanId is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Invoke-AssignTask -RestServer localhost -TaskId 1 -WmanId 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([Int])]
    param(
        [Parameter(Mandatory=$true)][int]$TaskId,
        [Parameter(Mandatory=$true)][int]$WmanId,
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            $body = @{STATUS_ID = "7"
                        WORKFLOW_MANAGER_ID = "$WmanId"
                    }
            Write-LogLevel -Message "Assigning task: $TaskId, to Wman: $WmanId, table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$TableName/$TaskId"
            Write-LogLevel -Message "URL is: $URL" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            $RestReturn = Invoke-RestMethod -Method Put -Uri "$URL" -body $body
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-AssignTask: $ErrorMessage $FailedItem"
        }
        $RestReturn
    } else {
        Throw "Invoke-AssignTask: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Invoke-Qman.ps1
function Invoke-Qman {
    <#
    .DESCRIPTION
        This function will gather Status information from PembrokePS web/rest for a Queue_Manager
    .PARAMETER PropertyFilePath
        A properties path is Required.
    .EXAMPLE
        Invoke-Qman -PropertyFilePath "c:\PembrokePS\qman\pembrokeps.properties"
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$PropertyFilePath
    )
    if (Test-Path -Path $PropertyFilePath) {
        # Gather Local Properties for the Queue Manager
        $PpsProperties = Get-LocalPropertySet -PropertyFilePath $PropertyFilePath
        $RestServer = $PpsProperties.'system.RestServer'
        $SystemRoot = $PpsProperties.'system.root'
        $ID = $PpsProperties.'component.Id'
        $RunLogLevel = $PpsProperties.'component.RunLogLevel'
        $LOG_FILE = $PpsProperties.'component.logfile'
        Write-LogLevel -Message "Gathering Local Properties from: $PropertyFilePath, SystemRoot: $SystemRoot" -Logfile "$LOG_FILE" -RunLogLevel CONSOLEONLY -MsgLevel CONSOLEONLY
     } else {
        Write-LogLevel -Message "Unable to Locate Local properties file: $PropertyFilePath." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel ERROR
        Throw "Invoke-Qman: Unable to Locate Properties file."
    }
    try
    {
        $script:QmanRunning = "Running"
        do {
            # Test connection with the Database Server
            Write-LogLevel -Message "Starting Invoke-Qman loop" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            if (Test-Connection -Count 1 $RestServer -Quiet) {
                # No Action needed if the RestServer can be reached.
            } else {
                $script:QmanRunning = "Shutdown"
                Write-LogLevel -Message "Unable to reach RestServer: $RestServer." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel ERROR
                Throw "Unable to reach Rest server."
            }
            Write-LogLevel -Message "Validated Connection to RestServer." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO 
            # Get the Status and Queue Manager Specific Information from the Database
            $QmanStatusData = Get-QmanStatus -ComponentId $ID -RestServer $RestServer
            $QueueManagerStatus = $QmanStatusData.STATUS_ID
            $LOG_FILE = $QmanStatusData.LOG_FILE
            $ManagerWait = $QmanStatusData.WAIT
            $QUEUE_MANAGER_TYPE_ID = $QmanStatusData.QUEUE_MANAGER_TYPE_ID
            Write-LogLevel -Message "Get-QmanStatus is complete" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
            $TableName = (Get-QmanTableName -RestServer $RestServer -Type_ID $QUEUE_MANAGER_TYPE_ID).TABLENAME
            Write-LogLevel -Message "Get QmanTablename is complete, TableName: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            # Based on the Status Perform Specific actions
            Write-LogLevel -Message "QueueManager ID: $ID" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
            Write-LogLevel -Message "QueueManager Status: $QueueManagerStatus" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
            Write-LogLevel -Message "QueueManager TableName : $TableName" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
            Write-LogLevel -Message "QueueManager Wait: $ManagerWait" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
            Write-LogLevel -Message "QueueManager LogFile: $LOG_FILE" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
            if ($QueueManagerStatus -eq 1) {
                # Down - Not doing Anything
                Write-LogLevel -Message "Get-QmanStatus is Down, Not taking Action." -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel ERROR 
            } elseif ($QueueManagerStatus -eq 2) {
                # Up - Perform normal Tasks
                Write-LogLevel -Message "Get-QmanStatus is UP, performing Normal Operations" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
                Write-LogLevel -Message "Aborting Cancelled Tasks" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
                Invoke-AbortCancelledTaskSet -RestServer $RestServer -TableName $TableName
                Invoke-Wait -Seconds 5
                Write-LogLevel -Message "Queuing Submitted Tasks" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
                Invoke-QueueSubmittedTaskSet -RestServer $RestServer -TableName $TableName 
                Invoke-Wait -Seconds 5
                Write-LogLevel -Message "Reviewing Queued Tasks" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
                Invoke-ReviewQueuedTaskSet -RestServer $RestServer -TableName $TableName
                Write-LogLevel -Message "Normal Operations Completed." -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
            } elseif ($QueueManagerStatus -eq 3) {
                # Starting Up - Perform startup Tasks
                Write-LogLevel -Message "Performting Startup Tasks" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
                Invoke-QmanStartupTaskSet -TableName $TableName -RestServer $RestServer -ID $ID
                Write-LogLevel -Message "Startup Tasks Completed." -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            } elseif ($QueueManagerStatus -eq 4) {
                # Shutting Down - Perform Shutdown Tasks
                Write-LogLevel -Message "Performing Shutdown tasks." -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel INFO 
                Invoke-QmanShutdownTaskSet -TableName $TableName -RestServer $RestServer -ID $ID
                $script:QmanRunning = "Shutdown"
                Write-LogLevel -Message "Shutdown tasks completed." -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
            }
            Write-LogLevel -Message "Manager running String: $script:QmanRunning" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel TRACE 
            if($script:QmanRunning -ne "Shutdown"){
                Write-LogLevel -Message "Waiting $ManagerWait Seconds" -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
                Invoke-Wait -Seconds $ManagerWait
            }
        } while ($script:QmanRunning -ne "Shutdown")
        Write-LogLevel -Message "Exiting QueueManager Function." -Logfile $LOG_FILE -RunLogLevel $RunLogLevel -MsgLevel DEBUG 
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-Qman: $ErrorMessage $FailedItem"
    }
    
}
# .\PembrokePSqman\private\Invoke-QmanShutdownTaskSet.ps1
function Invoke-QmanShutdownTaskSet {
    <#
    .DESCRIPTION
        This function will perform shutdown tasks for a Queue_Manager
    .PARAMETER RestServer
        A RestServer is Required.
    .PARAMETER TableName
        A properties path is Required.
    .PARAMETER ID
        An ID is Required.
    .EXAMPLE
        Invoke-QmanShutdownTaskSet -RestServer localhost -TableName tasks -ID 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][string]$TableName,
        [Parameter(Mandatory=$true)][int]$ID
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            Write-LogLevel -Message "Aborting cancelled tasks for table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            Invoke-AbortCancelledTaskSet -RestServer $RestServer -TableName $TableName
            Invoke-Wait -Seconds 5
            Write-LogLevel -Message "Shutting down QueueManager: $ID." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            Invoke-UpdateQmanData -ComponentId $ID -RestServer $RestServer -Column STATUS_ID -Value 1
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-QmanShutdownTaskSet: $ErrorMessage $FailedItem"
        }
        $ReturnMessage
    } else {
        Throw "Invoke-QmanShutdownTaskSet: Unable to reach Rest server: $RestServer."
    }

}
    
# .\PembrokePSqman\private\Invoke-QmanStartupTaskSet.ps1
function Invoke-QmanStartupTaskSet {
    <#
    .DESCRIPTION
        This function will perform shutdown tasks for a Queue_Manager
    .PARAMETER RestServer
        A RestServer is Required.
    .PARAMETER TableName
        A properties path is Required.
    .PARAMETER ID
        An ID is Required.
    .EXAMPLE
        Invoke-QmanStartupTaskSet -RestServer localhost -TableName tasks -ID 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][string]$TableName,
        [Parameter(Mandatory=$true)][int]$ID
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            Write-LogLevel -Message "Aborting Cancelled tasks for Table: $TableName" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            Invoke-AbortCancelledTaskSet -RestServer $RestServer -TableName $TableName
            Invoke-Wait -Seconds 5
            Write-LogLevel -Message "Setting the QueueManager status to 2(Running)." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            Invoke-UpdateQmanData -ComponentId $ID -RestServer $RestServer -Column STATUS_ID -Value 2
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-QmanStartupTaskSet: $ErrorMessage $FailedItem"
        }
        $ReturnMessage
    } else {
        Throw "Invoke-QmanStartupTaskSet: Unable to reach Rest server: $RestServer."
    }
}
    
# .\PembrokePSqman\private\Invoke-QueueSubmittedTaskSet.ps1
function Invoke-QueueSubmittedTaskSet {
    <#
    .DESCRIPTION
        This function will queue Submitted tasks
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Invoke-QueueSubmittedTaskSet -RestServer localhost -TableName tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([Int])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            # Get a list of Submitted tasks
            $TableName = $TableName.ToLower()
            $SubmittedTasks = (Get-SubmittedTaskSet -RestServer $RestServer -TableName $TableName).$TableName
            $SubmittedTasksCount = ($SubmittedTasks | Measure-Object).count
            Write-LogLevel -Message "Reviewing: $SubmittedTasksCount for queueing from table: $TableName." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            if($SubmittedTasksCount -gt 0) {
                foreach($Task in $SubmittedTasks){
                    # Foreach task, set it to Queued.
                    $TaskId = $Task.ID
                    $body = @{STATUS_ID = "6"} 
                    Write-LogLevel -Message "Queueing task: $TaskId." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
                    $ReturnMessage = Invoke-UpdateTaskTable -RestServer $RestServer -TableName $TableName -TaskID $TaskId -Body $body
                }
            } else {
                # No tasks to queue
                Write-LogLevel -Message "No Tasks to Queue from table: $TableName." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            }
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-QueueSubmittedTaskSet: $ErrorMessage $FailedItem"
        }
        $ReturnMessage
    } else {
        Throw "Invoke-QueueSubmittedTaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Invoke-ReviewQueuedTaskSet.ps1
function Invoke-ReviewQueuedTaskSet {
    <#
    .DESCRIPTION
        This function will Review Queued tasks for assignment to Workflow Managers.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .EXAMPLE
        Invoke-ReviewQueuedTaskSet -RestServer localhost -TableName tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([Int])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer,
        [string]$TableName="tasks"
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            # Get a list of Submitted tasks
            $TableName = $TableName.ToLower()
            $QueuedTasks = (Get-QueuedTaskSet -RestServer $RestServer -TableName $TableName).$TableName
            $QueuedTasksCount = ($QueuedTasks | Measure-Object).Count
            Write-LogLevel -Message "Reviewing: $QueuedTasksCount tasks that are queued from table: $TableName." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
            if($QueuedTasksCount -gt 0) {
                foreach($Task in $QueuedTasks){
                    # Foreach task, see if it can be assigned.
                    $TaskId = $Task.ID
                    $TASK_TYPE_ID = $Task.TASK_TYPE_ID
                    Write-LogLevel -Message "Reviewing TaskId: $TaskId TaskTypeID: $Task_Type_Id for Assignment." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
                    # Based on the task type ID, we need to get the Workflow Manager Type that is enabled to perform that task.
                    Write-LogLevel -Message "Determining the Workflow Manager Type that can perform the task." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
                    $Wman_Type_ID = ((Get-AvailableWmanType -RestServer localhost -TaskTypeId $TASK_TYPE_ID).wman_task_types).WORKFLOW_MANAGER_TYPE_ID
                    # then get the WMAN that is available
                    Write-LogLevel -Message "Getting list of Available Workflow Managers" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
                    $AvailableWmanSet = (Get-AvailableWmanSet -RestServer localhost -Wman_Type $Wman_Type_ID).workflow_manager
                    # Loop through each to see if the Wman is at it's max
                    :SingleTaskLoop
                    foreach($Workflow_Manager in $AvailableWmanSet) {
                        $WmanId = $Workflow_Manager.ID
                        $Wman_Max = $Workflow_Manager.MAX_CONCURRENT_TASKS
                        $Wman_Hostname = $Workflow_Manager.Hostname
                        # Get the current Number of 'active' tasks assigned to the Workflow Manager
                        $WmanActiveTaskCount = ((Get-ActiveWmanTaskSet -RestServer localhost -TableName tasks -WmanId $WmanId).$TableName | Where-Object {$_.WORKFLOW_MANAGER_ID -eq "$WmanId"} | Measure-Object).count
                        # if its not, then we can assign it!
                        if ($WmanActiveTaskCount -lt $Wman_Max){
                            #Don't forget to break out if we can assign it!
                            Write-LogLevel -Message "Assigning task: $TaskId to Mgr: $Wman_Hostname." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel INFO
                            $ReturnMessage = Invoke-AssignTask -RestServer localhost -TaskId $TaskId -WmanId $WmanId
                            break SingleTaskLoop
                        } else {
                            # Current Workflow MGR was at its Max.
                            Write-LogLevel -Message "Current Workflow Manager: $Wman_Hostname is running at it's max: $WmanActiveTaskCount." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
                        }
                    }
                }
            } else {
                # No tasks to queue
                Write-LogLevel -Message "No Tasks to Queue" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel DEBUG
            }
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-ReviewQueuedTaskSet: $ErrorMessage $FailedItem"
        }
        $ReturnMessage
    } else {
        Throw "Invoke-ReviewQueuedTaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\private\Invoke-UpdateQmanData.ps1
function Invoke-UpdateQmanData {
    <#
    .DESCRIPTION
        This function will update a column and field for a Queue_Manager
    .PARAMETER ComponentId
        An ID is required.
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER Column
        A Column/Field is required.
    .PARAMETER Value
        A Value is required.
    .EXAMPLE
        Invoke-UpdateQmanData -ComponentId 1 -RestServer localhost -Column STATUS_ID -Value 2
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][int]$ComponentId,
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][string]$Column,
        [Parameter(Mandatory=$true)][string]$Value
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-LogLevel -Message "Updating the Queue_Manager -Column $Column -Value $Value" -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
            if ((Invoke-UpdateComponent -ComponentId $ComponentId -RestServer $RestServer -Column $Column -Value $Value -ComponentType queue_manager) -eq 1) {
                # Good To go
            } else {
                Write-LogLevel -Message "Unable to update Queue Manager via restcall." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel TRACE
                Throw "Unable to update Qman data."
            }
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-UpdateQmanData: $ErrorMessage $FailedItem"
        }
    } else {
        Throw "Invoke-UpdateQmanData: Unable to reach Rest server: $RestServer."
    }
    
}
Write-Verbose 'Importing from [C:\projects\pembrokepsqman\PembrokePSqman\public]'
# .\PembrokePSqman\public\Invoke-DeployQman.ps1
function Invoke-DeployQman
{
    <#
    .DESCRIPTION
        Deploys artifacts to prepare a machine to run a PembrokePS Queue Manager.
    .PARAMETER Destination
        A Destitnation path is optional.
    .PARAMETER Source
        A Source location for PembrokePS artifacts is optional.
    .EXAMPLE
        Invoke-DeployQman -Destination c:\PembrokePS -Source c:\OpenProjects\ProjectPembroke\PembrokePSqman
    .NOTES
        It will create the directory if it does not exist. Also install required Modules.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [String]$Destination="C:\PembrokePS\",
        [String]$Source=(Split-Path -Path (Get-Module -ListAvailable PembrokePSqman).path)
    )
    try
    {
        if(Test-Path -Path "$Destination\qman") {
            Write-LogLevel -Message "The Qman Directory exists." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel CONSOLEONLY
        } else {
            Write-LogLevel -Message "Creating '\qman\data' and '\qman\logs' Directories." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel CONSOLEONLY
            New-Item -Path "$Destination\qman\data" -ItemType Directory
            New-Item -Path "$Destination\qman\logs" -ItemType Directory
        }
        Write-LogLevel -Message "Installing Required Modules." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel CONSOLEONLY
        Install-Module -Name PembrokePSrest,PembrokePSutilities,PowerLumber,RestPS -Force
        Import-Module -Name PembrokePSrest,PembrokePSutilities,PowerLumber,RestPS -Force
        Write-LogLevel -Message "Creating Rest Directory Set." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel CONSOLEONLY
        Invoke-CreateRouteDirectorySet -InstallDirectory "$Destination\Qman\rest"
        Write-LogLevel -Message "Copying Default Properties file to Qman\data Directory." -Logfile "$LOG_FILE" -RunLogLevel $RunLogLevel -MsgLevel CONSOLEONLY
        Copy-Item -Path "$Source\data\pembrokeps.properties" -Destination "$Destination\qman\data" -Confirm:$false       
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-DeployQman: $ErrorMessage $FailedItem"
    }

}
# .\PembrokePSqman\public\Invoke-RegisterQman.ps1
function Invoke-RegisterQman {
    <#
    .DESCRIPTION
        This function will gather Status information from PembrokePS web/rest for a Queue_Manager
    .PARAMETER RestServer
        A Rest Server is required.
    .EXAMPLE
        Invoke-RegisterQman -RestServer -localhost
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer
            # Update the Parameters
                # RestServer Required
                # ComponentType By Name (Required)
                # Description (Required)
                # Desired Port (Optional)
                # Hostname (Optional)
                # IP (Optional)
                # Wait
                # LogFile (Optional)
                # Kicker Port (Optional)
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            #Going to be creating a new record here, need to figure out the 'joins' to ensure the data is good.
            Write-Output "This function is not complete!"
            # if ports were specified
                # Check to see if the requested ports are available
                # if not, see if any are available
                    #if not, create the next ones

            # Build up the rest call to add the row

            # Write the local Properties file with all of the information used to create the row.

        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-RegisterQman: $ErrorMessage $FailedItem"
        }
        #$QmanStatusData
    } else {
        Throw "Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSqman\public\Start-Qman.ps1
function Start-Qman {
    <#
    .DESCRIPTION
        This function will gather Status information from PembrokePS web/rest for a Queue_Manager
    .PARAMETER RestServer
        A Rest Server is required.
    .EXAMPLE
        Start-Qman -RestServer localhost
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Low"
    )]
    [OutputType([hashtable])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RestServer
    )
    begin {
        if (Test-Connection -Count 1 $RestServer -Quiet) {
            # Gather the local properties
        } else {
            Throw "Unable to reach web server."
        }
    }
    process
    {
        if ($pscmdlet.ShouldProcess("PembrokePSQman"))
        {
            try
            {
                #Going to be creating a new record here, need to figure out the 'joins' to ensure the data is good.
                Write-Output "This function is not complete!"
                # Validate properties file exists
                # Import all the data.
                # Validate system.RestServer
                # Start Kicker process
                # Start Rest Service (and kicker rest)
            }
            catch
            {
                $ErrorMessage = $_.Exception.Message
                $FailedItem = $_.Exception.ItemName        
                Throw "Start-Qman: $ErrorMessage $FailedItem"
            }
        }
        else
        {
            # -WhatIf was used.
            return $false
        }
    }
    
}
Write-Verbose 'Importing from [C:\projects\pembrokepsqman\PembrokePSqman\classes]'