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.
 
.EXAMPLE
 
.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
            }

            $securePassword = ConvertTo-SecureString -String $moduleProfile.AzurePassword
            $credential = New-Object System.Management.Automation.PSCredential -argumentList $moduleProfile.AzureUserName, $securePassword
        
            $vmSession = New-DeploymentRemoteSession -HostName $businessCentralServer -Credential $credential
            
            $sessionArgument = @{ "Session" = $vmSession }
            
            Invoke-Command @sessionArgument -ScriptBlock { 
                Param($serverInstance, 
                      $appName, 
                      [System.Management.Automation.PSCredential]$credential)
                  
                $ErrorActionPreference = "Stop"
                  
                $global:progresspreference = $false
                Install-Module BCSPowershellModule -Force

                Get-BCSAppStatusForServerInstance -serverInstance $serverInstance -appName $appName

            } -ArgumentList $serverInstance, $appName, $credential        
        }
        else {
            getAppsFromServer -serverInstance $serverInstance -appName $appName
        }
    }

    end {
    }
}

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

    process {
        Import-BCSDynamicsNavModules -ServerInstance $serverInstance

        $tenants = Get-NAVAppTenant -ServerInstance $serverInstance | Select-Object -Property Id;
    
        $appsHash = [PSCustomObject]@();
    
        foreach ($tenant in $tenants) {
            if ([string]::IsNullOrEmpty($appName)) {
                $apps = Get-NAVAppInfo -ServerInstance $serverInstance -Tenant $tenant.Id -TenantSpecificProperties
            }
            else {
                $apps = Get-NAVAppInfo -ServerInstance $serverInstance -Tenant $tenant.Id -TenantSpecificProperties | Where-Object Name -eq $appName
            }
    
            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 'BCVersion' -Value $serverInstanceVersion;
                $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