{"id":298,"date":"2020-05-19T10:25:14","date_gmt":"2020-05-19T10:25:14","guid":{"rendered":"http:\/\/www.spktechfit.com\/?p=298"},"modified":"2020-05-19T10:39:53","modified_gmt":"2020-05-19T10:39:53","slug":"spiceworks-unassigned-tickets-report-with-powershell","status":"publish","type":"post","link":"https:\/\/www.spktechfit.com\/?p=298","title":{"rendered":"Spiceworks: Unassigned tickets report with PowerShell"},"content":{"rendered":"<p>An &#8220;Unassigned Tickets&#8221; report is very useful for making sure everyday that all tickets are being picked up and not left sitting unassigned. Spiceworks has the built in report functionality which are great, but the only issue I have with them is that you cant put report content directly in an email, it sends it as an attachment. So I created my own report using PowerShell.<\/p>\n<h2>Step 1: Create the SQL Query<\/h2>\n<p>This is a straightforward query that gets all unassigned tickets that are open:<\/p>\n<p><em>select <\/em><br \/>\n<em>tickets.id as &#8216;Ticketid&#8217;<\/em><br \/>\n<em>,&#8217;http:\/\/helpdesk.stbedes.catholic.edu.au\/tickets\/v2#single_ticket\/&#8217;||tickets.id AS &#8216;TicketLink&#8217;<\/em><br \/>\n<em>, (select email as &#8216;Created By&#8217; from users where tickets.created_by=users.id) AS &#8216;CreatedBy&#8217;<\/em><br \/>\n<em>, summary AS Summary<\/em><br \/>\n<em>, round(julianday(&#8216;now&#8217;) &#8211; julianday(tickets.created_at),2) AS &#8216;DaysOpen&#8217;<\/em><br \/>\n<em>from tickets <\/em><br \/>\n<em>WHERE assigned_to is null AND Status = &#8216;open&#8217;<\/em><\/p>\n<h2>Step 2: Setup the PowerShell script<\/h2>\n<p>This does the following:<\/p>\n<ol>\n<li>Import the PSSQLite module to query the database<\/li>\n<li>Execute the query<\/li>\n<li>Build the HTML email using the results found<\/li>\n<li>Send the email with contents in an HTML table<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>Powershell code:<\/strong><\/p>\n<p>==================================<br \/>\n#Import the PSSQLite module<br \/>\nImport-Module PSSQLite<\/p>\n<p>#Set the source to your database<br \/>\n$DataSource = PAth\\to\\DAtabase&#8221;<\/p>\n<p>#define your query<br \/>\n$Query = &#8220;select<br \/>\ntickets.id as &#8216;Ticketid&#8217;<br \/>\n,&#8217;http:\/\/SpiceworksServer\/tickets\/v2#single_ticket\/&#8217;||tickets.id AS &#8216;TicketLink&#8217;<br \/>\n, (select email as &#8216;Created By&#8217; from users where tickets.created_by=users.id) AS &#8216;CreatedBy&#8217;<br \/>\n, summary AS Summary<br \/>\n, round(julianday(&#8216;now&#8217;) &#8211; julianday(tickets.created_at),2) AS &#8216;DaysOpen&#8217;<br \/>\nfrom tickets<br \/>\nWHERE assigned_to is null AND Status = &#8216;open'&#8221;<\/p>\n<p>#Execute the query and export results to CSV<br \/>\n$ReportResults = Invoke-SqliteQuery -DataSource $DataSource -Query $Query<\/p>\n<p>#Check if the query returns anything (i.e. if there are unassigned tickets.) if not, exit<br \/>\nIf ($ReportResults -eq $null)<br \/>\n{<br \/>\nWrite-Host &#8220;No unassiagned tickets. Well done&#8221;<br \/>\nexit<br \/>\n}<\/p>\n<p>$HtmlTable1 =&#8221;&#8221;<br \/>\n# Create column headers of Table1<br \/>\n$HtmlTable1 = &#8220;&lt;table border=&#8217;1&#8242; align=&#8217;Left&#8217; cellpadding=&#8217;2&#8242; cellspacing=&#8217;0&#8242; style=&#8217;color:black;font-family:arial,helvetica,sans-serif;text-align:left;&#8217;&gt;<br \/>\n&lt;tr style =&#8217;font-size:13px;font-weight: normal;background: #FFFFFF&#8217;&gt;<br \/>\n&lt;th align=left&gt;&lt;b&gt;Ticket #&lt;\/b&gt;&lt;\/th&gt;<br \/>\n&lt;th align=left&gt;&lt;b&gt;Summary&lt;\/b&gt;&lt;\/th&gt;<br \/>\n&lt;th align=left&gt;&lt;b&gt;Created By&lt;\/b&gt;&lt;\/th&gt;<br \/>\n&lt;th align=left&gt;&lt;b&gt;Days Open&lt;\/b&gt;&lt;\/th&gt;<br \/>\n&lt;\/tr&gt;&#8221;<\/p>\n<p># Insert data into Table1<br \/>\nforeach ($row in $ReportResults)<br \/>\n{<br \/>\n$HtmlTable1 += &#8220;&lt;tr style=&#8217;font-size:13px;background-color:#FFFFFF&#8217;&gt;<br \/>\n&lt;td&gt;&lt;a href='&#8221; + $row.&#8217;ticketlink&#8217; + &#8220;&#8216;&gt;&#8221; + $row.&#8217;ticketid&#8217; + &#8220;&lt;\/a&gt;&lt;\/td&gt;<br \/>\n&lt;td&gt;&#8221; + $row.&#8217;summary&#8217; + &#8220;&lt;\/td&gt;<br \/>\n&lt;td&gt;&#8221; + $row.&#8217;CreatedBy&#8217; + &#8220;&lt;\/td&gt;<br \/>\n&lt;td&gt;&#8221; + $row.&#8217;DaysOpen&#8217; + &#8220;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;&#8221;<br \/>\n}<br \/>\n$HtmlTable1 += &#8220;&lt;\/table&gt;&#8221;<\/p>\n<p>#Email variables<br \/>\n$To = @(&#8220;name@yourdomain&#8221;)<br \/>\n$From = &#8220;name@yourdomain&#8221;<br \/>\n$SMTPServer = &#8220;mailserver&#8221;<\/p>\n<p>$TodaysDate = (Get-Date).ToString(&#8216;dd-MM-yyyy&#8217;)<\/p>\n<p>#send the email<br \/>\nSend-MailMessage -to $To -subject &#8220;Report: Unassigned Tickets $TodaysDate&#8221; -Body $HtmlTable1 -BodyAsHtml -SmtpServer $smtpserver -From $from<\/p>\n<p>=======================<\/p>\n<p>&nbsp;<\/p>\n<h2>Step 3: Create A Schedule task to run the report<\/h2>\n<ol>\n<li>Create a &#8220;Basic Scheduled task&#8221;<\/li>\n<li>Set your scheduled time<\/li>\n<li>Set the Action as &#8220;Start a program&#8221;<\/li>\n<li>set the &#8220;Program\/Script&#8221; as: Powershell.exe<\/li>\n<li>Add the arguments: -file &#8220;\\\\Server\\Directory\\UnassignedTickets.ps1&#8221;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>An &#8220;Unassigned Tickets&#8221; report is very useful for making sure everyday that all tickets are being picked up and not left sitting unassigned. Spiceworks has the built in report functionality which are great, but the only issue I have with them is that you cant put report content directly in an email, it sends it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":304,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,9,3],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts\/298"}],"collection":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=298"}],"version-history":[{"count":4,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions\/305"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/media\/304"}],"wp:attachment":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}