In PowerShell:
$tablesInPermissionSets = @()
$permissionSets = gci . -Recurse -Filter '*.al' | ? {(gc $_.FullName).Item(0).startsWith('permissionset')}
$permissionSets | % {
$content = gc $_.FullName -Raw
[Regex]::Matches($content, '(?<=tabledata ).*(?= =)') | % {
$tablesInPermissionSets += $_.Value
}
}
$tablesInTables = @()
$tables = gci . -Recurse -Filter '*.al' | Where-Object {(Get-Content $_.FullName).Item(0).StartsWith('table ')}
$tables | % {
$content = gc $_.FullName -Raw
[Regex]::Matches($content, "(?<=table \d+ ).*(?=$([Environment]::NewLine))") | % {
$tablesInTables += $_.Value
}
}
$missingTables = ""
Compare-Object $tablesInTables $tablesInPermissionSets | ? SideIndicator -eq '<=' | % {
$missingTables += $_.InputObject + [Environment]::NewLine
}
if ('' -ne $missingTables) {
throw "Missing table permissions: $missingTables"
}
In English:
- Find all the files in the current folder, and child folders, with a filename ending in .al and which have a first line starting with “permissionset”
- Build a collection of the tabledata objects that are referenced in those permission sets
- Find all the files in the current folder, and child folders, with a filename ending in .al and which have a first line starting with “table ” (with a space to avoid matching “tableextension”)
- Build a collection of the names of the tables
- Use
Compare-Object
to compare the collections and find names which appear in the list of tables but not in tabledata permissions - Build an error message of missing table permissions
- Throw the error
PowerShell Profile:
Like most small PowerShell scripts that I write, I’ve just added it to my PowerShell profile. Run code $profile
in a PowerShell prompt to open the profile file in VS Code.
function Test-Permissions() {
#...all of the above code
}
Maybe there is already a VS Code extension that checks for this? It would make sense, but I’m pretty minimalist with the extensions that I have installed anyway. I run it from the terminal in VS Code.
2 thoughts on “Tip: Test for Tables Missing from Permission Sets”