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

Contents

[edit] Hand grenade instead of sniper rifle

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"

[edit] Fire and forget

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:

Image:Powershell.png

  • I enter "sasv aos`$*1<Shift+Enter>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
  • aos`$*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"

[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")
}

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

[edit] Something like SQL

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

[edit] Links

Personal tools
Microsoft Community
Microsoft Dynamics Ax Community