PembrokePSrest.psm1

Write-Verbose 'Importing from [C:\projects\pembrokepsrest\PembrokePSrest\private]'
# .\PembrokePSrest\private\Invoke-CreateRouteDirectorySet.ps1
function Invoke-CreateRouteDirectorySet
{
    <#
    .DESCRIPTION
        Create Endpoint Route Directories to house Powershell scripts.
    .PARAMETER InstallDirectory
        A valid Directory is required.
    .EXAMPLE
        Invoke-CreateRouteDirectorySet -InstallDirectory C:\PembrokePS\Rest
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [Parameter(Mandatory = $true)][String]$InstallDirectory
    )
    try
    {
        if (Test-Path -Path $InstallDirectory)
        {
            Write-Output "Install Directory path: $InstallDirectory already exists."
            Throw "Invoke-CreateRouteDirectorySet: Install Directory path: $InstallDirectory already exists."
        }
        else
        {
            Write-Output "Creating Rest Directories in: $InstallDirectory."
            New-Item -Path $InstallDirectory -ItemType Directory
            New-Item -Path "$InstallDirectory\DELETE" -ItemType Directory
            New-Item -Path "$InstallDirectory\GET" -ItemType Directory
            New-Item -Path "$InstallDirectory\POST" -ItemType Directory
            New-Item -Path "$InstallDirectory\PUT" -ItemType Directory
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-CreateRouteDirectorySet: $ErrorMessage $FailedItem"
    }
}
# .\PembrokePSrest\private\Invoke-MoveAvailableRoutesFile.ps1
function Invoke-MoveAvailableRoutesFile 
{
    <#
    .DESCRIPTION
        Copy Available Endpoint Routes file to specified directory.
    .PARAMETER InstallDirectory
        A valid Directory is required.
    .PARAMETER SourceAvailableRoutesFile
        A valid File is required.
    .EXAMPLE
        Invoke-MoveAvailableRoutesFile -InstallDirectory C:\PembrokePS\Rest -SourceAvailableRoutesFile C:\OPEN_PROJECTS\ProjectPembroke\PembrokePSrest\PembrokePSrest\data\PembrokePSEndpointRoutes.ps1
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [Parameter(Mandatory = $true)][String]$InstallDirectory,
        [Parameter(Mandatory = $true)][String]$SourceAvailableRoutesFile
    )
    try
    {
        if (!(Test-Path -Path $SourceAvailableRoutesFile))
        {
            Write-Output "Source Directory path: $SourceAvailableRoutesFile does not exist."
            Throw "Invoke-MoveAvailableRoutesFile: Source Directory path: $SourceAvailableRoutesFile does not exist."
        }
        else
        {
            Write-Output "Copying Available Routes file to: $InstallDirectory."
            Copy-Item -Path "$SourceAvailableRoutesFile" -Destination $InstallDirectory -Force -Confirm:$false
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-MoveAvailableRoutesFile: $ErrorMessage $FailedItem"
    }
}
# .\PembrokePSrest\private\Invoke-MoveEndpointRouteSet.ps1
function Invoke-MoveEndpointRouteSet
{
    <#
    .DESCRIPTION
        Create Endpoint Route Directories to house Powershell scripts.
    .PARAMETER InstallDirectory
        A valid Directory is required.
    .PARAMETER SourceAvailableRoutesDirectory
        A valid Directory is required.
    .EXAMPLE
        Invoke-MoveEndpointRouteSet -InstallDirectory C:\PembrokePS\Rest -SourceAvailableRoutesDirectory C:\OPEN_PROJECTS\ProjectPembroke\PembrokePSrest\PembrokePSrest\data
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [Parameter(Mandatory = $true)][String]$InstallDirectory,
        [Parameter(Mandatory = $true)][String]$SourceAvailableRoutesDirectory
    )
    try
    {
        if (!(Test-Path -Path $SourceAvailableRoutesDirectory))
        {
            Write-Output "Source Directory path: $SourceAvailableRoutesDirectory does not exist."
            Throw "Invoke-MoveEndpointRouteSet: Source Directory path: $SourceAvailableRoutesDirectory does not exist."
        }
        else
        {
            Write-Output "Creating Rest Directories."
            if (Test-Path -Path "$SourceAvailableRoutesDirectory\DELETE")
            {
                Copy-Item -Path "$SourceAvailableRoutesDirectory\DELETE" -Destination $InstallDirectory -Container -Recurse -Force -Confirm:$false
            }
            if (Test-Path -Path "$SourceAvailableRoutesDirectory\GET")
            {
                Copy-Item -Path "$SourceAvailableRoutesDirectory\GET" -Destination $InstallDirectory -Container -Recurse -Force -Confirm:$false
            }
            if (Test-Path -Path "$SourceAvailableRoutesDirectory\POST")
            {
                Copy-Item -Path "$SourceAvailableRoutesDirectory\POST" -Destination $InstallDirectory -Container -Recurse -Force -Confirm:$false
            }
            if (Test-Path -Path "$SourceAvailableRoutesDirectory\PUT")
            {
                Copy-Item -Path "$SourceAvailableRoutesDirectory\PUT" -Destination $InstallDirectory -Container -Recurse -Force -Confirm:$false
            }
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-MoveEndpointRouteSet: $ErrorMessage $FailedItem"
    }
}
Write-Verbose 'Importing from [C:\projects\pembrokepsrest\PembrokePSrest\public]'
# .\PembrokePSrest\public\Get-ComponentStatus.ps1
function Get-ComponentStatus {
    <#
    .DESCRIPTION
        This function will gather Status information from PembrokePS web/rest.
    .PARAMETER ComponentType
        A Component type is required.
    .PARAMETER ComponentId
        An ID is required.
    .PARAMETER RestServer
        A Rest Server is required.
    .EXAMPLE
        Get-ComponentStatus -ComponentType Queue_Manager -ComponentId 1 -RestServer localhost
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [ValidateSet("Queue_Manager","Workflow_Manager")]
        [string]$ComponentType,
        [Parameter(Mandatory=$true)][int]$ComponentId,
        [Parameter(Mandatory=$true)][string]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Getting Component: $ComponentId, Type: $ComponentType Status."
            $ComponentType = $ComponentType.ToLower()
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$ComponentType" + "?filter=ID,eq,$ComponentId&transform=1"
            Write-Output "$URL"
            $ComponentStatusData = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-ComponentStatus: $ErrorMessage $FailedItem"
        }
        $ComponentStatusData
    } else {
        Throw "Get-ComponentStatus: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Get-EndpointPort.ps1
function Get-EndpointPort {
    <#
    .DESCRIPTION
        This function will gather Status information from PembrokePS web/rest.
    .PARAMETER EndpointPortID
        An ID is required.
    .PARAMETER RestServer
        A Rest Server is required.
    .EXAMPLE
        Get-EndpointPort -RestServer localhost -EndpointPortID 1
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][int]$EndpointPortID,
        [Parameter(Mandatory=$true)][string]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Getting Component Endpoint Port data for Id: $EndpointPortID."
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/endpoint_ports?filter=ID,eq,$EndpointPortID&transform=1"
            Write-Output "$URL"
            $EndpointPortData = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-EndpointPort: $ErrorMessage $FailedItem"
        }
        $EndpointPortData
    } else {
        Throw "Get-EndpointPort: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Get-PpsPropertySet.ps1
function Get-PpsPropertySet {
    <#
    .DESCRIPTION
        This function will gather Tasks based on a requested Status
    .PARAMETER RestServer
        A RestServer is Required.
    .EXAMPLE
        Get-PpsPropertySet -RestServer localhost
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][String]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Gathering Property data from: $RestServer."
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/properties?transform=1"
            Write-Output "$URL"
            $PropertyData = Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-PpsPropertySet: $ErrorMessage $FailedItem"
        }
        $PropertyData
    } else {
        Throw "Get-PpsPropertySet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Get-StatusIdByName.ps1
function Get-StatusIdByName {
    <#
    .DESCRIPTION
        This function will get the Status_ID from a Status_NAme
    .PARAMETER StatusName
        A StatusName is required.
    .PARAMETER RestServer
        A RestServer is required.
    .EXAMPLE
        Get-StatusIdByName -StatusName Running -RestServer localhost
    .NOTES
        This will return an integer.
    #>

    [CmdletBinding()]
    [OutputType([Int])]
    param(
        [ValidateSet("Submitted","Queued","Assigned","Running","Cancelled","Staged")]
        [Parameter(Mandatory=$true)][String]$StatusName,
        [Parameter(Mandatory=$true)][string]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Getting StatusID by Status Name: $StatusName."
            $StatusName = $StatusName.ToLower()
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/status?filter=STATUS_NAME,eq,$StatusName&transform=1"
            Write-Output "$URL" -Logfile "$LOG_FILE"
            $StatusID = ((Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing).status).ID
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-StatusIdByName: $ErrorMessage $FailedItem"
        }
        $StatusID
    } else {
        Throw "Get-StatusIdByName: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Get-TaskInfo.ps1
function Get-TaskInfo {
    <#
    .DESCRIPTION
        This function will gather Tasks based on a requested Status
    .PARAMETER TaskId
        A Status is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .PARAMETER RestServer
        A RestServer is Required.
    .EXAMPLE
        Get-TaskInfo -TaskId 1 -TableName Tasks -RestServer localhost
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][String]$TaskId,
        [String]$TableName="tasks",
        [Parameter(Mandatory=$true)][String]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Gathering Task data from: $TableName TaskId: $TaskId." -Logfile "$LOG_FILE"
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/" + $TableName + "?include=task_types,targets&filter=id,eq," + $TaskId + '&transform=1'
            Write-Output "$URL"
            $TaskData = (Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing).$TableName
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-TaskInfo: $ErrorMessage $FailedItem"
        }
        $TaskData
    } else {
        Throw "Get-TaskInfo: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Get-TaskSet.ps1
function Get-TaskSet {
    <#
    .DESCRIPTION
        This function will gather Tasks based on a requested Status
    .PARAMETER TaskStatus
        A Status is required.
    .PARAMETER TableName
        A TableName is optional, default is tasks.
    .PARAMETER RestServer
        A RestServer is Required.
    .EXAMPLE
        Get-TaskSet -Status Queued -TableName Tasks
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [ValidateSet("Submitted","Queued","Assigned","Running","Cancelled","Staged")]
        [Parameter(Mandatory=$true)][String]$TaskStatus,
        [String]$TableName="tasks",
        [Parameter(Mandatory=$true)][String]$RestServer
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Gathering Task data from: $TableName with Status: $TaskStatus."
            $Status_ID = Get-StatusIdByName -StatusName $TaskStatus -RestServer $RestServer
            $TableName = $TableName.ToLower()
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/" + $TableName + "?include=status&filter=status_id,eq," + $Status_ID + '&transform=1'
            Write-Output "$URL" -Logfile "$LOG_FILE"
            $TaskData = (Invoke-RestMethod -Method Get -Uri "$URL" -UseBasicParsing).$TableName
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-TaskSet: $ErrorMessage $FailedItem"
        }
        $TaskData
    } else {
        Throw "Get-TaskSet: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Invoke-DeployPPSRest.ps1
function Invoke-DeployPPSRest
{
    <#
    .DESCRIPTION
        Deploy Database Schema to web server. This needs to be updated
    .PARAMETER InstallDirectory
        A valid Directory is optional.
    .PARAMETER SourceAvailableRoutesDirectory
        A valid Directory is optional.
    .PARAMETER SourceAvailableRoutesFile
        A valid File is optional.
    .EXAMPLE
        Invoke-DeployPPSRest -InstallDirectory C:\PembrokePS\Rest -SourceAvailableRoutesDirectory C:\OPEN_PROJECTS\ProjectPembroke\PembrokePSrest\PembrokePSrest\data -SourceAvailableRoutesFile C:\OPEN_PROJECTS\ProjectPembroke\PembrokePSrest\PembrokePSrest\data\PembrokePSEndpointRoutes.ps1
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [String]$InstallDirectory = 'C:\PembrokePS\Rest',
        [String]$SourceAvailableRoutesDirectory = ((Split-Path -Path (Get-Module -ListAvailable PembrokePSrest).path) + "\Data"),
        [String]$SourceAvailableRoutesFile = '\PembrokePS\EndpointRoutes.ps1'
    )
    try
    {       
        if (Invoke-CreateRouteDirectorySet -InstallDirectory $InstallDirectory)
        {
            Write-Output "Invoke-CreateRouteDirectorySet complete successfully."
        }
        if (Invoke-MoveEndpointRouteSet -InstallDirectory $InstallDirectory -SourceAvailableRoutesDirectory $SourceAvailableRoutesDirectory)
        {
            Write-Output "Invoke-MoveEndpointRouteSet complete successfully."
        }
        # Move \bin files to Install Directory
        $SourceBinDirectory = ((Split-Path -Path (Get-Module -ListAvailable PembrokePSrest).path) + "\bin")
        if(Copy-Item -Path $SourceBinDirectory -Destination $InstallDirectory -Container -Recurse -Force -Confirm:$false){
            Write-Output "Copied \bin successfully."
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-DeployPPSRest: $ErrorMessage $FailedItem"
    }

}
# .\PembrokePSrest\public\Invoke-StartPPSEndpoint.ps1
function Invoke-StartPPSEndpoint
{
    <#
    .DESCRIPTION
        Start a PembrokePS ReST endpoint.
    .PARAMETER Port
        A valid Port is required.
    .PARAMETER SourceAvailableRoutesFile
        A valid File is required.
    .EXAMPLE
        Invoke-StartPPSEndpoint -Port 8999 -SourceAvailableRoutesFile $SourceAvailableRoutesFile
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [Parameter(Mandatory = $true)][Int]$Port,
        [Parameter(Mandatory = $true)][String]$SourceAvailableRoutesFile
    )
    try
    {
        if (!(Test-Path -Path $SourceAvailableRoutesFile))
        {
            Throw "Invoke-StartPPSEndpoint: Source Directory path: $SourceAvailableRoutesFile does not exist."
        }
        else
        {
            $ExecutionPath = "C:\PembrokePS\Rest\bin\Invoke-NewEndpoint.ps1"
            Write-Output "Starting Endpoint."
            Start-Process -WindowStyle Normal powershell.exe -ArgumentList "-file $ExecutionPath", "-SourceAvailableRoutesFile $SourceAvailableRoutesFile -Port $Port"
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-StartPPSEndpoint: $ErrorMessage $FailedItem"
    }
}
# .\PembrokePSrest\public\Invoke-UpdateComponent.ps1
function Invoke-UpdateComponent {
    <#
    .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 ComponentType
        A ComponentType is required.
    .PARAMETER Value
        A Value is required.
    .EXAMPLE
        Invoke-UpdateComponent -ComponentId 1 -RestServer localhost -Column STATUS_ID -Value 2 -ComponentType queue_manager
    .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,
        [Parameter(Mandatory=$true)][string]$ComponentType
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            Write-Output "Updating Component $ComponentId, Type: $ComponentType, Column: $Column, Value: $value."
            $ComponentType = $ComponentType.ToLower()
            $body = @{$Column = "$Value"} | convertto-json
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$ComponentType/$ComponentId"
            Write-Output "$URL"
            $RawRestReturn = Invoke-RestMethod -Method Put -Uri "$URL" -body $body
            $RestReturn = ConvertFrom-Json $RawRestReturn
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-UpdateComponent: $ErrorMessage $FailedItem"
        }
        $RestReturn
    } else {
        Throw "Invoke-UpdateComponent: Unable to reach Rest server: $RestServer."
    }
    
}
# .\PembrokePSrest\public\Invoke-UpdateTaskTable.ps1
function Invoke-UpdateTaskTable {
    <#
    .DESCRIPTION
        This function will update a column and field for a Queue_Manager
    .PARAMETER RestServer
        A Rest Server is required.
    .PARAMETER TableName
        An TableName is required.
    .PARAMETER TaskID
        An TaskID is required.
    .PARAMETER Body
        A Column/Field is required.
    .EXAMPLE
        Invoke-UpdateTaskTable -RestServer localhost -TableName tasks -TaskID 1 -Body "@{STATUS_ID = '2'}"
    .NOTES
        This will return a hashtable of data from the PPS database.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory=$true)][string]$TableName,
        [Parameter(Mandatory=$true)][string]$RestServer,
        [Parameter(Mandatory=$true)][hashtable]$Body,
        [Parameter(Mandatory=$true)][int]$TaskID
    )
    if (Test-Connection -Count 1 $RestServer -Quiet) {
        try
        {
            $TableName = $TableName.ToLower()
            Write-Output "Updating Task Table: $TableNAme, Task: $TaskId."
            $URL = "http://$RestServer/PembrokePS/public/api/api.php/$TableName/$TaskID"
            Write-Output "$URL"
            $RawRestReturn = Invoke-RestMethod -Method Put -Uri "http://$RestServer/PembrokePS/public/api/api.php/$TableName/$TaskID" -body $body
            $RestReturn = ConvertFrom-Json $RawRestReturn
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Invoke-UpdateTaskTable: $ErrorMessage $FailedItem"
        }
        $RestReturn
    } else {
        Throw "Invoke-UpdateTaskTable: Unable to reach Rest server: $RestServer."
    }
    
}
Write-Verbose 'Importing from [C:\projects\pembrokepsrest\PembrokePSrest\classes]'