Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created October 9, 2017 13:12
Show Gist options
  • Select an option

  • Save JohnLBevan/181bc778deb6d4e9e3a502963b51809e to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/181bc778deb6d4e9e3a502963b51809e to your computer and use it in GitHub Desktop.
Export microsoft dynamics ax 2009 object ids (i.e. contents of UtilIdElements "table") to CSV, using Business Connector to contact AX.
function Invoke-AxQuery {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
$Company
,
[Parameter(Mandatory = $false)]
$Language
,
[Parameter(Mandatory = $false)]
$AOS
,
[Parameter(Mandatory = $false)]
$Config
<# may add at some point, but I don't need & gave "Cannot find an overload for "Logon" and the argument count: '7'" on first run, so needs tweaking to get working... https://msdn.microsoft.com/en-us/library/jj765099.aspx
,
[Parameter(Mandatory = $false)]
$ObjectLayer
,
[Parameter(Mandatory = $false)]
$LayerCode
,
[Parameter(Mandatory = $false)]
$Tenant
#>
)
begin {
#$targetPath = "C:\Temp\Microsoft.Dynamics.BusinessConnectorNet.dll"
#[Reflection.Assembly]::Loadfile($targetPath) | Out-Null
#amended per notes here: https://stackoverflow.com/a/35702348/361842
Add-Type -AssemblyName 'Microsoft.Dynamics.BusinessConnectorNet, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
$ax = new-object 'Microsoft.Dynamics.BusinessConnectorNet.Axapta'
$ax.Logon($Company,$Language,$Aos,$Config)
}
process {
#would be good to pass in a script block to make this function reusable for other queries, but atm that adds complication without immediate benefit...
$UtilIdElements = $ax.CreateAxaptaRecord('UtilIdElements')
#$ax.ExecuteStmt("SELECT * FROM %1",$UtilIdElements) #removed 'code' to improve performance
$ax.ExecuteStmt("SELECT utilLevel, recordType, parentId, name, baseVersion, version, saveCount, id, modifiedDateTime, modifiedBy, createdDateTime, createdBy, recVersion, RecId FROM %1",$UtilIdElements)
while ($UtilIdElements.Found) {
(New-Object -TypeName 'PSObject' -Property @{
utilLevel = $UtilIdElements.get_field('utilLevel')
recordType = $UtilIdElements.get_field('recordType')
parentId = $UtilIdElements.get_field('parentId')
name = $UtilIdElements.get_field('name')
baseVersion = $UtilIdElements.get_field('baseVersion')
version = $UtilIdElements.get_field('version')
saveCount = $UtilIdElements.get_field('saveCount')
id = $UtilIdElements.get_field('id')
#code = $UtilIdElements.get_field('code')
modifiedDateTime = $UtilIdElements.get_field('modifiedDateTime')
modifiedBy = $UtilIdElements.get_field('modifiedBy')
createdDateTime = $UtilIdElements.get_field('createdDateTime')
createdBy = $UtilIdElements.get_field('createdBy')
recVersion = $UtilIdElements.get_field('recVersion')
RecId = $UtilIdElements.get_field('RecId')
})
$UtilIdElements.Next() | out-null
}
$UtilIdElements.Dispose()
}
end {
$ax.Logoff() | out-null
$ax.Dispose() #can't guarantee this will run here, but better to include than not.
}
}
Invoke-AxQuery | Export-CSV 'c:\temp\UtilIdElements.csv' -NoTypeInformation -Encoding UTF8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment