Creating service requests via powershell

Use case: auto creating vulnerability tickets for techs from a csv file. If anyone can help :)

I have a script that works well enough BUT

The last items I am struggling with is:

How do I set the usertext1 am hoping for it to be the hostname(in extensions tab) and how to set the affected user.

For affected user I would to include a domain user but if an easier solution is found like creating a user scsm - I made a Firstname: Security Lastname: Vulnerability user inside config items/ users

The last item was to combine the hostname's if they are the same and make the description different:

IE if only one is found:

CVSS sore:

Title:

Proof URL:

If more than one is found:

Title 1:

Proof url 1:

Title 2:

Proof url 2:

ECT

My Script below only omitting confidential info:

# Set default computer for SMlets
$GLOBAL:smdefaultcomputer = "test server"

# Import SMlets module
Import-Module SMlets

# Get SCSM class and enumerations
$srClass = Get-SCSMClass -name System.WorkItem.ServiceRequest$
$srprior = Get-SCSMEnumeration -Name ServiceRequestPriorityEnum.Medium
$srurg = Get-SCSMEnumeration -Name ServiceRequestUrgencyEnum.Medium
$ararea = get-SCSMEnumeration -Name Enum.add3768303064ec18890170ba33cffda
$source = Get-SCSMEnumeration -name Enum.c933f53299cd460c867871e0662be07d
#$affectedUser = Get-SCSMObject -Class (Get-SCSMClass -Name System.Domain.User$) -Filter "UserName -eq '*Vulnerability*"

# Read in CSV file
$csvPath = "C:\test\data.csv"
$requests = Import-Csv -Path $csvPath

# Initialize the result list
$resultList = @()

# Initialize previous host name and description list
$previousHostName = ""
$descriptionList = @()

# Loop through each row in CSV file and create service request
foreach ($request in $requests) {
  # Check for blank row or host_name with "Total"
  if ([string]::IsNullOrWhiteSpace($request.Title) -or [string]::IsNullOrWhiteSpace($request.host_name) -or $request.host_name -eq "Total") {
    continue
  }

  # Extract data from CSV row
  $title = $request.Title
  $descrip = $request.Description
  $hostName = $request.host_name

  # Check for host_name conditions
  $supportGroup = "Enum.7d3ab9057e1040248c4fc3b9998351ff"
  if ($hostName -match "SHR") {
    $supportGroup = "Enum.a8a0f089027f4b74ad15e8cfe39f8c86"
  } elseif ($hostName -match "SQL") {
    $supportGroup = "Enum.5cc39a482dab48e186615223f4d7e4ff"
  }

  # Set the due date
  $today = Get-Date
  $sloCountdown = [int]$request.'SLO Countdown'
  $dueDate = $today.AddDays($(if ($sloCountdown -lt 0) { 7 } else { $sloCountdown }))

  # Set the title
  $title = "vuln - $title on $hostName custodian: $($request.'SCSM Custodian') - Due date: $($dueDate.ToString('yyyy-MM-dd'))"

  # Set the description
  if ($hostName -eq $previousHostName) {
    $descriptionList += "Title: $($request.Title) Proof URL: $($request.'Proof URL')"
    $description = "Proof 1 CVSS score: $($request.RCS_CVSS) $($descriptionList -join '; ')"
  } else {
    $descriptionList = @("Title: $($request.Title) Proof URL: $($request.'Proof URL')")
    $description = "Proof 1 CVSS score: $($request.RCS_CVSS) $($descriptionList -join '; ')"
  } # This is the missing closing brace

  # Set service request arguments
 $srargs = @{
  Title = $title;
  Urgency = $srurg;
  Priority = $srprior;
  ID = "SR{0}";
  Area = $ararea;
  SupportGroup = $supportGroup;
  Description = $description;
  #AffectedUser = $affectedUser;
  Status = "New";
}

try {
  # Create service request
  $newServiceRequest = New-SCSMObject -Class $srClass -PropertyHashtable $srargs -PassThru
  $SRId = $newServiceRequest.id
  $srTypeProjection = Get-SCSMTypeProjection -name System.WorkItem.ServiceRequestProjection$
  $SRProj = Get-scsmobjectprojection -ProjectionName $srTypeProjection.Name -filter “Id -eq $SRId”

  # Output confirmation message
  Write-Host "Created service request $($newServiceRequest.Id) for $title"
  $resultList += "Created service request $($newServiceRequest.Id) for $title"
} catch {
  $errorMessage = "Error creating service request for $title`n$($_.Exception.Message)"
  Write-Host $errorMessage -ForegroundColor Red
  $resultList += $errorMessage
}

# Update previous host name
$previousHostName = $hostName
}

# Generate a timestamp for the filename
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"

# Generate a text file with the results
$outputPath = "C:\Users\rkelley\Documents\results-$timestamp.txt"
$resultList | Out-File -FilePath $outputPath

#Delete the CSV file
#Remove-Item $csvPath

# Output final result summary
Write-Host "The script has completed. The results have been saved to $outputPath."
Write-Host "The CSV file has been deleted."

8 replies