/
Export-NAVALfromNAVApplicationObject.ps1
80 lines (73 loc) · 2.93 KB
/
Export-NAVALfromNAVApplicationObject.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function Export-NAVALfromNAVApplicationObject {
[CmdletBinding()]
param (
[String]$ServerInstance,
[String]$WorkingFolder,
[String]$TargetPath,
[String]$Filter,
[Switch]$OpenResultFolderInVSCode,
[int]$extensionStartId = 70000000
)
#Import NAV Modules - we need the $NAVIde
Import-NAVModules | Out-Null
if ([String]::IsNullOrEmpty($NAVIDE)) {
Write-Error "Please make sure the module 'Microsoft.Dynamics.NAV.Model.Tools' is present on this machine"
}
#Initialize variables
$NAVServerInstanceObject = Get-NAVServerInstanceDetails -ServerInstance $ServerInstance
$LogFile = "$WorkingFolder\Log_ExportFile.txt"
$ExportFile = "$WorkingFolder\ExportFile.txt"
if (Test-Path "$WorkingFolder\navcommandresult.txt") {Remove-Item "$WorkingFolder\navcommandresult.txt"}
if (test-path $ExportFile) {remove-item $ExportFile}
#Get DatabaseServer
$Servername = $NAVServerInstanceObject.DatabaseServer
if (-not([string]::IsNullOrEmpty($NAVServerInstanceObject.DatabaseInstance))) {
$Servername += "\$($NAVServerInstanceObject.DatabaseInstance)"
}
$NAVFolder = 'C:\Program Files (x86)\Microsoft Dynamics NAV\71\RoleTailored Client'
$exportfinsqlcommand = """$NAVIde"" command=ExportToNewSyntax,file=$ExportFile,servername=""$Servername"",database=""$($NAVServerInstanceObject.DatabaseName)"",Logfile=$LogFile"
if ($Filter -ne "")
{$exportfinsqlcommand = "$exportfinsqlcommand,filter=$Filter"}
$Command = $exportfinsqlcommand
Write-Host -ForegroundColor Green "Export Objects with:"
Write-host -ForegroundColor Gray " $Command"
cmd /c $Command
$ExportFileExists = Test-Path "$ExportFile"
If (-not $ExportFileExists) {
write-error "Error on exporting to $ExportFile. Look at the information below."
if (Test-Path "$WorkingFolder\navcommandresult.txt") {Type "$WorkingFolder\navcommandresult.txt"}
if (Test-Path $LogFile) {type $LogFile}
break
}
else {
$NAVObjectFile = Get-ChildItem $ExportFile
if ($NAVObjectFile.Length -eq 0) {
Remove-Item $NAVObjectFile
}
if (Test-Path "$WorkingFolder\navcommandresult.txt") {
Type "$WorkingFolder\navcommandresult.txt"
}
}
#Split
$SplitDir = "$WorkingFolder\Split"
Write-Host "Splitting objects to $SplitDir"
Split-NAVApplicationObjectFile -Source $ExportFile -Destination $SplitDir -Force
#Convert
$NAVFolder = [io.path]::GetDirectoryName($NAVIDE)
$Convertcommand = """$NAVFolder\txt2al.exe"" --source=""$SplitDir"" --target=""$TargetPath"" --rename --extensionStartId=$extensionStartId"
$Command = $Convertcommand
Write-Host -ForegroundColor Green "Convert objects with:"
Write-host -ForegroundColor Gray " $Command"
cmd /c $Command
#Open
if ($OpenResultFolderInVSCode) {
code $TargetPath
}
}