Remotly install NAV with PowerShell

This will be my first post in a series of many that will cover a Dynamics NAV installation in Azure or on Premise.  Everything is set up as on premise so if you are planning to use this on Azure you will need to run the scripts from an Azure VM.

Every machine that I am installing on needs to allow PowerShell Remote Session.  Make sure that the Windows Management Framework 3.0 is installed.  Then make sure that you enable Remote Management.

OpenRemoteManagement

That should take care of the basics and now we will be working on our management machine where all the installs will be running from.

In the directory that I store my scripts, I create a subfolder for each machine that requires an install.  In that folder I have few files.

First I have a file called Set-MachineSettings.ps1 that contains the setup parameters for this machine.  Only two lines.

[code lang=”powershell”]$NAV_RemoteMachineAddress = ‘kl4msweb05’
$ClientServicesCredentialType = ‘NavUserPassword'[/code]

I also have an XML file that holds the installation configuration for Dynamics NAV

[code lang=”xml”]

[/code]

In the parent folder I store the general deployment settings file called Set-DeploySettings.ps1 that includes all setup settings that for the deployment.

[code lang=”powershell”]$NAV_DvdLocation = ‘N:\NAV2013R2\NAVDVD’
$NAV_RemoteFolder = ‘C:\REMOTE\’
$NAV_AdminRemoteDirectory = Join-Path $NAV_RemoteFolder "NAVAdministration"
$NAV_NavDvdRemoteDirectory = Join-Path $NAV_RemoteFolder "NavDvd"
$NAV_CertRemoteDirectory = Join-Path $NAV_RemoteFolder "Cert"
$NAV_ServerAddins = ‘N:\NAV2013R2\Add-Ins\Server’
$NAV_ClientAddins = ‘N:\NAV2013R2\Add-Ins\Client’

$NAVAdminUserName = ‘NAVAdmin’
$NAVAdminPassword = ‘NAV.Admin.2013R2’

# Specifies language and regional settings for NAV Web Server
$WebServerLanguage = ‘is-IS’
$WebServerRegionFormat = ‘is-IS’

# Security Certificates for NAV Client Services
#
# Specifies the security certificate PFX file and password to use with Microsoft Dynamics NAV client services.
# For more information about certificates, see http://go.microsoft.com/fwlink/?LinkID=285869.
# A sample pfx file called MyAzureVM.pfx is available in the WindowsPowerShellScripts\Cloud\Examples\HowTo directory. The password is pfxpassword.
$ClientServicesPfxFile = ‘N:\NAV2013R2\Uppsetning\kappi-is.pfx’
$ClientServicesPfxPassword = ‘CertitificatePassword’
$DnsIdentity = ‘kappi.is’
$ServerDNSName = ‘www.kappi.is’
$applicationPublisher = "Kappi"

# Security Certificate to enable HTTPS for NAV Web Client
#
# Specifies the security certificate PFX file and password for configuring https protocol for web server authentication with the Microsoft Dynamics NAV Web Client.
# If you provide an empty value for the $NAV_HttpsWebClientPfxFile parameter, then the script will generate and install a self signed certificate remotely.#
# HTTPS helps secure the connection between clients and NAV web server
# For more information about certificates, see http://go.microsoft.com/fwlink/?LinkID=285869.
$HttpsWebClientPfxFile = ‘N:\NAV2013R2\Uppsetning\kappi-is.pfx’
$HttpsWebClientPfxPassword = ‘CertitificatePassword’

# Specifies the ClickOnce signing settings for the NAV Windows Client.
# If you sign the deployment, then the ClickOnce install page presented to end users will say that the publisher has been verified.
# If you don’t sign, then end users will get warning that the publisher cannot be verified.
# To sign ClickOnce, set $NAV_ClickOnceCodeSigningPfxFile to the path and name of the certificate pfx file, and then set $NAV_ClickOnceCodeSigningPfxPassword to the pfx password.
# A sample pfx file called ClickOnceSignature.pfx is available in the WindowsPowerShellScripts\Cloud\Examples\HowTo directory. The password is clickoncesignaturepassword.
# To not sign ClickOnce, set the values to $null.
$ClickOnceCodeSigningPfxFile = Join-Path $NAV_DvdLocation ‘WindowsPowerShellScripts\Cloud\HowTo\ClickOnceSignature.pfx’
$ClickOnceCodeSigningPfxPassword = ‘clickoncesignaturepassword’

