Celerium.psm1

#Region './Private/Dependencies/Get-CeleriumDependency.ps1' -1

function Get-CeleriumDependency {
<#
    .SYNOPSIS
        Gets the PowerShell wrapper modules that Celerium depends on

    .DESCRIPTION
        The Get-CeleriumDependency cmdlet reads the RequiredModules of the imported
        Celerium module manifest and returns the minimum required version
        together with the version that is currently loaded for each module

        The API name is the part of the module name that follows Celerium.,
        such as ITGlue for Celerium.ITGlue

    .PARAMETER ModuleName
        The name of the imported module to read the RequiredModules from

        Default value: Celerium

    .EXAMPLE
        Get-CeleriumDependency

        Returns every module that is listed in the RequiredModules of the manifest,
        such as Celerium.Auvik, Celerium.DattoRMM, & Celerium.ITGlue

    .EXAMPLE
        Get-CeleriumDependency | Where-Object { -not $_.IsLoaded }

        Returns the required modules that are not loaded

    .NOTES
        N/A

    .LINK
        https://celerium.github.io/Celerium-PowerShell/site/Dependencies/Get-CeleriumDependency.html
#>


    [CmdletBinding()]
    [Alias('Get-clDependency')]
    param (
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ModuleName = 'Celerium'
    )

    begin {}

    process {

        try {

            $Module = Get-Module -Name $ModuleName | Select-Object -First 1
            if (-not $Module) { throw "The [ $ModuleName ] module is not imported" }

            $ManifestPath   = Join-Path -Path $Module.ModuleBase -ChildPath "$($Module.Name).psd1"
            $Manifest       = Import-PowerShellDataFile -Path $ManifestPath

            foreach ($Required in @($Manifest.RequiredModules)) {

                # A required module is either a module name or a module specification
                $RequiredName       = if ($Required -is [hashtable]) { $Required.ModuleName } else { [string]$Required }
                $RequiredVersion    = if ($Required -is [hashtable]) { $Required.ModuleVersion } else { $null }
                $RequiredGuid       = if ($Required -is [hashtable]) { $Required.Guid } else { $null }

                $Loaded = Get-Module -Name $RequiredName | Sort-Object -Property Version -Descending | Select-Object -First 1

                [PSCustomObject]@{
                    Name                    = $RequiredName
                    Api                     = $RequiredName -replace '^Celerium\.', ''
                    RequiredVersion         = $RequiredVersion
                    Guid                    = $RequiredGuid
                    Version                 = if ($Loaded) { $Loaded.Version } else { $null }
                    IsLoaded                = [bool]$Loaded
                    ExportedFunctionCount   = if ($Loaded) { $Loaded.ExportedFunctions.Count } else { 0 }
                }

            }

        }
        catch {
            Write-Error $_
        }

    }

    end {}

}
#EndRegion './Private/Dependencies/Get-CeleriumDependency.ps1' 88
#Region './Public/Commands/Get-CeleriumCommand.ps1' -1

function Get-CeleriumCommand {
<#
    .SYNOPSIS
        Gets the commands of the Celerium PowerShell wrapper modules

    .DESCRIPTION
        The Get-CeleriumCommand cmdlet lists the functions that are exposed by the
        modules listed in the RequiredModules of the Celerium manifest
        (Celerium.AutoTask, Celerium.Auvik, Celerium.DattoBCDR, Celerium.DattoEDR,
        Celerium.DattoRMM, Celerium.GitHub, Celerium.Inky, Celerium.ITGlue, Celerium.KQM,
        Celerium.MyITProcess, Celerium.RocketCyber, Celerium.SentinelOne, & Celerium.vPenTest) which are imported with Celerium

        The name of a function is built from the HTTP method of the API call
            GET -> Get-*
            POST -> New-*
            PUT -> Set-*
            PATCH -> Set-*
            DELETE -> Remove-*

    .PARAMETER Api
        Defines which API wrapper module to return commands from

        Default value: All

        Allowed values:
        'All' or the name of a wrapper without Celerium., such as 'AutoTask', 'Auvik', 'DattoBCDR',
        'DattoEDR', 'DattoRMM', 'GitHub', 'Inky', 'ITGlue', 'KQM', 'MyITProcess',
        'RocketCyber', 'SentinelOne', or 'vPenTest'

    .PARAMETER Name
        Defines one or more command names to return, wildcards are supported

        Default value: *

    .PARAMETER Verb
        Defines one or more verbs to return commands for, such as Get or New

    .EXAMPLE
        Get-CeleriumCommand

        Returns every command of every wrapper module

    .EXAMPLE
        Get-CeleriumCommand -Api ITGlue -Name *Organization*

        Returns every ITGlue command that has Organization in its name

    .EXAMPLE
        Get-CeleriumCommand -Verb New, Set, Remove

        Returns the commands that write data

    .NOTES
        N/A

    .LINK
        https://celerium.github.io/Celerium-PowerShell/site/Commands/Get-CeleriumCommand.html
#>


    [CmdletBinding()]
    [Alias('Get-clCommand')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Justification = 'The argument completer receives parameters that are not needed')]
    param (
        [Parameter(Mandatory = $false)]
        [ArgumentCompleter({
            param ($CommandName, $ParameterName, $WordToComplete)
            @('All') + @(Get-CeleriumDependency | ForEach-Object { $_.Api }) | Where-Object { $_ -like "$WordToComplete*" }
        })]
        [ValidateScript({
            if ($_ -eq 'All' -or $_ -in @(Get-CeleriumDependency | ForEach-Object { $_.Api })) { return $true }
            throw "[ $_ ] is not a wrapper module of Celerium, use [ All ] or the name of a wrapper without Celerium."
        })]
        [string]$Api = 'All',

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Name = '*',

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Verb
    )

    begin {}

    process {

        try {

            $Dependencies = @(Get-CeleriumDependency -ErrorAction Stop | Where-Object { $Api -eq 'All' -or $_.Api -eq $Api })

            foreach ($Dependency in $Dependencies) {

                $Module = Get-Module -Name $Dependency.Name | Sort-Object -Property Version -Descending | Select-Object -First 1
                if (-not $Module) {
                    Write-Warning "The [ $($Dependency.Name) ] module is not loaded"
                    continue
                }

                foreach ($Function in ($Module.ExportedFunctions.Values | Sort-Object -Property Name)) {

                    $FunctionVerb, $FunctionNoun = $Function.Name -split '-', 2

                    if (-not ($Name | Where-Object { $Function.Name -like $_ })) { continue }
                    if ($Verb -and $FunctionVerb -notin $Verb) { continue }

                    $Alias = @($Module.ExportedAliases.Values | Where-Object { $_.ResolvedCommandName -eq $Function.Name } | ForEach-Object { $_.Name })

                    [PSCustomObject]@{
                        Name            = $Function.Name
                        Verb            = $FunctionVerb
                        Noun            = $FunctionNoun
                        Api             = $Dependency.Api
                        Alias           = $Alias
                        ModuleName      = $Module.Name
                        ModuleVersion   = $Module.Version
                    }

                }

            }

        }
        catch {
            Write-Error $_
        }

    }

    end {}

}
#EndRegion './Public/Commands/Get-CeleriumCommand.ps1' 133