Tasks.psm1



<#
.SYNOPSIS
    Retrieves an automation task
.DESCRIPTION
    Get-HidTask will return one or more Tasks
.PARAMETER Name
    Specifies the name of an existing task to retrieve, can be specified as an array of strings to retrieve multiple tasks
.PARAMETER Context
    When specified gets the automation tasks under the specified contexts
.PARAMETER Category
    If specified also requires a context. returns the tasks nder the specified category
.PARAMETER CompanyName
    The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
.PARAMETER ApiKey
    The Apikey to use for the api call. Required if not connected with Connect-HelloId.
.PARAMETER ApiSecret
    The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
.EXAMPLE
    Get-HidTask -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)
    Returns all tasks in the tenant
.EXAMPLE
    Get-HidTask -Name "companyName"
    Returns the task with the name "companyName"
.INPUTS
    You can pipe a string that contains the name to Get-HidTask
.OUTPUTS
    Get-HidTask returns an object for each task that it gets.
#>

function Get-HidTask {
    [CmdletBinding(DefaultParameterSetName = 'Name',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (        
                
        # the name of an existing task
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "Name")]
        [ValidateNotNullOrEmpty()]
        [alias("TaskName")]
        [string[]]$Name,

        # the context of a task
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Context,
        
        # the category of a task
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Category,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {
        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    
    process {
        if ($PSBoundParameters.ContainsKey("Context")) {
            
            if ($PSBoundParameters.ContainsKey("Category")){

                if ($PSBoundParameters.ContainsKey("Name")){
                    foreach ($item in $Name){                        
                        $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context/category/$Category/$item"
                        $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                        $output
                    }
                } #end nested if Name
                else { #else for if cagegory is used without name parameter
                    $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context/category/$Category"
                    $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                    $output
                }
            } #end nested if Category
            else { #Only do this if there's no category used
                
                if ($PSBoundParameters.ContainsKey("Name")){
                    foreach ($item in $Name){                        
                        $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context/$item"
                        $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                        $output
                    }
                } #end nested if Name
                else { #do this only if parameter Context is only thing used, no category and no name
                    $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context"
                    $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                    $output
                }
            } #end elseif based on category
        
        } #end containskey Context
        elseif ($PSBoundParameters.ContainsKey("Category")) {
            throw "Please specify a context if you use the Category parameter"
        } #end elseif

        elseif ($PSBoundParameters.ContainsKey("Name")) {
            foreach ($item in $Name){
                $URI = "$BaseUrl/api/v1/automationstore/tasks/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output
            }
        }
        else {
            $URI = "$BaseUrl/api/v1/automationstore/tasks"
            $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
            <#
            if ($output.psobject.Properties.name -contains "data"){
                $output = $output.data
            }
            #>

            return $output               
            
        }
        
        
    } #End process
    
    end {
        
    } #End end
} #End function




<#
.SYNOPSIS
    Retrieves an automation task
.DESCRIPTION
    Get-HidTask will return one or more Tasks
.PARAMETER TaskName
    Specifies the name of an existing task to retrieve, can be specified as an array of strings to retrieve multiple tasks
.PARAMETER Context
    When specified gets the automation tasks under the specified contexts
.PARAMETER CompanyName
    The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
.PARAMETER ApiKey
    The Apikey to use for the api call. Required if not connected with Connect-HelloId.
.PARAMETER ApiSecret
    The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
.EXAMPLE
    Get-HidTask -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)
    Returns all tasks in the tenant
.EXAMPLE
    Get-HidTask -Name "companyName"
    Returns the task with the name "companyName"
.INPUTS
    You can pipe a string that contains the name to Get-HidTask
.OUTPUTS
    Get-HidTask returns an object for each task that it gets.
#>

function Get-HidTaskCategory {
    [CmdletBinding(PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (   
        # The task's context
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Context,

        # the name of a Task ONLY USABLE IF CONTEXT IS SET
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [alias("Name")]
        [string]$TaskName,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {
        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }
        
        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    
    process {
        if ($PSBoundParameters.ContainsKey("Context")) { #conditional if based on context parameter exists
            foreach ($Cont in $Context){

                if ($PSBoundParameters.ContainsKey("TaskName")){ #if parameter name exists
                    $URI = "$BaseUrl/api/v1/automationstore/categories/context/$Cont/$TaskName"
                } #closing if based on $name
                else {
                    $URI = "$BaseUrl/api/v1/automationstore/categories/context/$Cont"
                }
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output

            } #closing foreach cont in context
        } #closing if based on context
        elseif ($PSBoundParameters.ContainsKey("TaskName")){
            throw "Please specify a context if you use the TaskName parameter"
        }
        else {
            $URI = "$BaseUrl/api/v1/automationstore/categories/context/"
            $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
            <#
            if ($output.psobject.Properties.name -contains "data"){
                $output = $output.data
            }
            #>

            return $output            
        }
    } #End process
    end {        
    } #End end
} #End function