# Tool dependencies

# The following tools must be installed on the provisioning computer:
# Azure.psd1 – Part of Windows Azure PowerShell, which can be downloaded from http://www.windowsazure.com/en-us/manage/downloads/ (select "install" under Windows)
# mage.exe – Part of the Microsoft Windows SDK for Windows 7 and .NET Framework 4, which can be downloaded from http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=8279. This is included with Windows 8 and Windows Server 2012.
# winhttpcertcfg.exe – The Microsoft Windows HTTP Services (WinHTTP) Certificate Configuration Tool. Available at http://www.microsoft.com/en-us/download/details.aspx?id=19801
# makecert.exe – The Certificate Creation tool to generate x.509 certificates for testing purpose. This tool is available as part of the Windows SDK, which you can download from http://go.microsoft.com/fwlink/p/?linkid=84091.
# The following variables specify the path and file name of the tools.
$Global:MageExeFile = Join-Path ${env:ProgramFiles(x86)} ‘Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\mage.exe’
$Global:WinHttpCertCfgExeFile = Join-Path ${env:ProgramFiles(x86)} ‘Windows Resource Kits\Tools\winhttpcertcfg.exe’

# Windows PowerShell includes a set of variables that enable you to customize its behavior.
# Get help on all PowerShell preference parameters by issuing: Get-Help about_Preference_Variables
$verbosePreference = ‘Continue’
# To not display the verbose message and continue executing, use ‘SilentlyContinue’ (default PowerShell behavior)
$errorActionPreference = ‘Continue’
# To stop execution on first error, use ‘Stop’ (default value is ‘Continue’)
# To display the error message and ask you whether you want to continue, use ‘Inquire’
[/code]

Finally I have the script Install-NAV.ps1 in each computer subfolder that I execute with PowerShell ISE as Administrator.

[code lang=”powershell”]Set-StrictMode -Version 2.0

Import-Module ‘N:\NAV2013R2\NAVDVD\WindowsPowerShellScripts\Cloud\NAVRemoteAdministrationSamples\NAVRemoteAdministrationSamples.psm1’ -DisableNameChecking
Import-Module ‘N:\NAV2013R2\NAVDVD\WindowsPowerShellScripts\Cloud\NAVRemoteAdministration\Misc\Import-NAVAdministrationModuleRemotely.ps1’ -DisableNameChecking

# Import settings
$PSScriptRootV2 = Split-Path $MyInvocation.MyCommand.Definition -Parent
. (Join-Path $PSScriptRootV2 ‘..\Set-DeploySettings.ps1’)
. (Join-Path $PSScriptRootV2 ‘Set-MachineSettings.ps1’)

#New-NavAdminSession
[int]$currentMemoryLimitPerPSSessionInMB = Get-MaxMemoryPerShellRemotely -RemoteMachineAddress $NAV_RemoteMachineAddress
$requiredMemoryLimitPerSessionInMB = 1024
if(($currentMemoryLimitPerPSSessionInMB -ne 0) -and ($currentMemoryLimitPerPSSessionInMB -lt $requiredMemoryLimitPerSessionInMB))
{
Set-MaxMemoryPerShellRemotely -Value $requiredMemoryLimitPerSessionInMB -RemoteMachineAddress $NAV_RemoteMachineAddress
}

Write-Verbose "Creating remote PS session on $NAV_RemoteMachineAddress…"
$Session = New-PSSession -ComputerName $NAV_RemoteMachineAddress
Write-Verbose "Done creating remote PS session on $NAV_RemoteMachineAddress."

