Search code examples
powershellpowershell-2.0powershell-3.0freshdesk

Reset Powershell Form after User Submits Request


How can I get a form to reset/close after the user has successfully submitted their request? I'm testing using Powershell ise and the script never closes until I actually X out. Here are my current functions.

My form is working like it should thanks to the folks on this forum. I have another issue I'm struggling with. How do I get the form to reset after the user hits the submit button? I currenlty have to exit out of the form for the script to end.

#region gui events {
$btn1.Add_Click({ sendRequest; thankyou })
#endregion events }

#endregion GUI }
function sendRequest()
{
    # API Key
    $FDApiKey="api key"
    #################################################

    # Force TLS1.2 as Powershell defaults to TLS 1.0 and Freshdesk will fail connections 
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12

    # Prep
    $pair = "$($FDApiKey):$($FDApiKey)"
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
    $base64 = [System.Convert]::ToBase64String($bytes)
    $basicAuthValue = "Basic $base64"
    $FDHeaders = @{ Authorization = $basicAuthValue }
    ##################################################

    $Body = @{
        description = $description.Text
        email = $email.Text
        subject = $subject.Text
        type = $request.Text
        priority = 1
        status = 2
    }

    Invoke-WebRequest "https://clasd.freshdesk.com/api/v2/tickets/" `
        -Headers $FDHeaders `
        -ContentType "application/json" `
        -Method Post `
        -Body ($Body | ConvertTo-JSON)
}

function thankyou ()
{
    [System.Windows.Forms.MessageBox]::Show("Your ticket has been submitted!" , "Status") 
}

#Write your logic code here

[void]$Form.ShowDialog()

Solution

  • Without seeing all the controls that are on your form it is not really possible to give you complete code but this should give you the basic idea.

    function thankyou{
      [System.Windows.Forms.MessageBox]::Show("Your ticket has been submitted!" , "Status")
      $description.Text = ''
      $email.Text = ''
      $subject.Text = ''
      $request.Text = ''
    }