Module/Administration/Export-BCSInstalledAppsPerTenant.ps1

<#
.SYNOPSIS
 
.DESCRIPTION
 
.EXAMPLE
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>

function Export-BCSInstalledAppsPerTenant {
    Param (
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
        [string]$OutputFilename,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
        [string]$OutputDirectory,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
        [string]$ServerInstance,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [ValidateSet('140', '150', '160', '170', '180')]
        [string]$BCVersion,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
        [ValidateSet('Console', 'File')]
        [string]$OutputTo = 'Console',
        [Parameter(Mandatory = $False)]
        [switch]$Append
    )
    begin {}

    process {

        $OutFileCsv = ("{0}\{1}" -f $OutputDirectory, $OutputFilename);

        if ($a) {
            $a.Clear()
        }

        if ($Append -eq $False) {
            if (Test-Path $OutFileCsv) {
                Remove-Item $OutFileCsv;
            }
        }

        $nstRegistryPath = ("HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\{0}\Service" -f $BCVersion)
        $nstPath = (Get-ItemProperty -Path $nstRegistryPath).Path

        Import-Module (Join-Path $nstPath.Parent.FullName 'Microsoft.Dynamics.Nav.Management.dll') -NoClobber
        Import-Module (Join-Path $nstPath.Parent.FullName 'Microsoft.Dynamics.Nav.Apps.Management.dll') -NoClobber

        $ServerInstanceVersion = Get-NAVServerInstance -ServerInstance $ServerInstance | Select-Object -Property Version;
        $ServerInstanceVersion = $ServerInstanceVersion.Version.Split('.')[0];

        $tenants = Get-NAVAppTenant -ServerInstance $ServerInstance | Select-Object -Property Id;

        $a = @();

        foreach ($tenant in $tenants) {

            $apps = Get-NAVAppInfo -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 '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

                $a += $item
            }
        }

        $output = $a | Sort-Object Tenant, Name

        if ($OutputTo -eq 'Console') {
            $output
        }
        elseif ($OutputTo -eq 'File') {
            if ($Append -eq $False) {
                $output | Export-Csv -Path $OutFileCsv -Encoding utf8 -Delimiter ';' -NoTypeInformation   
            }
            else {
                $output | Export-Csv -Path $OutFileCsv -Encoding utf8 -Delimiter ';' -Append -NoTypeInformation
            }
        }
    }

    end {
    }
}

Export-ModuleMember -Function Export-BCSInstalledAppsPerTenant