Search code examples
windowspowershellmsmqqueueingmessageid

MSMQ triggered powershell - fires but does nothing for forwarded message


This is on Windows Server 2008 R2 Enterprise (64 bit)

I want to execute a powershell script when a message arrives in a particular queue. It works as expected if I log on to the server and use a utility to move/copy a message into the queue.

The rule has no conditions and is a "peek" rule.

The message is actually meant to be sent from another server. When this happens the message arrives and, by looking at the task manager, I can see powershell.exe runs, but the powershell script does not seem to execute.

I've tried simplifying this by just having a test script write to a file in the same directory as the script. Again, this works if I manually move a message into the queue but the results are the same if a message arrives from another server; the script doesn't seem to execute.

The trigger definitely fires, I can see powershell.exe with the expected command line in the windows task manager it just does nothing if the message arrives after being sent from another server.

For parameters I just have the full path of the script as a string parameter for my testing.

I made sure network service had permissions on the queues and the script directory.

There are no errors in the event logs.

I've tried the following which did not produce different results:

  1. Switching to the 32 bit powershell.exe.
  2. Add a condition which is always true.
  3. Switch from network service to a domain account for the message queueing and message queueing triggers services and add the account's permissions to the queues and directories.

So can anyone else think of something to try?

Is there any way to add a program switch to the parameters of a rule? It puts all string parameters in quotes and the exe selection doesn't allow switches.

Is there any way to capture the stderr output on the execution of a trigger? It's probably failing somehow but I can't find a way to see it.

My "test script" contents:

$fileName = "C:\Users\Public\Documents\Scribe\Test\MoveMessage.err";
("tested ok") | Out-File $fileName;

Thanks!

Edit:

I switched to using write-eventlog and that logs messages correctly.

write-eventlog -logname "Windows PowerShell" -source "PowerShell" -eventID 1 -message "TestScript.ps1 Script Started."

Doing this I discovered that any script with the MessageId as a parameter will not run if the message was submitted from another computer. This occurs even if the messageId isn't even used in the script.

This might have to do with the messageId's looking like "73493861-3988-4109-8356-206a1d7792da\25" but I'm not sure why this wouldn't work depending on the source of the message. The messageId does get split into 2 arguments though with the \xx being in the additional argument.


Solution

  • Apparently the main issue is that the messageId can not be used as a trigger rule parameter in some situations. I used the lookupId instead and everything works as expected.