How to stop a Message Agent with powershell

Sometimes you need to stop and start a particular message agent without affecting the other message agents running on the BlackBerry Enterprise Server (BES).

Last time I wrote about how to do this manually, this time I will show how you can do this remotely from a management workstation/server in the same Active Directory domain as the server.

First connect with Enter-PSSession to the BES hosting the message agent. In the example I will be connecting to server bes01.

PS C:\Users\remy> Enter-PSSession bes01

After you have connected to the server you will see the prompt change to include the servername between brackets.

[bes01]: PS C:\Users\remy\Documents>

In this case I have landed in my Documents folder on server bes01. For brevity I will leave out the prompt from the next listings.

Next step is to find the process id (PID) of the message agent to be stopped. To find the PID search the message agent’s most recent log for the string “Process id:” In this example I use Get-Content to read the log and pipe it through to Select-String.

Get-Content D:\Logs\BES\20120630\BES01_MAGT_01_20120630_0001.txt  | Select-String “Process id:”

The result will be something like this:

[30000] (06/30 12:47:17.899):{0x2074} [ENV] Current Process id: 4876

Now we have found the process id of the message agent let’s have a look at the process with Get-Process just to check it really is a BlackBerry Message Agent process.

Get-Process -pid 4876

The result will be something like this:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
——-  ——    —–      —– —–   ——     — ———–
579      50    15172      27724   173    74,86   4876 BlackBerryAgent

Stop the process with Stop-Process:

Stop-Process -id 4876

When you use Stop-Process like this you will get asked for confirmation:

Confirm
Are you sure you want to perform the Stop-Process operation on the following item: BlackBerryAgent(4876)?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is “Y”): y

After the confirmation the agent’s process gets stopped. The BlackBerry controller should pick this up and start a new process for the message-agent. Check for the creation of a new _MAGT logfile to be sure.