CheckInvoicing/Get-HeimdalInvoicing.ps1

Write-Host "Checking Heimdal API for customer licenses"
$headers = @{
    Authorization = "Bearer YPMTX4WVCTERSJQHJD6GHAAFGOWXGEA2"
}
$datenow = Get-Date

$datebefore = $datenow.AddDays(-30)

$enddate = $(Get-Date $datenow -Format o).Substring(0, 16)

$startdate = $(Get-Date $datebefore -Format o).Substring(0, 16)

$uri = "https://dashboard.heimdalsecurity.com/api/heimdalapi/customers?customerId=121016&startDate=$startdate&endDate=$enddate"

$startTime = Get-Date

$r = Invoke-RestMethod -Uri $uri -Headers $headers

$array = @()
foreach ($cust in $r) {
    $customerName = $cust.name
    $klNumber = ($customerName -split ' ')[0]

    $licenses = $cust.licenseType -split ','
    $activeEndpoints = $cust.activeEndpoints
    if($activeEndpoints -eq 0) { continue }

    $customerInfo = [pscustomobject]@{
        KL                         = $klNumber
        Customer                   = $customerName
        'Standard Sikkerhedspakke' = 0
        'Basis Sikkerhedspakke'    = 0
        'ThirdParty Applications'  = 0
    }


    if ('AV' -in $licenses -and 
        'REP' -in $licenses -and
        'DNS-E' -in $licenses) {
        $customerInfo.'Standard Sikkerhedspakke' = $activeEndpoints
    }
    elseif ('AV' -in $licenses -and $licenses.count -eq 1) {
        $customerInfo.'Basis Sikkerhedspakke' = $activeEndpoints
    }

    elseif ('VM' -in $licenses -and $licenses.count -eq 1) {
        $customerInfo.'ThirdParty Applications' = $activeEndpoints
    }

    else {
        "Could not place customer i catecory: $customerName"
        $licenses -join ','
        "Active endpoints: $activeEndpoints"
    }

    $array += $customerInfo
}

$endTime = Get-Date

$elapsedTime = $endTime - $startTime
Write-Host "Elapsed time: $elapsedTime"
Write-Host "Done - Now comparing..."


$abo = import-csv -Path '.\CheckInvoicing\ABO.csv' -Delimiter ';'
$heimdal = $array
$errors = 0
$allErrors = @()
$clients = 0
foreach ($HeimdalCustomer in $heimdal) {
    $KL = $HeimdalCustomer.KL
    if ($KL -eq '000000') { continue }


    $klInfo = $abo | Where-Object Kundenr. -eq $KL
    if (-not $klInfo) {
        $allErrors += [pscustomobject]@{
            KL        = $KL
            KUNDENAVN = $HeimdalCustomer.Customer
            ABO       = "Ingen abonnement"
            ABO_ANTAL = 0
            HEIMDAL   = $HeimdalCustomer.'Basis Sikkerhedspakke'+$HeimdalCustomer.'Standard Sikkerhedspakke'+$HeimdalCustomer.'ThirdParty Applications'
        }
        $errors++
        continue
    }

    foreach ($klAbo in $klInfo) {
        $expired = $false
        if(($klAbo.'Opsagt pr. dato').Length -gt 2) {
            if(($klAbo.'N�ste hovedfaktureringsdato').Length -gt 2) {
                $linieUdloeb = [DateTime]::ParseExact($klAbo.'N�ste hovedfaktureringsdato', "dd-MM-yyyy", $null)
            }
            $cancelledDate = [DateTime]::ParseExact($klAbo.'Opsagt pr. dato', "dd-MM-yyyy", $null)
            $latestDate = $cancelledDate
            if($linieUdloeb -gt $cancelledDate) {
                $latestDate = $linieUdloeb
            }

            $expired = (Get-Date) -gt $latestDate
        }

        if (-not $expired) {
            $numOfClients = [int]$klAbo.'Antal ialt'
            if ($klAbo.'Faktureret antal' -gt $klAbo.'Antal ialt') {
                $numOfClients = [int]$klAbo.'Faktureret antal'
            }
        }
        $err = [PSCustomObject]@{
            KL        = $KL
            KUNDENAVN = $HeimdalCustomer.Customer
            ABO       = $null
            KL_ANTAL = $numOfClients
            API_ANTAL   = $null
        }
        switch ($klAbo.'Varenr.') {
            { $_ -eq 160097 -or $_ -eq 160098 } {
                if ([int]$HeimdalCustomer.'Standard Sikkerhedspakke' -gt $numOfClients) {
                    $errors++

                    $clients += $HeimdalCustomer.'Standard Sikkerhedspakke' - $numOfClients

                    $err.ABO = 'Standard Sikkerhedspakke'

                    $err.API_ANTAL = $($HeimdalCustomer.'Standard Sikkerhedspakke')
                    $allErrors += $err
                }
            }
    
            { $_ -eq 160099 -or $_ -eq 160100 } {
                if ([int]$HeimdalCustomer.'Basis Sikkerhedspakke' -gt $numOfClients) {
                    $errors++

                    $clients += $HeimdalCustomer.'Basis Sikkerhedspakke' - $numOfClients

                    $err.ABO = 'Basis Sikkerhedspakke'

                    $err.API_ANTAL = $($HeimdalCustomer.'Basis Sikkerhedspakke')
                    $allErrors += $err
                }
            }
    
            { $_ -eq 170134 -or $_ -eq 170135 } {
                if ([int]$HeimdalCustomer.'ThirdParty Applications' -gt $numOfClients) {
                    $errors++

                    $clients += $HeimdalCustomer.'ThirdParty Applications' - $numOfClients

                    $err.ABO = 'ThirdParty Applications'

                    $err.API_ANTAL = $($HeimdalCustomer.'ThirdParty Applications')
                    $allErrors += $err
                }
            }


        }

    }
}

"Errors: $errors"
"Clients: $clients"

$allErrors | Export-Excel -Path "CheckInvoicing\HeimdalErrors.xlsx" -AutoSize -BoldTopRow -TableName "Errors" -WorksheetName "Errors" -ClearSheet