diff --git a/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecExtensionMapping.ps1 b/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecExtensionMapping.ps1 index dcf5feb7605a..a903fefa499e 100644 --- a/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecExtensionMapping.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecExtensionMapping.ps1 @@ -25,6 +25,10 @@ Function Invoke-ExecExtensionMapping { $Body = Get-AutotaskManaged -CIPPMapping $Table } + 'IronScales' { + $Body = Get-IronScalesMapping -CIPPMapping $Table + } + 'Halo' { $Body = Get-HaloMapping -CIPPMapping $Table } @@ -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 @@ -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 diff --git a/Modules/CippExtensions/Autotask/Get-AutotaskManaged.ps1 b/Modules/CippExtensions/Autotask/Get-AutotaskManaged.ps1 index 1fa2704d6bde..20f53cc43581 100644 --- a/Modules/CippExtensions/Autotask/Get-AutotaskManaged.ps1 +++ b/Modules/CippExtensions/Autotask/Get-AutotaskManaged.ps1 @@ -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)" } } diff --git a/Modules/CippExtensions/Autotask/Get-AutotaskMapping.ps1 b/Modules/CippExtensions/Autotask/Get-AutotaskMapping.ps1 index 4893fded7cdd..b5d096c22c80 100644 --- a/Modules/CippExtensions/Autotask/Get-AutotaskMapping.ps1 +++ b/Modules/CippExtensions/Autotask/Get-AutotaskMapping.ps1 @@ -18,7 +18,7 @@ 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 @@ -26,18 +26,19 @@ function Get-AutotaskMapping { $_.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) diff --git a/Modules/CippExtensions/Autotask/New-AutotaskTicket.ps1 b/Modules/CippExtensions/Autotask/New-AutotaskTicket.ps1 index 58c226174a74..0d06f44bb2bc 100644 --- a/Modules/CippExtensions/Autotask/New-AutotaskTicket.ps1 +++ b/Modules/CippExtensions/Autotask/New-AutotaskTicket.ps1 @@ -1,6 +1,6 @@ #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 @@ -8,7 +8,7 @@ function New-AutotaskTicket { $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" diff --git a/Modules/CippExtensions/Autotask/Set-AutotaskManaged.ps1 b/Modules/CippExtensions/Autotask/Set-AutotaskManaged.ps1 index 1f7bec34ce10..0b1f1ebffe30 100644 --- a/Modules/CippExtensions/Autotask/Set-AutotaskManaged.ps1 +++ b/Modules/CippExtensions/Autotask/Set-AutotaskManaged.ps1 @@ -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' diff --git a/Modules/CippExtensions/Autotask/Set-AutotaskMapping.ps1 b/Modules/CippExtensions/Autotask/Set-AutotaskMapping.ps1 index a12f6e7fc36c..10a5e57b5431 100644 --- a/Modules/CippExtensions/Autotask/Set-AutotaskMapping.ps1 +++ b/Modules/CippExtensions/Autotask/Set-AutotaskMapping.ps1 @@ -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)" diff --git a/Modules/CippExtensions/CippExtensions.psd1 b/Modules/CippExtensions/CippExtensions.psd1 index 3895ef233886..ec36d2d5eaa7 100644 Binary files a/Modules/CippExtensions/CippExtensions.psd1 and b/Modules/CippExtensions/CippExtensions.psd1 differ diff --git a/Modules/CippExtensions/IronScales/Get-IronScalesIncidents.ps1 b/Modules/CippExtensions/IronScales/Get-IronScalesIncidents.ps1 index 852b67ea4e94..5b8692f16ffc 100644 --- a/Modules/CippExtensions/IronScales/Get-IronScalesIncidents.ps1 +++ b/Modules/CippExtensions/IronScales/Get-IronScalesIncidents.ps1 @@ -30,17 +30,16 @@ 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) { @@ -48,7 +47,6 @@ function New-IronScalestickets { } 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){ @@ -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 } @@ -73,7 +71,7 @@ function New-IronScalestickets { continue } - New-AutotaskTicket -atCompany $FulcraATCompany ` + New-AutotaskTicket -atCompany 0 ` -title $mTitle ` -description ($managed_issues_body|Join-String) } diff --git a/Modules/CippExtensions/IronScales/Get-IronScalesMapping.ps1 b/Modules/CippExtensions/IronScales/Get-IronScalesMapping.ps1 new file mode 100644 index 000000000000..c8b0a6ade080 --- /dev/null +++ b/Modules/CippExtensions/IronScales/Get-IronScalesMapping.ps1 @@ -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 +} \ No newline at end of file diff --git a/Modules/CippExtensions/IronScales/Set-IronScalesMapping.ps1 b/Modules/CippExtensions/IronScales/Set-IronScalesMapping.ps1 new file mode 100644 index 000000000000..41c0124ef03f --- /dev/null +++ b/Modules/CippExtensions/IronScales/Set-IronScalesMapping.ps1 @@ -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 +} \ No newline at end of file