Recursive New-UDEndpointInitialization Solution?

Hey all! Working on a project with lots of functions and a fair few pages. I’m struggling to get my functions imported in a nice way.

Right now, when the dashboard launches this block of code happens:

$Location = (Split-Path -Path $MyInvocation.MyCommand.Path)
$LoadDir = $Location + "\LoadFirst"
$FunctionDir = $Location + "\functions"
$PageDir = $Location + "\pages"
...
Get-ChildItem -Path $LoadDir -Filter *.ps1 -Recurse | ForEach-Object {
    . $_.FullName
}

#Source Functions
Get-ChildItem -Path $FunctionDir -Filter *.ps1 -Recurse | ForEach-Object {
    . $_.FullName
}

#Source Pages
Get-ChildItem -Path $PageDir -Filter *.ps1 -Recurse | ForEach-Object {
    . $_.FullName
}

This goes to the load first folder which loads some important things, then loads functions and then loads some page variables.

The pages contain things like the AzureAD Login page and page variable - this can be referenced when starting the dashboard. The home page also loads, but I’m running in to an issue where the functions do not work in the pages.

For instance, one of the pages has the following:

        New-UDColumn -size 4 -Endpoint {
            $CountOfNewDetections = Get-CSDetectionCount
            New-UDcard -Title "CS Detections" -Text "$CountOfNewDetections"
        }

The Get-CSDetectionCount is defined in a function file which is loaded to the terminal or IDE when I launch the dashboard. That file just contains the following:

function Get-CSDetectionCount {
    $idHeader = @{
        Authorization = "$CSAccessToken"
        "Content-Type" = "application/json"
    }
    $NewDetections = Invoke-RestMethod -URI "https://api.crowdstrike.com/detects/queries/detects/v1?filter=status:'new'" -Method Get -Headers $idHeader
    $CountOfNewDetections = $NewDetections.resources.count
    Return $CountOfNewDetections
}

I believe the solution is to have a New-UDEndpointInitialization with an array of the functions. However, I’m not sure if there’s a good way or intended way to support building that array as the functions will come across from many files?

Worst case I could do something like $FunctionNames = @() in the initial variable definitions and then at the end of each function file have a line like $FunctionNames += 'New-FunctionName' which would catch them but doesn’t seem too elegant.

Curious if anyone has good solutions to this?

You could do something like:

$Functions  = (Get-ChildItem Function:\).Name
$EndpointInit = New-UDEndpointInitialization -Function $Functions

It’s what 2.6.1 will do automatically:

2 Likes

Thank you! I was able to run with that that and solve two problems I was bumping up against. Appreciate the insight!!!