Forms.psm1




<#
.SYNOPSIS
    Returns a delegated form
.DESCRIPTION
    Returns one or more Delegated forms.
.PARAMETER DelegatedFormGuid
    Specifies the Guid of an existing Delegated form to return, can be specified as an array of strings to retrieve multiple variables
.PARAMETER Name
    Specifies the name of an existing Delegated form to return, can be specified as an array of strings to retrieve multiple variables
.PARAMETER SearchString
    String to search for in delegated forms
.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-HidDelegatedForm -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)

    Returns all Delegated forms
.EXAMPLE
    Get-HidDelegatedForm -Name testform

    Returns the Delegated form with name testform
.EXAMPLE
    Get-HidDelegatedForm -DelegatedFormGUID "1a38f4fb-74c2-4201-a19a-4cf8938435c3" -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)

    Returns the Delegated form with guid "1a38f4fb-74c2-4201-a19a-4cf8938435c3"
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Get-HidDelegatedForm {
    [CmdletBinding(DefaultParameterSetName = 'FormGuid',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # the GUID of an existing Delegated form
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "FormGuid")]
        [alias("Guid")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")] #Matches a GUID
        [string[]]$DelegatedFormGuid,        
        
        # the name of an existing Delegated form
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "FormName")]
        [ValidateNotNullOrEmpty()]
        [string[]]$Name, 
        
        # String to search for in delegated forms
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $false,
            ValueFromPipelineByPropertyName = $false,
            ParameterSetName = "Listforms")]
        [string]$SearchString = "",

        # 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
    )
    
    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)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        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("DelegatedFormGuid")){            
            foreach ($guid in $DelegatedFormGuid){
                $URI = "https://$CompanyName.helloid.com/api/v1/delegatedforms/$guid"                
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing               
                $output
            }

        }
        elseif ($PSBoundParameters.ContainsKey("Name")) {
            foreach ($item in $Name){
                $URI = "https://$CompanyName.helloid.com/api/v1/delegatedforms/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output
            }
        }
        else {
            
            $URI = "https://$CompanyName.helloid.com/api/v1/delegatedforms?search=$SearchString&skip=0&take=9999"
            $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
    Creates or updates a delegated form
.DESCRIPTION
    Creates a new delegated form if the delegated form GUID is left empty. Updates an existing delegated form record if the passed identifier matches an existing delegated form record.
.PARAMETER DelegatedFormGuid
    The GUID of the delegated form to update. If omitted the cmdlet creates a new dynamicform
.PARAMETER Name
    The name of the dynamic form to create or update
.PARAMETER FormDefinition
    The code that represents the new or updated dynamicform in powershell array form
.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

.EXAMPLE

.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Set-HidDelegatedForm {
    [CmdletBinding(DefaultParameterSetName = "DynamicForm")]
    [OutputType([String])]
    Param (
        # Optional, the GUID of an existing delegated form
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [guid]$DelegatedFormGUID,

        # name of the delegated form
        [Parameter(Mandatory= $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        # The GUID of the AD integration to execute the action on. This is only used for delegated forms based on a standard TemplateID.
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "templateID")]
        [ValidateNotNullOrEmpty()]
        [guid]$AdConfigurationGUID,
        
        # A short slug that contains no spaces. If left empty, a slug will be generated based on the name.
        [Parameter(Mandatory= $false,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Slug,

        # Guid of the dynamic form you want to use for this delegated form
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "DynamicForm")]
        [ValidateNotNullOrEmpty()]
        [guid]$DynamicFormGUID,

        # Identifier of an existing delegated form template, such as "Disable account". Leave empty if creating a delegated form based on a dynamic form.
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "templateID")]
        [ValidateNotNullOrEmpty()]
        [string]$TemplateId,

        # boolean that indicates if the form is enabled, default: true
        [Parameter(Mandatory= $false,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [bool]$IsEnabled = $true,

        # Boolean that indicates if the form dropdown (if available) should only be filled with users managed by the logged in user. This is for form using a standard TemplateID only.
        [Parameter(Mandatory= $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "templateID")]
        [ValidateNotNullOrEmpty()]
        [bool]$UseManagedUsers,

        # Boolean that indicates if the form dropdown (if available) should only be filled with users form specific groups. This is for form using a standard TemplateID only.
        [Parameter(Mandatory= $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "templateID")]
        [ValidateNotNullOrEmpty()]
        [bool]$UseUsersFromGroups,

        # Specified groups to use when {useUsersFromGroups} is set to true
        [Parameter(Mandatory= $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "templateID")]
        [ValidateNotNullOrEmpty()]
        [array]$SourceGroups,

        # List of group guids that can use the delegated form
        [Parameter(Mandatory= $false,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [guid[]]$AccessGroups = @(),

        # AD attribute to write comments to about the actions executed by the delegated form. For example: Account disabled on 20-3-20
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$AdCommentAttribute,

        # 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
    )
    
    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)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }        

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        #Uri
        $URI = "https://$CompanyName.helloid.com/api/v1/delegatedforms"


        $JsonSourceGroups = ConvertTo-Json -InputObject $SourceGroups -Depth 15
        $JsonAccessGroups = ConvertTo-Json -InputObject $AccessGroups -Depth 15


    } #End begin
    
    process {

        $SbBody = [System.Text.StringBuilder]::new()
        $null = $SbBody.AppendLine("{")
        if ($PSBoundParameters.ContainsKey("DelegatedFormGUID")){ $null = $SbBody.AppendLine("`"delegatedFormGUID`": `"$DelegatedFormGUID`"`,") }
        $null = $SbBody.AppendLine("`"name`": `"$Name`",")
        if ($PSBoundParameters.ContainsKey("Slug")){ $null = $SbBody.AppendLine("`"slug`": `"$Slug`"`,") }
        if ($PSBoundParameters.ContainsKey("adConfigurationGUID")){ $null = $SbBody.AppendLine("`"adConfigurationGUID`": `"$AdConfigurationGUID`"`,") }
        if ($PSBoundParameters.ContainsKey("DynamicFormGUID")){ $null = $SbBody.AppendLine("`"dynamicFormGUID`": `"$DynamicFormGUID`"`,") }
        else { $null = $SbBody.AppendLine("`"dynamicFormGUID`": null,") }
        if ($PSBoundParameters.ContainsKey("TemplateId")){ $null = $SbBody.AppendLine("`"templateID`": `"$TemplateId`"`,") }
        else { $null = $SbBody.AppendLine("`"templateID`": null,") }
        $null = $SbBody.AppendLine("`"isEnabled`": $(($IsEnabled).ToString().ToLower()),")
        if ($PSBoundParameters.ContainsKey("UseManagedUsers")){ $null = $SbBody.AppendLine("`"useManagedUsers`": $(($UseManagedUsers).ToString().ToLower()),") }
        if ($PSBoundParameters.ContainsKey("UseUsersFromGroups")){ $null = $SbBody.AppendLine("`"useUsersFromGroups`": $(($UseUsersFromGroups).ToString().ToLower()),") }
        if ($PSBoundParameters.ContainsKey("SourceGroups") -AND $UseUsersFromGroups){ $null = $SbBody.AppendLine("`"sourceGroups`": $JsonSourceGroups,") }
        if ($PSBoundParameters.ContainsKey("AdCommentAttribute")){ $null = $SbBody.AppendLine("`"adCommentAttribute`": `"$AdCommentAttribute`"`,") }
        $null = $SbBody.AppendLine("`"accessGroups`": $JsonAccessGroups")
        $null = $SbBody.AppendLine("}")

        Write-Debug -Message "Body is: $($SbBody.ToString())"

        
        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Body $SbBody.ToString() -Credential $Cred -UseBasicParsing


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




<#
.SYNOPSIS
    Returns a dynamic form
.DESCRIPTION
    Returns one or more dynamic forms.
.PARAMETER DynamicFormGuid
    Specifies the Guid of an existing dynamic form to return, can be specified as an array of strings to retrieve multiple variables
.PARAMETER Name
    Specifies the name of an existing dynamic form to return, can be specified as an array of strings to retrieve multiple variables
.PARAMETER ResultSize
    Specifies the amount of results to return. Use "Unlimited" for all results
.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-HidDynamicForm -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force) -ResultSize Unlimited

    Returns all dynamic forms
.EXAMPLE
    Get-HidDynamicForm -Name testform

    Returns the dynamic form with name testform
.EXAMPLE
    Get-HidDynamicForm -DynamicFormGUID "1a38f4fb-74c2-4201-a19a-4cf8938435c3" -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)

    Returns the dynamic form with guid "1a38f4fb-74c2-4201-a19a-4cf8938435c3"
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Get-HidDynamicForm {
    [CmdletBinding(DefaultParameterSetName = 'FormName',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # the GUID of an existing dynamic form
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "FormGuid")]
        [alias("Guid")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")] #Matches a GUID
        [string[]]$DynamicFormGuid,        
        
        # the name of an existing dynamic form
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "FormName")]
        [ValidateNotNullOrEmpty()]
        [string[]]$Name, 

        # Amount of results to return
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $false,
            ValueFromPipelineByPropertyName = $false,
            ParameterSetName = "all")]
        [ValidateNotNullOrEmpty()]
        [string]$ResultSize = "Unlimited",
        
        # 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
    )
    
    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)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        #Variables

        
        [array]$resultArray = @()
        $take = 20

        
    } #End begin
    
    process {

        if ($PSBoundParameters.ContainsKey("DynamicFormGuid")){
            
            foreach ($guid in $DynamicFormGuid){
                $URI = "https://$CompanyName.helloid.com/api/v1/forms/$guid"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output.formSchema = ConvertTo-Json -Dept 100 -InputObject $output.formSchema               
                $output
            }

        }
        elseif ($PSBoundParameters.ContainsKey("Name")) {

            foreach ($item in $Name){
                $URI = "https://$CompanyName.helloid.com/api/v1/forms/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output.formSchema = ConvertTo-Json -Dept 100 -InputObject $output.formSchema    
                $output
            }
        }
        else {
            

            if ($ResultSize -eq "Unlimited") {
                do {  
                    $URI = "https://$CompanyName.helloid.com/api/v1/forms/?&skip=$($resultArray.Count)&take=$take"
                    $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
              
                    $resultArray += $output.data
                } while ($resultArray.count -lt $output.total)
            } #end if ($ResultSize -eq "Unlimited")
            else {                
                do {
                    if ($take + $resultArray.Count -gt $ResultSize) {
                        $take = $ResultSize - $resultArray.Count
                    }

                    $URI = "https://$CompanyName.helloid.com/api/v1/forms/?&skip=$($resultArray.Count)&take=$take"
                    $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
              
                    $resultArray += $output.data
                } while ($resultArray.count -lt $ResultSize)
            } #end else
            
            
            return $resultArray
        }
        
        
    } #End process
    
    end {
        
    } #End end
} #End function



<#
.SYNOPSIS
    Creates or updates a dynamic form
.DESCRIPTION
    Creates a new record of dynamic form if the dynamic form GUID is left empty. Updates an existing dynamic form record if the passed identifier matches an existing dynamic form record.
.PARAMETER DynamicFormGuid
    The GUID of the dynamic form to update. If omitted the cmdlet creates a new dynamicform
.PARAMETER Name
    The name of the dynamic form to create or update
.PARAMETER FormDefinition
    The code that represents the new or updated dynamicform in powershell array form
.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
    Set-HidDynamicForm -Name "myform" -FormDefinition $formdef

    Creates a new dynamicform named myform with the specified definition
.EXAMPLE
    Get-HidDynamicForm -Name testform | Set-HidDynamicForm -Name "myform"

    Gets a dynamic form with the name testform, and changes the name to myform
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Set-HidDynamicForm {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # Optional, the GUID of an existing dynamic form
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromRemainingArguments = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")] #Matches a GUID
        [string]$DynamicFormGUID,

        # name of the form
        [Parameter(Mandatory= $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        # Form definition
        [Parameter(Mandatory= $true,
            ValueFromPipelineByPropertyName = $true)]
        [alias("FormSchema")]
        [ValidateNotNullOrEmpty()]
        [string]$FormDefinition,
        
        # 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
    )
    
    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)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        #Uri
        $URI = "https://$CompanyName.helloid.com/api/v1/forms"
    } #End begin
    
    process {

        #$JsonFormDefinition = ConvertTo-Json $FormDefinition -Depth 99

        if ($PSBoundParameters.ContainsKey("DynamicFormGUID")) {
            $Body = @"
            {
                "dynamicFormGUID": "$DynamicFormGUID",
                "name": "$Name",
                "formSchema": $FormDefinition
            }
"@

            
        } #endif
        else {
            $Body = @"
            {
                "name": "$Name",
                "formSchema": $FormDefinition
            }
"@

        } #closing else

        Write-Debug -Message "Body is: `n$Body"
        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Body $Body -Credential $Cred -UseBasicParsing
        $output.formSchema = ConvertTo-Json -Depth 100 -InputObject $output.formSchema

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



<#
.SYNOPSIS
    Removes a dynamic form
.DESCRIPTION
    Removes a dynamic form based on the guid
.PARAMETER DynamicFormGuid
    Specifies the Guid of an existing dynamic form to remove, can be specified as an array of strings to remove multiple variables
.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
    Remove-HidDynamicForm -DynamicFormGUID "c31fd53a-b346-4bb4-9a67-f9406d3968db"

    Removes the dynamic form with the specified GUID
.EXAMPLE
    Get-HidDynamicForm -Name testform | Remove-HidDynamicForm

    Gets the dynamic form with name testform, and pipes the result to Remove-HidDynamicForm which removes the form
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Remove-HidDynamicForm {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # the GUID of an existing dynamic form
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [alias("Guid")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")] #Matches a GUID
        [string[]]$DynamicFormGUID,        
        
        # 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
    )
    
    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)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        
    } #End begin
    
    process {        

        foreach ($guid in $DynamicFormGUID){
        #Uri
        $URI = "https://$CompanyName.helloid.com/api/v1/forms/$guid"        
        Invoke-RestMethod -Uri $URI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing        
        } #end foreach

    } #End process
    
    end {
    } #End end
} #End function



<#
.SYNOPSIS
    Removes a delegated form
.DESCRIPTION
    Removes a delegated form based on the guid
.PARAMETER DelegatedFormGuid
    Specifies the Guid of an existing delegated form to remove, can be specified as an array of strings to remove multiple variables
.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
    Remove-HiddelegatedForm -delegatedFormGUID "c31fd53a-b346-4bb4-9a67-f9406d3968db"

    Removes the delegated form with the specified GUID
.EXAMPLE
    Get-HiddelegatedForm -Name testform | Remove-HiddelegatedForm

    Gets the delegated form with name testform, and pipes the result to Remove-HiddelegatedForm which removes the form
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Remove-HidDelegatedForm {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # the GUID of an existing delegated form
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [alias("Guid")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$DelegatedFormGuid,        
        
        # 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
    )
    
    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)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        
    } #End begin
    
    process {        

        foreach ($guid in $DelegatedFormGuid){
        #Uri
        $URI = "https://$CompanyName.helloid.com/api/v1/delegatedforms/$guid"        
        Invoke-RestMethod -Uri $URI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing        
        } #end foreach

    } #End process
    
    end {
    } #End end
} #End function