Public/Install-P1Demo.ps1

function Install-P1Demo {
    <#
    .Synopsis
    Install PlannerOne for an existing instance of Dynamics NAV.
 
    .Description
    Detect NAV version and instances, and configure a Plannerone tenant named Demo.
    #>

    [cmdletbinding()]
    param( 
    )    
    Process {
        Initialize-NAVVersion

        $P1Tenant = "Demo"
        $NAVInstances = Get-NAVServerInstance

        if ($NAVInstances.GetType().IsArray) {
            Write-Warning "Multi-instance not supported yet in this DEMO scenario!"
            Write-Warning "Run command Get-P1SampleScript and customize the script with your values."
            return
        } else {
            $NAVInstance = $NAVInstances
        } 

        $xmlConfig = $NAVInstance | Get-NAVServerConfiguration -AsXml
        $values = $xmlConfig.configuration.appSettings.add

        $NAVServerInstance = ($values | Where-Object key -eq 'ServerInstance').value
        $NAVDatabaseServer = ($values | Where-Object key -eq 'DatabaseServer').value
        $NAVDatabaseInstance = ($values | Where-Object key -eq 'DatabaseInstance').value
        $NAVDatabaseName = ($values | Where-Object key -eq 'DatabaseName').value

        if ($NAVDatabaseInstance) {
            $NAVDatabaseServer += "\$NAVDatabaseInstance"
        }

        Write-Output "Parameters detected from you Dynamics NAV Instance:"
        Write-Output "> NAVServerInstance: $NAVServerInstance"
        Write-Output "> NAVDatabaseServer: $NAVDatabaseServer"
        Write-Output "> NAVDatabaseName: $NAVDatabaseName"

        # Prepare NAV objects, Add-ins, Setup
        $workspace = "."
        $LicenceDevelopment = Read-Host "Enter path to your DEV licence"
        $LicenceUser = Read-Host "Enter path to your Customer licence"

        new-item $workspace -type "Directory" -force

        #Set the log path path to log all actions in verbose mode.
        Set-P1Logpath "$workspace\SampleScript-log.txt"
        #Set the verbose level, Silent, Warning, verbose , in this example the level is set at Silent
        Set-P1VerboseOutPut 2
        #Set the licence in Dynamics NAV development the "Development licence" from $LicenceDevelopment path
        Set-P1NavLicence -path $LicenceDevelopment -serverInstance $NAVInstance 

        #Get the files with PlannerOne Dynamics NAV objects in your Work space folder
        Get-P1NavFobFiles -path $workspace
        #Get, set and install the PlannerOne Addins to instance $NAVInstance
        Get-P1NavAddins -ServerInstance $NAVInstance  

        #Import in Dynamics NAV the PlannerOne Specific NAV Objects
        Import-NAVApplicationObject "$Workspace\PLA-specific.fob" -DatabaseName "$NAVDatabaseName" -DatabaseServer $NAVDatabaseServer -ImportAction OverWrite 
        #Get the Dyanmics NAV objects modified by plannerone in Database and Server NAV set in parameter
        Start-P1MergeProcess  -DatabaseName "$NAVDatabaseName" -DatabaseServer $NAVDatabaseServer -Force
        #Set the User licence in NAV development environnement
        Set-P1NavLicence -path $LicenceUser  -serverInstance $NAVInstance         


        # Technical account (must exist in $NAVInstance database)
        # Replace this value if the current user is not the technical account
        $UserDomain = $env:USERDOMAIN
        $UserLogin = $env:USERNAME
        $UserPassword = Read-Host -AsSecureString "Please enter your password"        

        $features = Test-P1Prerequisites

        $missingFeatures = ($features | Where-Object InstallState -ne Installed)

        if ($missingFeatures.Count -eq 0) {
            $ERP = "NAV"
            $Version = $NavVersion
            # Install PlannerOne packages
            Install-P1Package -ERP $ERP -Version $Version

            # Create new tenant
            New-P1Tenant $P1Tenant -ERP $ERP

            # Configure adapter of tenant
            Set-P1Adapter -Tenant $P1Tenant -InstanceName $NAVInstance -ServiceDomain $UserDomain -ServiceLogin $UserLogin -ServicePassword $UserPassword -AutoPort

            # Get environments of tenant and save them in a variable
            $envList += Get-P1Environments -Tenant $P1Tenant -Product "Production" 
            $envList += Get-P1Environments -Tenant $P1Tenant -Product "Project" 

            # Initialize all environments in list
            Initialize-P1Environments -Tenant $P1Tenant -Environments $envList
            
        } else {
            $missingFeatures
        }
    }
}