Skip to content

Commit

Permalink
Update a variety of logging output. Add IronScales mapping interface.…
Browse files Browse the repository at this point in the history
… Increase Autotask filtering to customers only
  • Loading branch information
Troy Harshberger committed Feb 23, 2024
1 parent 7fdf3f8 commit a642e61
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Function Invoke-ExecExtensionMapping {
$Body = Get-AutotaskManaged -CIPPMapping $Table
}

'IronScales' {
$Body = Get-IronScalesMapping -CIPPMapping $Table
}

'Halo' {
$Body = Get-HaloMapping -CIPPMapping $Table
}
Expand All @@ -49,7 +53,11 @@ Function Invoke-ExecExtensionMapping {

'AutotaskManaged' {
$Body = Set-AutotaskManaged -CIPPMapping $Table -APIName $APIName -Request $Request
}
}

'IronScales' {
$Body = Set-IronScalesMapping -CIPPMapping $Table -APIName $APIName -Request $Request
}

'Halo' {
$body = Set-HaloMapping -CIPPMapping $Table -APIName $APIName -Request $Request
Expand Down Expand Up @@ -87,8 +95,6 @@ Function Invoke-ExecExtensionMapping {
$body = [pscustomobject]@{'Results' = "Failed. $($_.Exception.Message)" }
}

Write-Host $(ConvertTo-Json $Body -Depth 5)

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Expand Down
2 changes: 1 addition & 1 deletion Modules/CippExtensions/Autotask/Get-AutotaskManaged.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Get-AutotaskManaged {
if($null -ne $_.AutotaskPSAName -and "" -ne $_.AutotaskPSAName){
[PSCustomObject]@{
name = "$($_.AutotaskPSAName)"
value = Get-ManagedState $_
value = [bool](Get-ManagedState $_)
aid = "$($_.AutotaskPSA)"
}
}
Expand Down
9 changes: 5 additions & 4 deletions Modules/CippExtensions/Autotask/Get-AutotaskMapping.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ function Get-AutotaskMapping {
$Configuration = ((Get-CIPPAzDataTableEntity @Table).config | ConvertFrom-Json -ea stop).Autotask

Get-AutotaskToken -configuration $Configuration | Out-Null
$RawAutotaskCustomers = Get-AutotaskAPIResource -Resource Companies -SimpleSearch "isactive eq $true"
$RawAutotaskCustomers = Get-AutotaskAPIResource -Resource Companies -SearchQuery "{'filter':[{'op':'and',items:[{'op':'eq','field':'isactive','value':true},{'op':'eq','field':'companyType','value':'1'}]}]}"
} catch {
$Message = if ($_.ErrorDetails.Message) {
Get-NormalizedError -Message $_.ErrorDetails.Message
} else {
$_.Exception.message
}

Write-LogMessage -Message "Could not get Autotask Clients, error: $Message " -Level Error -tenant 'CIPP' -API 'HaloMapping'
Write-LogMessage -Message "Could not get Autotask Clients, error: $Message " -Level Error -tenant 'CIPP' -API 'AutotaskMapping'
$RawAutotaskCustomers = @(@{name = "Could not get Autotask Clients, error: $Message" })
}

$AutotaskCustomers = $RawAutotaskCustomers| ForEach-Object {
$AutotaskCustomers = $RawAutotaskCustomers | Sort-Object -Property companyName | ForEach-Object {
[PSCustomObject]@{
name = $_.companyName
value = "$($_.id)"
}
}

$Tenants = Get-Tenants -IncludeAll
$Tenants = Get-Tenants

$MappingObj = [PSCustomObject]@{
Tenants = @($Tenants)
AutotaskCustomers = @($AutotaskCustomers)
Expand Down
4 changes: 2 additions & 2 deletions Modules/CippExtensions/Autotask/New-AutotaskTicket.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#Hints on int values below
function New-AutotaskTicket {
param($atCompany, $title, $description, $estHr = 0.1)
param($atCompanyId, $title, $description, $estHr = 0.1)

try{
Get-AutotaskToken -configuration $Configuration.Autotask | Out-Null

$ticket = New-AutotaskBody -Resource Tickets -NoContent
$ticket.Id = "0" #Always 0 for a new ticket
$ticket.ticketType = "1"
$ticket.companyId = "$($atCompany.id)"
$ticket.companyId = "$($atCompanyId)"
$ticket.priority = "2"
$ticket.ticketCategory = "3"
$ticket.ticketType = "5"
Expand Down
2 changes: 1 addition & 1 deletion Modules/CippExtensions/Autotask/Set-AutotaskManaged.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Set-AutotaskManaged {
Update-AzDataTableEntity @Table -Entity @{
PartitionKey = $res.PartitionKey
RowKey = $res.RowKey
'IsManaged' = "$($mapping.value)"
'IsManaged' = [bool]$mapping.value
}

Write-LogMessage -API $APINAME -user $request.headers.'x-ms-client-principal' -message "Updated 'IsManaged' mapping for $($mapping.name)." -Sev 'Info'
Expand Down
1 change: 0 additions & 1 deletion Modules/CippExtensions/Autotask/Set-AutotaskMapping.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function Set-AutotaskMapping {
)

foreach ($Mapping in ([pscustomobject]$Request.body.mappings).psobject.properties) {
Write-Host $(ConvertTo-Json $Mapping)
$AddObject = @{
PartitionKey = 'Mapping'
RowKey = "$($mapping.name)"
Expand Down
Binary file modified Modules/CippExtensions/CippExtensions.psd1
Binary file not shown.
10 changes: 4 additions & 6 deletions Modules/CippExtensions/IronScales/Get-IronScalesIncidents.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,23 @@ function New-IronScalestickets {

Get-AutotaskToken -configuration $Configuration.Autotask

$FulcraATCompany = Get-AutotaskAPIResource -resource Companies -SimpleSearch "companyname beginswith Fulcra"
$managed_issues_body = @()

foreach($company in $IronScalesIncidents) {
$AtCompany = $MappingFile | Where-Object { $_.AutotaskPSAName -eq $company.customername }
$AtCompany = $MappingFile | Where-Object { $_.IronScalesId -eq $company.Id }

if($company.companyName -eq 'Fulcra Networks'){
$managed_issues_body += Get-BodyForTicket $company
}
elseif($null -eq $AtCompany){
Write-LogMessage -API 'IronScales' -tenant 'none' -message "IronScales company $($company.customername) does not have corresponding Autotask company." -Sev Info
Write-LogMessage -API 'IronScales' -tenant 'none' -message "IronScales company $($company.customername) is not mapped." -Sev Info
continue
}
elseif($AtCompany.IsManaged) {
$managed_issues_body += Get-BodyForTicket $company
}
else {
Write-LogMessage -API 'IronScales' -tenant 'none' -message "Creating Autotask ticket for IronScales company $($company.customername)" -Sev Info
$ATCompany = Get-AutotaskAPIResource -resource Companies -SimpleSearch "companyname beginswith $($company.Customername.Substring(0,4))"
$tTitle = "[IronScales] New Incident(s) for $($company.CustomerName)"

if(Get-ExistingTicket $tTitle){
Expand All @@ -59,7 +57,7 @@ function New-IronScalestickets {

$body = Get-BodyForTicket $company
$estHr = 0.1*$company.Incidents.Count
New-AutotaskTicket -atCompany $ATCompany `
New-AutotaskTicket -atCompany $ATCompany.AutotaskPSA `
-title $tTitle `
-description ($body|Join-String) -estHr $estHr
}
Expand All @@ -73,7 +71,7 @@ function New-IronScalestickets {
continue
}

New-AutotaskTicket -atCompany $FulcraATCompany `
New-AutotaskTicket -atCompany 0 `
-title $mTitle `
-description ($managed_issues_body|Join-String)
}
Expand Down
51 changes: 51 additions & 0 deletions Modules/CippExtensions/IronScales/Get-IronScalesMapping.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Get-IronScalesMapping {
[CmdletBinding()]
param (
$CIPPMapping
)
#Get available mappings
$Mappings = [pscustomobject]@{}

$Filter = "PartitionKey eq 'Mapping'"
Get-CIPPAzDataTableEntity @CIPPMapping -Filter $Filter | ForEach-Object {
if($null -ne $_.IronScalesName -and "" -ne $_.IronScalesName){
$Mappings | Add-Member -NotePropertyName $_.RowKey -NotePropertyValue @{ label = "$($_.IronScalesName)"; value = "$($_.IronScalesId)" }
}
}

$Table = Get-CIPPTable -TableName Extensionsconfig
try {
$Configuration = ((Get-CIPPAzDataTableEntity @Table).config | ConvertFrom-Json -ea stop).IronScales

$JWT = Get-IronScalesToken -configuration $configuration
$reqargs = @{ Uri = "$($Configuration.apiHost+"/company/list/")"; Headers = @{ Authorization = "Bearer $($JWT)"; }}
$resp = Invoke-RestMethod @reqargs
$RawCompanies = $resp.companies
} catch {
$Message = if ($_.ErrorDetails.Message) {
Get-NormalizedError -Message $_.ErrorDetails.Message
} else {
$_.Exception.message
}

Write-LogMessage -Message "Could not get IronScales Companies error: $Message " -Level Error -tenant 'CIPP' -API 'IronScalesMapping'
$RawCompanies = @(@{name = "Could not get IronScales Companies, error: $Message" })
}

$IronScalesCompanies = $RawCompanies | Sort-Object -Property name | ForEach-Object {
[PSCustomObject]@{
name = $_.name
value = "$($_.id)"
}
}

$Tenants = Get-Tenants

$MappingObj = [PSCustomObject]@{
Tenants = @($Tenants)
IronScalesCompanies = @($IronScalesCompanies)
Mappings = $Mappings
}

return $MappingObj
}
37 changes: 37 additions & 0 deletions Modules/CippExtensions/IronScales/Set-IronScalesMapping.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Set-IronScalesMapping {
[CmdletBinding()]
param (
$CIPPMapping,
$APIName,
$Request
)

foreach ($Mapping in ([pscustomobject]$Request.body.mappings).psobject.properties) {
$Filter = "PartitionKey eq 'Mapping' and RowKey eq '$($mapping.name)'"
$res = Get-CIPPAzDataTableEntity @CIPPMapping -Filter $Filter
if($null -ne $res){
Write-LogMessage -API $APINAME -user $request.headers.'x-ms-client-principal' -message "Updated mapping for $($mapping.name)." -Sev 'Info'
Update-AzDataTableEntity @CIPPMapping -Entity @{
PartitionKey = $res.PartitionKey
RowKey = $res.RowKey
'IronScalesName' = "$($Mapping.value.label)"
'IronScalesId' = "$($Mapping.value.value)"
}
}
else {
Write-LogMessage -API $APINAME -user $request.headers.'x-ms-client-principal' -message "Added mapping for $($mapping.name)." -Sev 'Info'

$AddObject = @{
PartitionKey = 'Mapping'
RowKey = "$($mapping.name)"
'IronScalesId' = "$($mapping.value.value)"
'IronScalesName' = "$($mapping.value.label)"
}

Add-CIPPAzDataTableEntity @CIPPMapping -Entity $AddObject -Force
}
}
$Result = [pscustomobject]@{'Results' = "Successfully edited mapping table." }

Return $Result
}

0 comments on commit a642e61

Please sign in to comment.