Introduction
I needed to extract the number of IT service tickets created per day and the cumulative growth of the ticket database, and then plot this graphically. Such a graph would be of interest to organization users and management and also to IT staff, whose daily work efforts are primarily driven by user ticket submission. Developing this is very simple using just a bit of PowerShell. This posting shows you how.
Procedure
I needed to extract the number of IT service tickets created per day and the cumulative growth of the ticket database, and then plot this graphically. Such a graph would be of interest to organization users and management and also to IT staff, whose daily work efforts are primarily driven by user ticket submission. Developing this is very simple using just a bit of PowerShell. This posting shows you how.
Procedure
- First, you'll need to gather some key data that will be needed in the PowerShell script:
- Identify the path to the site hosting the ticket list.
- Identify the name of this list.
- Create a list to hold the ticket history metric data. At a minimum, it will need these fields:
- Ticket Create Date
- Tickets Created
- Cumulative Total
- Identify the site that will be hosting the ticket metrics list.
- Identify the name of this list you created.
- Determine the start and end dates of the history you want to examine.
- Next, on a farm server, open an elevated instance of the SharePoint Management Shell, and then execute the following script:
If ( (Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null ) { Add-PsSnapin Microsoft.SharePoint.PowerShell } # Get source list # StartDate Example: 1/1/2013 $StartDate = Get-Date "[your start date]" # Note that the start date actually retained is: # "1/1/2013 12:00 AM" # EndDate Example: 12/31/2013 $EndDate = Get-Date "[your end date]" # this actually enters the date as "12/31/2013 12:00 AM" # To get something that will work best for this simple code, # add a day, which will then give: "1/1/2014 12:00 AM" $EndDate = $EndDate.AddDay(1) $SourceWebURL = "http://[URL to your ticket site]" $SourceWebListName = "[name of ticket list]" $SourceWeb = Get-SPWeb $SourceWebURL $List = $SourceWeb.Lists[$SourceWebListName] $SourceWeb.Dispose() # Now filter out unwanted dates: $ListItems = $List.Items | Where {($_['Created'] -ge $StartDate) -and ($_['Created'] -lt $EndDate)} # note that the operator "lt" was used. This is all # that was needed, given the format of the EndDate. # Establish a connection to the Metrics list $SourceWebURL = "http://[URL to your metrics site]" $SourceWebListName = "[name of metrics list]" $SourceWeb = Get-SPWeb $SourceWebURL $MetricsList = $SourceWeb.Lists[$SourceWebListName] # Set looping variables $IncDate = $StartDate $CumulativeTotal = 0 # Begin loop While ($IncDate -lt $EndDate) { $NewItem = $MetricsList.Items.Add() $FilteredItems = $ListItems | Where {$_['Created'].ToShortDateString() -eq $IncDate.ToShortDateString()} $NumberCreated = $FilteredItems.Count $NewItem["TicketDate"] = $IncDate.ToShortDateString() $NewItem["NumberCreated"] = $NumberCreated $CumulativeTotal = $CumulativeTotal + $NumberCreated $NewItem["CumulativeTotal"] = $CumulativeTotal $NewItem.Update() Write-Host (" On {0}: {1}({2})" -f $IncDate.ToShortDateString(), $NumberCreated, $CumulativeTotal) $IncDate = $IncDate.AddDays(1) } $SourceWeb.Dispose() - Then add and configure chart web part:
- Add a chart web part to a page.
- Connect it to the metrics list
- Configure primary and secondary axes as desired.
- Lastly, an example
Here's an example chart showing both daily tickets created and also the cumulative total. Using separate axes is necessary here for otherwise the daily counts would not be visible .
References
- Changing Modified, and Created details in SharePoint
- Powershell and last modified date
- Windows PowerShell Tip of the Week: More fun with dates (and times)
- PowerShell Tutorial 9: Getting Loopy
- Calculated Columns In SharePoint 2010
Notes
- You would think that you could filter the returned set also using the date obtained from ToShortDateString(). However, I found this not to be the case: the list items that were filtered using this approach frequently included list items having valid dates that should have been included in the filtered set, but weren't. Using the full date avoided this problem and returned the expected set of list items. Why this was so, was not immediately apparent to me.
- TIP: when graphing the result listing, the full listing may contain periods you don't want included in the graph. For example, for a multi-year listing of tickets, you may want to graph just the results for a particular year. To accomplish this, add another column to the results list, say, Year, and then, when configuring the data connection for the Chart web part, filter on the desired year. This can even be automated by adding a Calculated column that calculates the value based upon the Created date of the ticket.
No comments:
Post a Comment