Magic Dash: Asset Health

Magic Dash widget to display the percentage of active assets (those updated within the specified time frame) out of the total number of assets per company.

Features

  • Displays the asset health for a company (0% = all inactive; 100% = all active)

  • Provides a list view of all inactive assets

  • Provides summary of script results and timing.

# Asset_health.ps1
# Generates a Magic Dash widget displaying the percentage of active assets (those updated within the specified time frame) out of the total number of assets per company.
# Usage: ./asset_health.ps1

# -------------------------------------------------------------------------
# User Environment
# -------------------------------------------------------------------------

# Before starting, you'll need to set 3 variables, just below their explanations.
# 1. Set your Azure Key Vault name
# 2. Set the name of your secret (which holds Hudu API key)
# 3. Set the URL of your Hudu instance-

$AzVault_Name = "ENTER YOUR KEY VAULT NAME HERE"
$AzVault_HuduSecretName = "ENTER YOUR KEY VAULT SECRET NAME HERE"
$HuduBaseURL = "HTTPS://YOUR.HUDU.DOMAIN"

# -------------------------------------------------------------------------
# Init Modules and Sign-In
# -------------------------------------------------------------------------
foreach ($module in @('Az.KeyVault', 'HuduAPI')) {
  if (Get-Module -ListAvailable -Name $module) { 
      Write-Host "Importing module, $module..."; 
      Import-Module $module 
  } else {
      Write-Host "Installing and importing module $module..."; 
      Install-Module $module -Force -AllowClobber; 
      Import-Module $module 
  }
}

if (-not (Get-AzContext)) { 
Connect-AzAccount };

New-HuduAPIKey "$(Get-AzKeyVaultSecret -VaultName "$AzVault_Name" -Name "$AzVault_HuduSecretName" -AsPlainText)"
New-HuduBaseUrl $HuduBaseURL

# -------------------------------------------------------------------------
# Begin Script Logic
# -------------------------------------------------------------------------

# Fetch all companies
$AllCompanies = Get-HuduCompanies

# Process each company
foreach ($Company in $AllCompanies) {
    $CompanyName = $Company.name
    $CompanyId = $Company.id
    Write-Host "Processing assets for $CompanyName (ID: $CompanyId)..." -ForegroundColor Cyan

    # Fetch assets
    $AllAssets = Get-HuduAssets | Where-Object { $_.company_id -eq $CompanyId }
    $InactiveAssets = $AllAssets | Where-Object { $_.updated_at -lt (Get-Date).AddMonths(-6) }
    $TotalAssets = $AllAssets.Count
    $InactiveCount = $InactiveAssets.Count
    $HealthPercentage = if ($TotalAssets -gt 0) { [math]::Round(((1 - ($InactiveCount / $TotalAssets)) * 100), 2) } else { 100 }

    # Generate widget content
    $Message = "$HealthPercentage%"
    $Content = "<h3>Inactive Assets</h3><table><tr><th>Name</th><th>Last Updated</th><th>Link</th></tr>"
    foreach ($Asset in $InactiveAssets | Sort-Object -Property updated_at) {
        $AssetUrl = "https://$HuduBaseURL/a/$($Asset.slug)"
        $Content += "<tr><td>$($Asset.name)</td><td>$($Asset.updated_at)</td><td><a href='$AssetUrl' target='_blank'>View</a></td></tr>"
    }
    $Content += "</table>"
    $Content += "<p>Total Assets: $TotalAssets</p>"
    $Content += "<p>Inactive Assets: $InactiveCount</p>"
    $Content += "<p><em>Script executed on $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss').</em></p>"
    $Content = $Content -join "`n"

    $WidgetColor = if ($HealthPercentage -ge 50) { "success" } else { "danger" }

    Write-Host "Updating Asset Health Magic Dash widget for $CompanyName (ID: $CompanyId)..." -ForegroundColor Cyan
    Set-HuduMagicDash -Title "Asset Health" `
                      -CompanyName $CompanyName `
                      -Message $Message `
                      -Content $Content `
                      -Shade $WidgetColor
}

Write-Host "Asset Health widgets updated for all companies!" -ForegroundColor Green
7
2 replies