Coffee Break – Search in a Dynamics NAV object file using Windows PowerShell
This coffee break post illustrates how to search a text file for specific words or a phrase. You can do this with Windows PowerShell in any text files, but let’s use some Dynamics NAV objects exported as text. Technically speaking we are reading a text file then piping it line for line through a search cmdlet, which pipes matching lines further to a log.txt file.
Coffee Break 5 – Searching through a Dynamics NAV text file
Customer story:
The developer wants an automated way of locating all occurrences of a string (table name, field name, comment, …) in a country-specific version of Dynamics NAV, in this example the Norwegian version. And we will log the output of this search to a log file in c:\NAVApp\Log.
Exporting objects from Dynamics NAV:
Prerequisites:
Crete a folder C:\MyNAVApp with a subfolder \Log so that the resulting full path is C:\MyNAVApp\Log.
For this purpose we use the Microsoft Dynamics NAV Application Merge Utilities. Note that these install to the equivalent of C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client. This time we don’t need to import the Management module, only the application merge utilities:
Import-Module “${env:ProgramFiles(x86)}\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1”
- Note 1: Make sure to import Microsoft.Dynamics.Nav.Model.Tools.psd1, and not NavModelTools.ps1.
- Note 2: This will load the path for finsql.exe too, and use the finsql in the client folder.
Set a few variables. Assuming that we work in folder C:\MyNAVApp\, and we will be searching for where “G/L Account” table reference is used. And we will log the output of this search to a log file in the c:\MyNAVApp\Log folder.
$objectPath = ‘C:\MyNAVApp’
$sourcepath = Join-Path $ObjectPath ‘\MyObjects’
$NAVobjects = Join-Path $ObjectPath ‘NAVobjects.txt’
$LogPath = Join-Path $ObjectPath ‘\log\whereused.txt’
#Note, a search string can also be an array of strings
$SearchString = ‘”G/L Account”’
Export the objects you like, either all objects:
Export-NAVApplicationObject –DatabaseName “Demo Database NAV (8-0)” –DatabaseServer “.\NAVDEMO” –Path $NAVObjectFile
Or filter (choose the filter you like):
$FilterString = “Version List=*NAVNO*”
#or
$FilterString = “Modified=Yes”
Export-NAVApplicationObject –DatabaseName “Demo Database NAV (8-0)” –DatabaseServer “.\NAVDEMO” –Path $NAVObjects –Filter $FilterString
#Split into individual object files
split-navapplicationobjectfile –Source $NAVobjects –Destination $sourcepath –PassThru -Force
Now load the list of files in the folder. We’re using the -File parameter with Get-ChildItem to limit the scope to files only (sub folders are not included).
$myobjects = Get-ChildItem –Path $SourcePath –Filter *txt -File
The next line shows a very simple way to read through all text files in the specified path (c:\MyNAVApp) and for each file searches for the search string (in our case “G/L Account) and for each hit pipe the source line to the log file along with the line number. For this we will use the Select-String cmdlet, that can work directly on Objects with File Info (objects returned by calling Get-ChildItem cmdlet).
$myobjects | Select-String $SearchString | Out-File –Filepath $LogPath
Note that using the parameters and segments above implies that:
- The script raises an error if the $ObjectPath does not exist.
- Out-File will overwrite the existing file per pipeline
- Out-File will append lines to the file, per pipeline object
Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA
Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA
Bas Graaf, Senior Software Engineer Dynamics NAV