[UPDATE on 2010-05-15]

To documentation for this powershell module has been placed here: http://cid-19ec39cb500669d8.skydrive.live.com/self.aspx/Public/Dynamics/Powershell/DynamicsAX2009-Powershell.docx

 

if you read this blog, you know I love command line interfaces – so this shouldn’t be a surprise. If you look at the Dynamics AX BI CodePlex project, you’ll see a Visual Studio 2008 project called “DynamicsAXPS”.

image

 

This is a Powershell 2.0 module that lets you start exploring and controlling AX from the command line. In this post I’ll demonstrate how to use it.

First, install Powershell 2.0 and start it. This module assumes you already have the AX Client installed on the machine and configured to connect to an AX Server.

LOADING THE MODULE

Once you build the project you’ll see these DLLs in the bin/debug directory… The module itself is the file called “DynamicsAXPS.dll”

image

Now load it via this command

Import-Module .\DynamicsAXPS.dll

image

The warnings are because I have some non-standard cmdlet verbs defined.

FINDING AX CMDLETS

Get-Command -Module DynamicsAXPS

image

INTERROGATING THE AOT

Let’s get a node in the AOT – I’ll choose the CustTable

$custtable = Get-AXNode "\Data Dictionary\Tables\CustTable"
$custtable | Get-Member

image

Of course we can pipe this to Out-GridView …

 

$custtable = Get-AXNode "\Data Dictionary\Tables\CustTable"
$custtable | Get-Member | Out-GridView

image

image

What about the child nodes under CustTable? Use Get-AXNodeChildItem

Get-AXNodeChildItem "\Data Dictionary\Tables\CustTable"

image

What if we wanted all the nodes underneath CustTable recursively? Just add the –Recurse flag. As you can see there are a lot of nodes

 

Get-AXNodeChildItem "\Data Dictionary\Tables\CustTable" -Recurse

image

Use Get-AXNodeProperties to get info about the properties for a specific node.

Get-AXNodeProperties "\Data Dictionary\Tables\CustTable"

image

LABELS

If you need to get the value for a label us Get-AXLabel

Get-AXLabel SYS11307

image

You can ask for multiple labels at the same time

Get-AXLabel SYS11307,SYS125115

image 

Getting MetaData for Tables, Enums, and EDTs

There are three commands available.

One for Extended Data Types.

Get-AXEDTMetaData ABCModelType

image

One for Enums:

Get-AXEnumMetaData ABCModel

image

One for Tables

Get-AXTableMetaData CustTable

image

If you are curious about the metadata, you can retrieve it from the metadata objects that are returns

Here’s how to find out about the fields.

$md = Get-AXTableMetaData CustTable
$md.Fields | Out-GridView

image

OK, that’s a lot of fields, we can filter it down

 

$md = Get-AXTableMetaData CustTable
$md.Fields | select Name,DataType,FieldType,DisplayLength  | Out-GridView

image

 

GETTING DATA FROM A TABLE

$datatable = Get-AXTableData -Table CustTable -Fields *
$datatable.Rows | Out-GridView

image

image

 

$datatable = Get-AXTableData -Table CustTable -Fields AccountNum,Name,Phone
$datatable.Rows | Out-GridView

image

 

CREATE A TABLE VIA AN XPO

Create-XPOForTable -Name Foo -Filename "D:\\Create_Table_Foo.xpo" -Fields A,B,C –FieldTypes INT,STRING,REAL

This creates a simple XPO file that will create that table.

Just import the XPO like any other and your table is ready

image

image

ADD RECORDS TO A TABLE

Add-AXRecord -Table Foo -Fields A,B,C  -Values (1,"FOO",3.3)
Add-AXRecord -Table Foo -Fields A,B,C  -Values (2,"Bar",7.1)

And if you open the table in AX …

image

You’ll see the data has been placed in the table

 image