NAVmoble - the pocket-sized ERP
Optimized for Microsoft Dynamics NAV and Windows Mobile powered devices

Friday, February 16, 2007

“How to” Series: Automating Windows Mobile with Windows PowerShell

A lot of noise is going around the Windows PowerShell these days. I like this tool and definitely it may be of use even for the embedded developer.

Are you having some repeating tasks in your daily dev activities? Are you copying files on/from the device, manipulating registry keys, start /stop processes, etc... All these may be automated using Windows PowerShell.

I'll give 2 short examples, here – just to illustrate the idea.

Microsoft provides API called Remote Applications Programming Interface - a.k.a. RAPI. It allows to "touch" the device from the desktop machine, when the device is cradled. It works via ActiveSync or Windows Mobile Device Center on Vista. Unfortunately the RAPI itself is unmanaged and will be a little tricky to use it from a PowerShell script. So, here comes the RAPI class from the OpenNETCF.Desktop.Comunication library - a managed RAPI wrapper.

Example 1: Copying pictures from the device

The following example connects to the cradled device and copies all the pictures located on the device (\My Documents\My Pictures\) in the current location on the desktop

Note that in this example you should have
OpenNETCF.Desktop.Communication.dll placed
in D:\RAPI. You may change the path or think about a smarter assembly loading scheme.

[System.Reflection.Assembly]::LoadFrom("d:\RAPI\OpenNETCF.Desktop.Communication.dll") |Out-null

$rapi = New-Object OpenNETCF.Desktop.Communication.RAPI

$desktopPath = Get-Location

$desktopPath = $desktopPath.Path

$devicePath = "\My Documents\My Pictures\"

echo "Connecting to device..."

$rapi.Connect()

$pictures = $rapi.EnumFiles($devicePath+"*.jpg")

$pictures.Count.ToString()+" pictures found"

echo "-------------------------------"

foreach($f in $pictures)

{

  $remoteFile=$devicePath+$f.FileName

$localFile=$desktopPath+$f.FileName;

"Copying "+$remoteFile+" to "+$localFile

$rapi.CopyFileFromDevice($localFile,$remoteFile,1)

}


 

Example 2: Creating a registry key

This example reads and displays a list of registry keys on the screen – the installed applications in this case. Then it creates a new registry key called "My Key"

Note that in this example you should have
OpenNETCF.Desktop.Communication.dll placed in D:\RAPI. You may change the path or think about a smarter assembly loading scheme.

[System.Reflection.Assembly]::LoadFrom("d:\RAPI\OpenNETCF.Desktop.Communication.dll") |Out-null

echo "---------------------------"

echo "Installed applications list"

echo "---------------------------"

[OpenNETCF.Desktop.Communication.CERegistry]::LocalMachine.OpenSubKey("SOFTWARE\Apps").GetSubKeyNames()

echo "---------------------------"

echo "Create new key "

echo "---------------------------"

[OpenNETCF.Desktop.Communication.CERegistry]::LocalMachine.OpenSubKey("SOFTWARE\Apps").CreateSubKey("MyKey")|Out-Null

echo "My Key" created


 

You may think about automating even more useful and boring tasks - task that you do not want to do manually every day. Explore the OpenNETCF.Desktop.Comunication library to discover even more useful features.

Links:

Windows PowerShell Team's Blog

OpenNETCF.Desktop.Communication library downloads

OpenNETCF.Desktop.Comunication Docs

Enjoy.

2 comments:

Anonymous said...

Example 1 does not work correctly. There is a "\" missing from the end of the desktop path.
Also the copy command errors for folders.

Ruslan Trifonov said...

The example is not intended to work on every single environment in the world
I'm just giving the idea here - how to automate some device-related tasks using power shell.

If you environment does not have the exact paths illustrated in the example , the example wiil fail
If you want to use the example you should tailor it regarding your environment