Updated / Modified Magic Dash Customer Services Script (Primarily to work with new List Items)

I've managed to get round to updating the Magic Dash script used here - https://mspp.io/hudu-magic-dash-customer-services/ and the point of a little discussion on the forums lately. All tested and appears to be working well.

Primary changes:

1 - Update to only process "Active" companies within HUDU Instance

2 - Scan through ItemList Field Types within the Company Information field (NOTE Field)

3 - Added support for Multiple Selection lists - e.g. 2 different broadband connections specified for that customer

As below (excluding API Key / Domain information at top!):

$DetailsLayoutName = 'Company Information'
$SplitChar = ':'
import-module HuduAPI
#Login to Hudu
New-HuduAPIKey $HuduAPIKey
New-HuduBaseUrl $HuduBaseDomain
$AllowedActions = @('ENABLED', 'NOTE', 'URL')
# Get the Asset Layout
$DetailsLayout = Get-HuduAssetLayouts -name $DetailsLayoutName
$Lists = Invoke-WebRequest -Uri "$HuduBaseDomain/api/v1/lists" -Headers @{
"x-api-key" = "$HuduAPIKey"
"Content-Type" = "application/json"
"accept" = "application/json"
} -Method Get # -ErrorAction Stop
$ListsResponse = $Lists.Content | ConvertFrom-Json -Depth 16
Write-Output $ListsResponse
function Get-HuduListItem {
param ($ListID)
# Ensure the ListID is treated as an integer for comparison
$ListID = [int]$ListID
# Search through all lists and their items to find a matching ID
foreach ($List in $ListsResponse) { #was $Lists
$MatchedItem = $List.list_items | Where-Object { $_.id -eq $ListID }
if ($MatchedItem) {
# Ensure the output is a clean string
return [string]$MatchedItem.name
}
}
return "None"
}
# Check we found the layout
if (($DetailsLayout | measure-object).count -ne 1) {
Write-Error "No / multiple layout(s) found with name $DetailsLayoutName"
} else {
# Get all the detail assets and loop - NOTE - Only ACTIVE Customers
$DetailsAssets = Get-HuduAssets -assetlayoutid $DetailsLayout.id | Where-Object { $_.archived -eq $false }
foreach ($Asset in $DetailsAssets) {
# Loop through all the fields on the Asset
$Fields = foreach ($field in $Asset.fields) {
# Split the field name
$SplitField = $Field.label -split $SplitChar
# Check the field has an allowed action.
if ($SplitField[1] -notin $AllowedActions) {
Write-Error "Field $($Field.label) is not an allowed action"
} else {
# Format an object to work with
[PSCustomObject]@{
ServiceName = $SplitField[0]
ServiceAction = $SplitField[1]
Value = $field.value
}
}
}
Write-Output "*** Starting processing for: $($Asset.company_name) ***"
Foreach ($Service in $fields.servicename | select-object -unique){
$EnabledField = $fields | Where-Object {$_.servicename -eq $Service -and $_.serviceaction -eq 'ENABLED'}
$NoteField = $fields | Where-Object {$_.servicename -eq $Service -and $_.serviceaction -eq 'NOTE'}
$URLField = $fields | Where-Object {$_.servicename -eq $Service -and $_.serviceaction -eq 'URL'}
if ($EnabledField){
$Colour = Switch ($EnabledField.value) {
$True {'success'}
$False {'grey'}
default {'grey'}
}
# Create/Update DASH Title - based on Name and Service
$DashTitle = "$($Asset.company_name) - $Service"

$Param = @{
Title = $DashTitle
CompanyName = $Asset.company_name
Shade = $Colour
}
# Process NOTE Field - Identify and handle multiple List IDs
if ($NoteField.value -match '"list_ids":\[(.*?)\]') {
$listIds = $Matches[1] -split ',' | ForEach-Object { [int]$_.Trim() }
} else {
$listIds = @()
}
# Process NOTE Field - Match each ListID to its text value
if ($NoteField.Value -and $listIds.Count -gt 0) {
$ListReadableValues = $listIds | ForEach-Object { Get-HuduListItem -ListID $_ }
$ListReadableValue = $ListReadableValues -join ", "
$Param['Message'] = "Note: $ListReadableValue"
Write-Output "Readable Value(s): $ListReadableValue"
$NoteField.value = [String]$ListReadableValue
} else {
# Handle cases with no valid ListID
$ListReadableValue = "None"
$Param['Message'] = $ListReadableValue
}
# Create/Update DASH Note Field
if ($NoteField.value){
$Param['Message'] = $NoteField.value
$Param | Add-Member -MemberType NoteProperty -Name 'Message' -Value $NoteField.value
} else {
$Param['Message'] =Switch ($EnabledField.value) {
$True {"Customer has $Service"}
$False {"No $Service"}
default {"No $Service"}
}
}
# Create/Update DASH URL Field
if ($URLField.value){
$Param['ContentLink'] = $URLField.value
}
$null = Set-HuduMagicDash @Param
} else {
Write-Error "No Enabled Field was found"
}
}
Write-Output "*** Finished processing for: $($Asset.company_name) ***"
}
}

7
2 replies