Remotely Compiling Objects - Powershell

nick_robbonick_robbo Member Posts: 49
Hi, I am trying to write a Powershell script which will compile a selection of objects in our Development DB, before exporting them.
I am trying to make this part of a bigger script which we can then run for deployments.

Currently I get an error when using Invoke-Command to remotely compile the objects.
The error relates to not being able to Login to the SQL server, even if I use Get-Credential in the script block which is being run on the server.
Has anyone experienced this before? Or found a solution to this problem?

Answers

  • Jan_VeenendaalJan_Veenendaal Member Posts: 206
    I use this code (NAV 2016 and up), that runs on the server where NAV is installed. The SQL server in my case is a separate server.
    $cred = .....a PSCredentials Object, the user that runs the NAV Service and has access to the database ..... 
    
    #Set up compiling servicetier
    New-NAVServerInstance -ServerInstance Compile -ManagementServicesPort 8900 -ClientServicesPort 8901 -SOAPServicesPort 8902 -ODataServicesPort 8903 -DatabaseServer $ServerName -DatabaseInstance $ServerInstance -DatabaseName $DatabaseName -ServiceAccount User -ServiceAccountCredential $cred
    Set-NAVServerInstance -ServerInstance Compile -Start
            
    #Compile objects
    Compile-NAVApplicationObject -DatabaseServer $ServerNameInstance -DatabaseName $DatabaseName -LogPath $OutputFolder -Recompile -SynchronizeSchemaChanges No -ErrorAction $ErrorActionPreference
    
    #Clean up after compilation
    Set-NAVServerInstance -ServerInstance Compile -Stop
    Remove-NAVServerInstance -ServerInstance Compile -Force
    


    The CompileNAVApplicationObject cmdlet is imported using the NAV module "Microsoft.Dynamics.Nav.Model.Tools.psd1'' (in the RoleTailored Client folder)
    Jan Veenendaal
  • nick_robbonick_robbo Member Posts: 49
    edited 2017-07-03
    Thanks for your reply. Some of this is useful, I didn't realise you could create an instance just for compiling, then remove it. Cool idea.

    I'm not sure if I explained my issue correctly, so I should probably include code examples...

    This is the part of the script I am using, the first part updates the Version List, by running a codeunit. The second is the part where I am getting the error. (When trying to run "Compile-NAVApplicationObject")

    This is the part where I am getting the error relating to logging in to the SQL server.
    Invoke-Command -ComputerName $ComputerName -Credential $Credentials -ArgumentList $Arguments, ` $FromServerInstance, $CodeunitIDToRun, $MethodName, $FromDatabaseName, $DatabaseServerName, $VersionListFilter -ScriptBlock {
          param(
            $Arguments,
            $FromServerInstance,
            $CodeunitIDToRun,
            $MethodName,
            $FromDatabaseName,
            $DatabaseServerName,
            $VersionListFilter
          ) 
          Import-Module 'C:\Program Files\Microsoft Dynamics NAV\100\Service\NavAdminTool.ps1' -WarningAction SilentlyContinue -Scope Global -Force| Out-Null
          Import-Module 'C:\Program Files (x86)\Microsoft Dynamics NAV\100\RoleTailored Client\Microsoft.Dynamics.NAV.Model.Tools.psd1' -WarningAction SilentlyContinue -Scope Global -Force | Out-Null
          Import-Module 'C:\Program Files (x86)\Microsoft Dynamics NAV\100\RoleTailored Client\Microsoft.Dynamics.Nav.Apps.Tools.psd1' -WarningAction SilentlyContinue -Scope Global -Force | Out-Null
          Invoke-NAVCodeunit -ServerInstance $FromServerInstance -CodeunitId $CodeunitIDToRun -MethodName $MethodName -Argument $Arguments
          Write-Host "Please supply credentials to allow object compilation.." -ForegroundColor Green
          $Credentials2 = Get-Credential
          if(!$Credentials2){exit} 
          $ServerInstance = Get-NavServerInstance $FromDatabaseName     
          Compile-NAVApplicationObject -DatabaseName $FromDatabaseName -DatabaseServer $DatabaseServerName `
           -Filter $VersionListFilter -NavServerInstance $FromDatabaseName -SynchronizeSchemaChanges 'Yes' `     -Username $Credentials2.UserName -Password $Credentials2.Password
      }
    

  • Jan_VeenendaalJan_Veenendaal Member Posts: 206
    I create a new servicetier because there usually is not a service available (I create a new database from a CRONUS backup, import all my objects as .txt from a TFS repository, and then I need to have a service tier to be able to compile)

    The credentials that you use must be credentials of a user that has access to the database on SQL level - so maybe not a NAV user. I think this should be the same user account that is used to run the NAV service.
    Jan Veenendaal
  • nick_robbonick_robbo Member Posts: 49
    Yes I my user accound credentials have access at a SQL Level, I am working every day with this system through NAV and SQL. So I definitely have access, I am beginning to think this is a double-hop issue, and it can't pass my credentials again to the SQL Server, after already passing them to the Remote Computer.
Sign In or Register to comment.