xlfm22 发表于 2018-9-2 13:00:13

学习 NAV PowerShell之部署Microsoft Dynamics NAV

#准备工作  
$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 FOR LOGIN "
  
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


页: [1]
查看完整版本: 学习 NAV PowerShell之部署Microsoft Dynamics NAV