Home > Software engineering >  Trying to export tickets/incidents from SCSM to csv file using powershell
Trying to export tickets/incidents from SCSM to csv file using powershell

Time:02-10

Hi everyone need some help hopefully its simple. I am a System Analyst for our company and I am trying to do a simple ticket/incident file export using powershell. All of our tickets go into the Service manager 2019 Console.

What I am trying to do is be able to filter and export our tickets using certain date range. Our senior system engineer was able to help me get started and I am trying to figure out the best way to do this. So this is what he sent me:

____________________________________________ you always want to filter / where as far left as possible to reduce processing overhead also Tab is your friend when typing cmdlets - usually something like Get-SCSMIncident -<Tab> and it will show you your options or Get-SCSMIncident -Help you can also use Where-Object to filter once you have the correct subitems Get-SCSMIncident | Where-Object {$_.Status -eq "Active"} because you're doing the filter AFTER Get-SCSMIncident, it's going to find ALL incidents in the background, THEN filter them (slow/bad) ____________________________________________

So I tried a few things. He suggested to do the following below, create variables, store them and pull the data later.

$allincidents = Get-SCSMIncident $resolved = $allincdients | Where-Object {$_.Status -eq "Resolved"} $active = $allincdients | Where-Object {$_.Status -eq "Active"

Then I would export the info such as below to a csv file

$active | export-csv c:\temp\scsm_incidents.csv -nti

The issue is that when I execute it, the initial storing of the variables it is taking too long, because we have obviously thousands and thousands of tickets.

I then thought what if I did the following below Create the date variables first and store them.

$startDate = Get-Date -Year 2022 -Month 1 -Day 1

$endDate = Get-Date -Year 2022 -Month 2 -Day 2

Get-SCSMIncident | Where-Object {($_.createddate.date -le $endDate) -and ($_.createddate.date -ge $startDate)} | Export-Csv C:\Temp\SCSM-tickets.csv -nti

And given the logic that my Senior Engineer told me, it is going through all the tickets first because of the Get-SCSMIncident and then filtering and then storing into an csv file.

So my question is there a way to sort of go backwards? I know computer language wise it wouldn't make sense because it doesn't know what object it is being pointed to.

Like for example

Where-Object {($_.createddate.date -le $endDate) -and ($_.createddate.date -ge $startDate)} | Get-SCSMIncident | Export-Csv C:\Temp\SCSM-tickets.csv -nti

The end result is that I want to be able to pull data on a weekly basis using just a date range, without have to run through all the tickets every time. Since new tickets are being generated everyday, each time I run through it, it will take longer and longer and longer. I am by no means expert with powershell at all and looking for any insight on export data files much simpler or faster. If anyone has any ideas I would greatly appreciate it.

FYI I know I can pull each ticket at a time, our naming scheme used is INC##### so for example to pull any ticket

Get-SCSMIncident -id "INC10105"

This would pull up this one ticket in powershell.
I don't know all the powershell commands and searching through the library is confusing

If anyone knows a way of how to do something like this pulling tickets in sets that would be helpful.

Get-SCSMIncident -id "INC00001" TO -id "INC00500" | Export-Csv C:\Temp\SCSM-tickets.csv -nti

Or evening pulling data by date.

Apologize for the super long post. Also if anyone knows how to export tickets in Service Manager Console please let me know too!!! I searched everywhere and seems like I can't export anything

CodePudding user response:

As stated in my comments, unless the cmdlet Get-SCSMIncident has filtering by DateTime range capabilities, it is unlikely that there is a way around having to go through all the collection, however, there are ways the code could be improved so it can do the filtering faster. A foreach loop (enumeration) loop in addition to an if condition (filtering condition) is much faster than Where-Object.

$startDate = Get-Date -Year 2022 -Month 1 -Day 1
$endDate = Get-Date -Year 2022 -Month 2 -Day 2

$incs = foreach($inc in Get-SCSMIncident)
{
    if($inc.CreatedDate -gt $endDate -or $inc.CreatedDate -lt $startDate)
    {
        # If the Incident's CreatedDate is greater than `$endDate` OR
        # lower than `$startDate` go to the next Incident.
        # In other words, skip this Incident.
        continue
    }

    [pscustomobjct]@{
        ID           = $inc.ID
        Status       = $inc.Status
        Title        = $inc.Title
        Description  = $inc.Description -replace '\r?\n', ' '
        AffectedUser = $inc.AffectedUser
        AssignedTo   = $inc.AssignedTo
        CreatedDate  = $inc.CreatedDate
        TierQueue    = $inc.TierQueue
        Urgency      = $inc.Urgency
        Priority     = $inc.Properity
    }
}

$incs | Export-Csv path/to/exported.csv -NoTypeInformation
  • Related