Document directory
Introduction
This article describes how to use Powershell to control Windows Embedded CE and Windows Mobile devices through RAPI.
Reason
I developed AS400 RPG and unix c when I entered the line. All development environments are character interfaces, so I am used to the development mode of vi + grep + make. Later, I started to develop Windows, and I was not very familiar with it. I installed cygwin to use it, but it was not necessary to use the command line. In fact, the rational use of command lines can greatly simplify daily work. I remember a book called efficient programmers, which mentioned the rational use of command lines to simplify daily work. I strongly agree with this statement. The following uses an example to describe how to simplify the work.
AS/400 Interface
UNIX Interface
Cygwin Interface
We are currently developing Windows Embedded CE 6 R3 applications. For some reason, the devices used do not have backup batteries. Therefore, each time you restart the device, the device time is restored to the initial state. After the system is started, I need to manually modify the system time of the device. At least 20 mouse clicks are required to complete the time setting. This is a very annoying thing. As a programmer, we should try to hand over a large number of repeated operations to the computer, so I want to use the script to complete the time settings. Then I want to use Microsoft PowerShell to complete this task.
Why PowerShell?
PowerShell can be well combined with. NET Framework to integrate existing. NET resources. At first, I considered using the CMD batch file. I have also used a batch file to simplify the compilation process. This automatic compilation script is still in use, but anyone who has done CMD batch file processing and UNIX Shell development will know that, the batch processing file of CMD is very difficult to use. Compared with any shell (SH, KSH, CSH, and BASH), it is a large part. So I gave up the idea of using CMD and chose PowerShell.
Install and configure PowerShell
To Download and install PowerShell, Download and install Windows PowerShell 1.0. . NET Framework 2.0 is required.
After the installation is complete, you need to modify the Execution Policy, which is the security setting for executing the script. Because the default Execution Policy of PowerShell is Restricted, Restricted indicates that only one command can be executed and Batch scripts cannot be executed. If you execute a batch script, the following error is prompted.
File C:\Temp\projects\DatetimeSync\DatetimeSync.ps1 cannot be loaded because the execution of scripts is disabled on th
is system. Please see "get-help about_signing" for more details.
At line:1 char:18
+ ./DatetimeSync.ps1 <<<<
Therefore, you must use the Set-ExecutionPolicy RemoteSigned command to modify the Execution Policy. After modification, you can run the batch script.
Use OpenNETCF Desktop Communication
PowerShell does not support direct operation on Windows Embedded CE and Windows Mobile devices. Therefore, you need to use RAPI. If you use. NET to develop RAPI, you can use OpenNETCF Desktop Communication. For more information about OpenNETCF Desktop Communication and RAPI, see
Development of ActiveSync in Windows Embedded CE and Windows Mobile.
Download OpenNETCF Desktop Communication Library and compile OpenNETCF. Desktop. Communication. dll. The source code provided below already contains the DLL, so this step can be omitted.
PowerShell Development
First, create a text file and change the file extension to ps1. For example, my file is DatetimeSync. ps1. Then you can write the script.
Note: Windows does not automatically bind the ps1 file to PowerShell. To execute the ps1 file each time, you need to open PowerShell and run it with the dot (.). This is consistent with the UNIX shell style.
For example:
Only input DatetimeSync. ps1 cannot execute this file, you need to input./DatetimeSync. ps1.
Source code
$dllPath = Get-Location
$dllPath = $dllPath.Path + "\OpenNETCF.Desktop.Communication.dll"
[System.Reflection.Assembly]::LoadFrom($dllPath) |Out-null
$rapi = New-Object OpenNETCF.Desktop.Communication.RAPI
echo "Connecting to device..."
$rapi.Connect()
if ($rapi.Connected)
{
$dt = get-date
$date = $dt.Month.ToString() + "-" + $dt.Day.ToString() + "-" + $dt.Year.ToString()
echo "Setting date [$date]..."
$rapi.CreateProcess("cmd", "/c date " + $date)
$time = $dt.Hour.ToString() + ":" + $dt.Minute.ToString() + ":" + $dt.Second.ToString()
echo "Setting time [$time]..."
$rapi.CreateProcess("cmd", "/c time " + $time)
echo "Finished"
}
else
{
echo "Cannot connect to the device, please check the physical connection."
}
Get-Location and get-date are called cmdlet (read as command-let), which represents a separate FUNCTION command. For example, Get-Location indicates to retrieve the current path, and get-date indicates to retrieve the current date and time. If you need to use. NET assembly, you need to use LoadFrom for reflection loading. In this example, we load OpenNETCF. Desktop. Communication. dll. New-Object indicates the instantiated Object. In this example, the Object RAPI of OpenNETCF. Desktop. Communication. rapi is instantiated. With rapi objects, you can call RAPI class methods to manipulate Windows Mobile and Windows Embedded CE devices. In this example, call the Connect () method to Connect to the device, and then use the CreateProcess () function to call cmd to modify the time and date. You can call the API SetSystemTime to modify the system time. To do this, you need to use C ++ to develop a DLL on the device and deploy it on the device. to simplify the work, I directly used the time and date functions provided by cmd to modify the time.
So far, this function has been implemented. Using PowerShell can complete many other functions to simplify our work, such as managing processes on the device, deploying programs, and starting unit tests. Are you using tools in development to simplify your work? If any, you are welcome to discuss it.
If you think the article is good, click "support". If you think the article is not good, click "disagree". If you can, leave a message to correct it. Your feedback is the motivation for my progress. Thank you.
Reference
"How to" Series: Automating Windows Mobile with Windows PowerShell
Source code:/Files/procoder/DatetimeSync.zip