Skip to main content

Hi Freshworks community.

 

I am currently in the midst of updating my company’s offboarding powershell script, and I would like to have the script create a private note with an attachment of the report the script makes, when the user has been offboarded.

I have tried to follow multiple different ways both from the API documentation and from here/stackoverflow but haven’t been able to make it work.

I always end up getting either an Error 400 or Error 500 when I try to use the code.

I have been using the content-type multipart/form-data, and tried with both Invoke-WebRequest and Invoke-RestMethod to no prevail.

Hi Freshworks community.

 

I am currently in the midst of updating my company’s offboarding powershell script, and I would like to have the script create a private note with an attachment of the report the script makes, when the user has been offboarded.

I have tried to follow multiple different ways both from the API documentation and from here/stackoverflow but haven’t been able to make it work.

I always end up getting either an Error 400 or Error 500 when I try to use the code.

I have been using the content-type multipart/form-data, and tried with both Invoke-WebRequest and Invoke-RestMethod to no prevail.

https://api.freshservice.com/v2/#create_a_note you used that ?


Hi Freshworks community.

 

I am currently in the midst of updating my company’s offboarding powershell script, and I would like to have the script create a private note with an attachment of the report the script makes, when the user has been offboarded.

I have tried to follow multiple different ways both from the API documentation and from here/stackoverflow but haven’t been able to make it work.

I always end up getting either an Error 400 or Error 500 when I try to use the code.

I have been using the content-type multipart/form-data, and tried with both Invoke-WebRequest and Invoke-RestMethod to no prevail.

https://api.freshservice.com/v2/#create_a_note you used that ?

Yes, I have tried using that, but to no prevail.


Can you ost your command line for us ?


Can you ost your command line for us ?

This is the function I’m using currently

function Attach-HelpdeskTicket ($TicketID, $UserName, $ApiKey, $isPrivate, $attachmentPath, $Uri) # Adds a private note on the offboard ticket with the decommission report attached
{
    
    Write-Host "`r`nAttaching Report to Ticket: $TicketID"
    
    #Create Auth "token"
    $Pair = "$($ApiKey):$($null)"
    $EncodedCreds = System.Convert]::ToBase64String(eSystem.Text.Encoding]::ASCII.GetBytes($Pair))
    $BasicAuthValue = "Basic $EncodedCreds"
    
    #Create HEADER
    $Headers = @{
        "Content-Type"  = "multipart/form-data"
        "Authorization" = $BasicAuthValue
    }
    
    #Enable TLS1.2
    Net.ServicePointManager]::SecurityProtocol = bNet.SecurityProtocolType]::Tls12
    
    $Form = @{
        "body"            = "Offboarding completed for $UserName Please read and verify the report to make sure $UserName has been offboarded correctly"
        "private"        = "true"
        "attachmentse]" = Get-Item $attachmentPath
    }
    
    try
    {
        Invoke-RestMethod -Method POST -Uri "https://$Uri/api/v2/tickets/$TicketID/notes" -Headers $Headers -Form $Form
        Write-Host "Attached decommission report for $UserName on ticket $TicketID"
    }
    catch
    {
        Write-Host "Could not attach the decommission report for $UserName on the ticket $TicketID"
    }
    
}

 


Just anormal not without attachments works ?
You tested this ?

Invoke-WebRequest -uri $uri -Method Put -Infile $picPath

Yes, doesn’t work without the attachments aswell.

nor with 

Invoke-WebRequest -uri $uri -Method Put -Infile $picPath

 


Long time since I used PS but I checked MS support doc and 

Shouldn’t the form look like this ?

$Form = @{
firstName = 'John'
lastName = 'Doe'
email = 'john.doe@contoso.com'
avatar = Get-Item -Path 'c:\Pictures\jdoe.png'
birthday = '1980-10-15'
hobbies = 'Hiking','Fishing','Jogging'
}

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.4#example-4-simplified-multipart-form-data-submission

 

 $Form = @{
body = "Offboarding completed for $UserName Please read and verify the report to make sure $UserName has been offboarded correctly"
private = "true"
}