在本次的咖啡时间里面我们介绍如何部署自动Microsoft Dynamics NAV。我们假设已经在一台机器上安装好了Microsoft Dynamics NAV,但是我们想增加额外的Microsoft Dynamics NAV安装/实例。


咖啡时间2:自动部署Microsoft Dynamics NAV


背景:

客户在以下情况下会增加一个新的Microsoft Dynamics NAV系统

--新建一个分支机构

--新建一系列测试

--开始一个新的财务年度

--等等


在实践阶段一个“新的Microsoft Dynamics NAV系统”可以使一个新的Microsoft Dynamics NAV 实例,一个database,一个company或者是一个tenant。为了简化介绍我们重用现有的Microsoft Dynamics NAV Service创建我们的新的database。


准备工作:

我们使用import-module sqlps.来导入SQLServer的PowerShell。安装SQLServer的时候提供了SQLPS,但是不安装SQL也可以获得该module。

如果在运行PS的机器上没有安装SQLSever,可以按照下面的地址安装Microsoft SQL Server 2012Feature Pack:


http://www.microsoft.com/en-us/download/details.aspx?id=29065


以下是代码

#准备工作
$MyNAVServerName = "DynamicsNAV80"
$MySQLServerName = "."
$MyNewCustomerName = "NewCustomer"
$MyNewDatabaseName = "NewCustomerDatabase"
Set-ExecutionPolicy unrestricted
import-module "C:\Program Files\Microsoft Dynamics NAV\80\Service\NavAdminTool.ps1"

Push-Location #jump back to standard prompt with pop-location
import-module sqlps #ignore any warnings you may get

#Restore SQL db (NAV demo db)
#Relocate database files http://stackoverflow.com/questions/26400752/cannot-bind-relocatefile-when-using-restore-sqldatabase-cmdlet 

$mdf = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("Demo Database NAV (8-0)_Data", "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\DB_Data_$MyNewCustomerName.mdf")
$ldf = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("Demo Database NAV (8-0)_Log", "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\DB_Log_$MyNewCustomerName.ldf")

restore-SqlDatabase -ServerInstance $MySQLServerName `                   
                    -Database $MyNewDatabaseName `                    
                    -BackupFile "C:\NAVDVD\SQLDemoDatabase\CommonAppData\Microsoft\Microsoft Dynamics NAV\80\Database\Demo Database NAV (8-0).bak" `                    
                    -ReplaceDatabase `                    
                    -RelocateFile @($mdf,$ldf) 
                                      
#Set network service as dbo
$CreateServiceAccountUser = "CREATE USER [NT AUTHORITY\NETWORK SERVICE] FOR LOGIN [NT AUTHORITY\NETWORK SERVICE]"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $CreateServiceAccountUser$AddServiceAccountAsDbo = "exec sp_addrolemember 'db_owner', 'NT AUTHORITY\NETWORK SERVICE'"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $AddServiceAccountAsDbo
pop-location # Finished with SQL commands so popping back to normal PS prompt

#For NAV 2013(R2), to convert the database to latest executable version.
#For NAV 2015 we don't need this (in fact we don't have the Invoke-DatabaseConversion cmdlet).
import-module "C:\PSscripts\Upgrade\Cmdlets\NAVUpgradeCmdlets.psm1"
Invoke-NAVDatabaseConversion -DatabaseServer localhost -DatabaseName "Demo Database NAV (7-1)" -FinSqlExeFile "C:\Program Files\Microsoft Dynamics NAV\71\Service\finsql.exe"

#At this point the next steps depend on whether we want a new NAV Service, connect to an existing one, etc. Here we just reuse existing NAV Service
#Configure then restart the service and get status
Set-NAVServerConfiguration $MyNAVServerName -KeyName DatabaseName -KeyValue $MyNewDatabaseName 
Set-NAVServerInstance $MyNAVServerName -restart
Get-NAVServerInstance $MyNAVServerName

-----------------------------------------------------------------------------------------------------------------------------------------------------------

原文地址

http://blogs.msdn.com/b/nav/archive/2015/02/13/nav-powershell-for-your-coffee-break-deploy-nav-via-powershell.aspx


-------------------------------------------------------------------------------------------

nutcracker点评

如果希望新建一个Nav Service Instance的话,可以加入这段代码

New-NAVServerInstance NewInstance -ManagementServicesPort 8099 -ClientServicesPort 8100 -SOAPServicesPort 8101 -ODataServicesPort 8102 -verbose