try
{
# Import the NAVAdministration module into the remote PS session
Write-Verbose (‘Copying the NAVAdministration module to ‘ + $Session.ComputerName + ‘ and importing it into the remote PS session…’)
$navAdministrationDirectory = Join-Path $NAV_dvdLocation ‘WindowsPowerShellScripts\Cloud\NAVAdministration’

Import-NAVAdministrationModuleRemotely `
-LocalDirectory $navAdministrationDirectory `
-RemoteDirectory $NAV_AdminRemoteDirectory `
-Session $Session
Write-Verbose (‘Done copying the NAVAdministration module to ‘ + $Session.ComputerName + ‘ and importing it into the remote PS session.’)

# Copy the NAV DVD to the remote machine through the remote PowerShell session (slower but doesn’t have a dependency on Azure Storage)
Write-Verbose ("Copying the NAV DVD to the remote machine at " + (Get-Date).ToLongTimeString() + "…")
#Copy-DirectoryToRemoteMachine -SourceDirectory $NAV_DvdLocation -RemoteDirectory $NAV_NavDvdRemoteDirectory -psSession $Session
Write-Verbose ("Done copying the NAV DVD to the remote machine at " + (Get-Date).ToLongTimeString() + ".")

# Install the SSL certificate that is used for HTTPS on the NAV Web Client
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate = $null

Write-Verbose "Importing certificate for https authentication on remote machine…"

[System.Security.SecureString]$HttpsWebClientPfxPasswordAsSecureString = ConvertTo-SecureString $HttpsWebClientPfxPassword -AsPlainText -Force

$Certificate = Import-PfxFileRemotely `
-PfxFile $HttpsWebClientPfxFile `
-PfxPassword $HttpsWebClientPfxPasswordAsSecureString `
-Session $Session

# Get installed certificate thumbprint
$WebServerSSLCertificateThumbprint = Get-CertificateThumbprint -Certificate $Certificate

# Prepare NAV Installer Configuration file for NAV Web Site SSL Binding specific to current deployment
$TemplateSetupConfigFile = (Join-Path $PSScriptRootV2 ‘NAVBoxConfigFile.xml’)
$SetupConfigFile = Prepare-NAVInstallerConfigFileForWebSiteSSLBinding -TemplateSetupConfigFile $TemplateSetupConfigFile -WebServerSSLCertificateThumbprint $WebServerSSLCertificateThumbprint

# Run the NAV Installation with a 1 Box setup
Install-NAV -RemoteNavDvdLocation $NAV_NavDvdRemoteDirectory -SetupConfigFile $SetupConfigFile -PSSession $Session

#Upload Files to Remote Machine

# Copy the Add-ins to the remote machine through the remote PowerShell session (slower but doesn’t have a dependency on Azure Storage)
Write-Verbose ("Copying the Add-ins to the remote machine at " + (Get-Date).ToLongTimeString() + "…")
Copy-DirectoryToRemoteMachine -SourceDirectory $NAV_ServerAddins -RemoteDirectory ‘C:\Program Files\Microsoft Dynamics NAV\71\Service\Add-ins’ -psSession $Session
Copy-DirectoryToRemoteMachine -SourceDirectory $NAV_ClientAddins -RemoteDirectory ‘C:\Program Files (x86)\Microsoft Dynamics NAV\71\RoleTailored Client\Add-ins’ -psSession $Session
Write-Verbose ("Done copying the Add-ins to the remote machine at " + (Get-Date).ToLongTimeString() + ".")

# If your application requires that you install server-side add-ins or copy additional files to the virtual machine,
# then you can customize the scripts to accomplish that. This sample code copies a single file to the VM.
# Update the code with your file(s) and uncomment the line.
# Copy-FileToRemoteMachine -SourceFile ‘C:\MyAddin.dll’ -DestinationFile ‘C:\Program Files\Microsoft Dynamics NAV\71\Service\Add-ins\MyAddin\MyAddin.dll’ -Session $psSession

}
finally {
Remove-NAVAdminSession -Session $Session
}
[/code]

The machine is now ready for the next step, to install NAV Server Instances, Tenants, ClickOnce and Web Sites. Before I will show that the next post will be about creating the databases with scripts.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.