Module/Administration/Get-BCSAppStatusForServerInstance.ps1

<#
.SYNOPSIS
    Get a list of apps installed and published on a Business Central Instance
 
.DESCRIPTION
    Get a list of apps installed and published on a Business Central Instance
 
.PARAMETER serverInstance
    Specifies the Business Central Server Instance to collect apps from.
 
.PARAMETER appName
    If specified the output will be for only apps with this name.
 
.PARAMETER businessCentralServer
    If specified the function will connect to the server and get the results remotely.
 
.PARAMETER userName
    User name to use when logging into the Business Central Server
 
.PARAMETER password
    Password to use when logging into the Business Central Server
 
.EXAMPLE
    Get-BCSAppStatusForServerInstance -serverInstance myServerInstance -appName myAppName
 
    Get-BCSAppStatusForServerInstance -serverInstance myServerInstance -appName myAppName -businessCentralServer myBusinessCentralServer
 
    Get-BCSAppStatusForServerInstance -serverInstance myServerInstance -appName myAppName -businessCentralServer myBusinessCentralServer | Format-Table
 
    Get-BCSAppStatusForServerInstance -serverInstance BCQA -appName "BRC Core" -businessCentralServer "sebcasqa01.brightcom.online" | Where-Object IsInstalled -EQ True | Format-Table
     
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>

function Get-BCSAppStatusForServerInstance {
    Param (
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
        [string]$serverInstance,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [string]$appName,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [string]$businessCentralServer,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [string]$userName,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [SecureString]$password
    )
    begin {
    }

    process {
        if (-not [string]::IsNullOrEmpty($businessCentralServer)) {
            if ([string]::IsNullOrEmpty($userName)) {
                $moduleProfile = Get-BCSModuleProfile;            
                $userName = $moduleProfile.AzureUserName
                $password = $moduleProfile.AzurePassword
            }

            $ScriptBlock = { 
                Param($serverInstance, 
                    $appName,
                    $importDynamicsNavModules)
                  
                $ErrorActionPreference = "Stop"

                Get-BCSAppStatusForServerInstance -serverInstance $serverInstance -appName $appName
            }

            $credential = New-Object System.Management.Automation.PSCredential -argumentList $userName, $password
    
            $session = New-PSSession -UseSSL -ComputerName $businessCentralServer -Credential $credential -ErrorAction SilentlyContinue
            $remoteResponse = Invoke-Command -Session $session -ScriptBlock $ScriptBlock -ArgumentList ($ServerInstance, $appName)    

            $remoteResponse            
        }
        else {
            getAppsFromServer -serverInstance $serverInstance -appName $appName
        }
    }

    end {
    }
}

function getAppsFromServer {
    Param (
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
        [string]$serverInstance,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [string]$appName
    )
    begin {}

    process {
        $params = @{}

        if (-not ([string]::IsNullOrEmpty($appName))) {
            $params.Add("appName", $appName)
        }

        Import-BCSDynamicsNavModules -ServerInstance $serverInstance

        $tenants = Get-NAVAppTenant -ServerInstance $serverInstance | Select-Object -Property Id;
    
        $appsHash = [PSCustomObject]@();
    
        foreach ($tenant in $tenants) {
            $apps = Get-NAVAppInfo @params -ServerInstance $serverInstance -Tenant $tenant.Id -TenantSpecificProperties
    
            foreach ($app in $apps) {
                $NameVersion = ("{0}{1}" -f $app.Name, $app.Version);
    
                $item = New-Object PSObject
                $item | Add-Member -type NoteProperty -Name 'ServerInstance' -Value $serverInstance;
                $item | Add-Member -type NoteProperty -Name 'Tenant' -Value $tenant.Id
                $item | Add-Member -type NoteProperty -Name 'Name' -Value $app.Name
                $item | Add-Member -type NoteProperty -Name 'Publisher' -Value $app.Publisher
                $item | Add-Member -type NoteProperty -Name 'Version' -Value $app.Version
                $item | Add-Member -type NoteProperty -Name 'Scope' -Value $app.Scope
                $item | Add-Member -type NoteProperty -Name 'IsInstalled' -Value $app.IsInstalled
                $item | Add-Member -type NoteProperty -Name 'IsPublished' -Value $app.IsPublished
                $item | Add-Member -type NoteProperty -Name 'SyncState' -Value $app.SyncState
                $item | Add-Member -type NoteProperty -Name 'NameVersion' -Value $NameVersion
                $item | Add-Member -type NoteProperty -Name 'AppID' -Value $app.AppId
    
                $appsHash += $item;
            }
        }
    
        $appsHash
    }
    end {
    }
}

Export-ModuleMember -Function Get-BCSAppStatusForServerInstance