Create a Ticket in Autotask using a Powershell module

With our new PowerShell module for Autotask it is a lot easier to create a ticket in Autotask from automated scripts than before. Let us compare the old and the new way. This is how you had to do it if you created a WebServiceProxy object in PowerShell:

$Ticket = New-Object Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.Ticket

$Ticket.AccountID = 0 # The internal account for test. Required.
$Ticket.DueDateTime = '2015-12-31' # Any date. Required.
$Ticket.Priority = 2 # Medium. Required.
$Ticket.Status = 1 # New. Required
$Ticket.QueueId = 12345678 # Your Queue IDs are unique 
$Ticket.Title = 'Ticket title for a test ticket'

$Result = $atws.create($Ticket)

You had to specify Priority, Status and QueueId by their numerical values. It was possible to look them up using the API, but it wasn’t easy. With our PowerShell module, all picklist values are downloaded and included in any function for parameter validation. If you use a PowerShell editor with IntelliSense support (Visual Studio Code or PowerShell ISE) you can select any values from a dropdown list provided by the editor.

Here is the same code with our module:

# You can use PowerShell DateTime objects with any datetime parameter
$DueDate = Get-Date '2015-12-31'
$Ticket = New-AtwsTicket -AccountId 0 -DueDateTime $DueDate -Priority Medium -Status New -QueueId 'Queue name' -Title 'Ticket title for a test ticket'

# Or the same thing with parameter @splatting. Splatting makes your code readable, 
# but at the expense of IntelliSense suggestions for your parameters
$Parameters = @{
  AccountId = 0
  DueDateTime = $DueDate
  Priority = 'Medium'
  Status = 'New'
  QueueId = 'Queue name'
  Title = 'Ticket title for a test ticket'

$Ticket = New-AtwsTicket @Parameters

Let us add a ticket contact:

$Contact = Get-AtwsContact -EMailAddress '[email protected]'
$Ticket = New-AtwsTicket -AccountId $Contact.AccountId -ContactId $ `
  -DueDateTime $DueDate -Priority Medium -Status New -QueueId 'Queue name' `
  -Title 'Ticket title for a test ticket'

And a resource. When you add a resource to a ticket, a resource role is required:

$Resource = Get-AtwsResource -Username user
# If your resource has more than 1 role for a particular queue, 
# you will need to specify the Role Id as well
$Role = Get-AtwsRole -Name 'Role name'
$ResourceRole = Get-AtwsResourceRole -QueueId 'Queue name' -Resource $ -Role $
$Ticket = New-AtwsTicket -AccountId $Contact.AccountId -ContactId $ `
  -DueDateTime $DueDate -Priority Medium -Status New -QueueId 'Queue name' `
  -Title 'Ticket title for a test ticket' -AssignedResourceID $ `
  -AssignedResourceRoleID $

The Ticket object that is returned by the function is a copy of the actual ticket in Autotask. You can use it to get the system generated ticket number, for instance:

# Print the ticket number to output
Write-Output $Ticket.TicketNumber

Is is easy to get started with our module, as you can find it on PowershellGallery. On any machine with WMF 5+ (pre-installed on Windows 10 and Windows Server 2016) you can install it directly with:

# Sorry, our module is not signed
Set-ExecutionPolicy RemoteSigned
Install-Module Autotask

If you find it useful I would be happy to hear from you :)