Axaptapedia is now maintained by AgileCadence For more information please click here.

PowerShell

From Axaptapedia
Jump to: navigation, search

PowerShell is a command line interface for Windows.

Here is some information for administering Dynamics Ax with PowerShell

PSh has some advantages such as

  • object-oriented interface between commands
  • it uses the Microsoft .NET framework (so, I think, it can use business connector)
  • graphical shell

Hand grenade instead of sniper rifle[edit]

If you use Windows Explorer, you can find some new abilities to work with groups of objects. Unlike cmd.exe, PowerShell can work with different objects in the same way.

For example, here is a command which stops all AOSes

  spsv aos*

where spsv is commandlet for stopping services, aos* is a mask for all services which have name starting with string "aos"

Fire and forget[edit]

Sometimes I need to start an AOS after the deletion of application indexes. When I do it with "services" control panel applet, it waits for a while than in outputs a message, saying that service start took longer than expected and it stops to monitor the starting of the service.

Here is how I do it with PSh:

Powershell.png

  • I enter "sasv aos6*1;done"
  • this command
    • starts the service
    • wait while it starts
    • informing me about service is running

Where

  • sasv - the alias for the Start-Service commandlet
  • aos6*1 - name of the AOS service (you can type "gsv aos*" to list all AOSes with names and description, the Dollar sign has a special meaning in PSh so it is escaped with `. when I am working with Ax 2009, I write just like "sasv aos5*1" because there is no any other service which is matching such a mask)
  • done - is a function which is defined in my profile outputting just a message "done"

<xpp> [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") function msgBox($x){

   [System.Windows.Forms.MessageBox]::Show($x, 'Done!:PowerShell', 

[Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information, [Windows.Forms.MessageBoxDefaultButton]::Button1, [Windows.Forms.MessageBoxOptions]::ServiceNotification

   )

} function done(){

   msgBox "done"

} </xpp>

So there is no need to switch to "services" applet constantly pressing "Refresh"

Something like SQL[edit]

PowerShell has some commands which work like SQL. For example, when I looking for extensions of indexes which are related to application labels, I typed the following

 ls *ru* | group extension

it means output all files, that have "ru" sub string in name and group it by extension

 Count Name                      Group                                                                  
 ----- ----                      -----                                                                  
   1 .ahi                      {C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\Dynamics...
   1 .alt                      {C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\Dynamics...
   1 .ahd                      {C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\Dynamics...
   1 .alc                      {C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\Dynamics...
   1 .ald                      {C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\Dynamics...
   1 .ali                      {C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\Dynamics...

It printed groups, with the following columns:

  • count - number of elements in the group
  • name - name of the group
  • group - elements in the group

because I wanted only names, I typed:

 ls *ru* | group extension | select name

which has in result

 Name                                                                                                   
 ----                                                                                                   
 .ahi                                                                                                   
 .alt                                                                                                   
 .ahd                                                                                                   
 .alc                                                                                                   
 .ald                                                                                                   
 .ali

Using the .NET Business Connector from PowerShell[edit]

from www.crankturner.com

Ok, we've made the big jump to Dynamics AX 2009! Wow, lots of work going all of the way from Axapta 3.0

One of the hurdles was dealing with the COM connector. This is no small undertaking - the COM interface has CHANGED! It was time to upgrade to the .NET business connector. After installing the .NET client dll, we are now able to connect to our servers using the .NET Business Connector. Here is an example in PowerShell.


 # load the .dll (similar to the c# "Using" statement)   
 [reflection.Assembly]::Loadfile("C:\Program Files\Microsoft Dynamics AX\50\Client\Bin\Microsoft.Dynamics.BusinessConnectorNet.dll")   
 # or [Reflection.Assembly]::LoadWithPartialName("Microsoft.Dynamics.BusinessConnectorNet")
 # or [Reflection.Assembly]::Load("Microsoft.Dynamics.BusinessConnectorNet, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") 
 $ax = new-object Microsoft.Dynamics.BusinessConnectorNet.Axapta   
 
 # Look at the properties   
 # More info at: http://msdn.microsoft.com/en-us/library/aa626373.aspx   
 # $ax | get-member   
 
 $ax.logon("","","","")   
 $o = $ax.CallStaticClassMethod("SysLabel","labelId2String2","@SYS21669" )    
 $o # Should be "ABC" 
 
 # Look at some session info   
 $xSession = $ax.CreateAxaptaObject("XSession")   
 $xSession.call("AOSName")     
 $xApplication = $ax.CreateAxaptaObject("XApplication")   
 $xApplication.call("buildNo")  
 $ax.CallStaticClassMethod("Session", "isBusinessConnector")     
 $ax.CallStaticClassMethod("Session", "getAOSInstance")     
 $ax.CallStaticClassMethod("Session", "getAOSPort")     
 $ax.logoff()

Links[edit]