Manage SquaredUp Dashboards with PowerShell

For those looking to leverage the API on the Enterprise tier, the below functions might help to kickstart your scripting.

Please share any suggestions or tweaks to make the functions or scripts better, if we get enough collected we can start to build out a module.

function Get-SqDashboard {
    param (
        [Parameter(Mandatory = $true)]
        [string]$apiKey,
        [Parameter(Mandatory = $false)]
        [string]$workspaceId = "",
        [Parameter(Mandatory = $false)]
        [string]$dashboardId = "",
        [Parameter(Mandatory = $false)]
        [string]$region = 'us'
    )
    
    $apiUrl = $region -eq 'eu' ? 'https://eu.api.squaredup.com' : 'https://api.squaredup.com'

    # If a workspace ID is provided, get all dashboards for that workspace
    if ($workspaceId -ne "") {
        try {
            $workspaceResult = Invoke-RestMethod -Uri "$apiUrl/api/workspaces/$workspaceId/dashboards" -Method Get -Headers @{ 'apiKey' = $apiKey } -ContentType 'application/json'
            return $workspaceResult
        }
        catch {
            Write-Error "Failed to get dashboards for workspace with id $workspaceId"
            Write-Error $_
        }
    }

    try {
        $dashboardResult = Invoke-RestMethod -Uri "$apiUrl/api/dashboards/$dashboardId" -Method Get -Headers @{ 'apiKey' = $apiKey } -ContentType 'application/json'
        return $dashboardResult
    }
    catch {
        Write-Error "Failed to get dashboard with id $dashboardId"
        Write-Error $_
    }
}

function New-SqDashboard {
    param (
        [Parameter(Mandatory = $true)]
        [string]$apiKey,
        [Parameter(Mandatory = $true)]
        [string]$workspaceId,
        [Parameter(Mandatory = $true)]
        [string]$name,
        [Parameter(Mandatory = $true)]
        [string]$content,
        [Parameter(Mandatory = $false)]
        [string]$region = 'us',
        [Parameter(Mandatory = $false)]
        [string]$timeframe = $null
    )
    
    $apiUrl = $region -eq 'eu' ? 'https://eu.api.squaredup.com' : 'https://api.squaredup.com'

    # Validate that the passed content is a valid JSON string
    try {
        $contentObject = $content | ConvertFrom-Json -Depth 14
    }
    catch {
        Write-Error "The content parameter must be a valid JSON string"
        return
    }

    $body = @{
        workspaceId = $workspaceId
        displayName = $name
        content     = $contentObject
        timeframe   = $timeframe
    }

    try {
        # Creates the dashboard and returns the Id
        $dashboardResult = Invoke-RestMethod -Uri "$apiUrl/api/dashboards" -Method Post -Headers @{ 'apiKey' = $apiKey } -ContentType 'application/json' -Body ($body | ConvertTo-Json -Depth 14 -Compress)
        return $dashboardResult
    }
    catch {
        Write-Error "Failed to create dashboard"
        Write-Error $_
    }
}

function Remove-SqDashboard {
    param (
        [Parameter(Mandatory = $true)]
        [string]$apiKey,
        [Parameter(Mandatory = $true)]
        [string]$dashboardId,
        [Parameter(Mandatory = $false)]
        [string]$region = 'us'
    )
    
    $apiUrl = $region -eq 'eu' ? 'https://eu.api.squaredup.com' : 'https://api.squaredup.com'

    try {
        $dashboardResult = Invoke-RestMethod -Uri "$apiUrl/api/dashboards/$dashboardId" -Method Delete -Headers @{ 'apiKey' = $apiKey } -ContentType 'application/json'
        return $dashboardResult
    }
    catch {
        Write-Error "Failed to delete dashboard with id $dashboardId"
        Write-Error $_
    }
}
1 Like