Purge MSMQ queue and reset IIS from a bat file
Asked Answered
R

3

8

Is it possible to purge a msmq queue from a bat file?

Essentially I want to make a bat file or at least something quick and easy so that an untrained employee can click and fix without knowing any shell or admin tools

Could someone please help me in the right direction?

Regen answered 3/8, 2012 at 9:57 Comment(0)
C
13

Take a look on MSMQAdm Utility

Tasks administered through the utility programs include the following:

  • Browsing local queues
  • Purging messages
  • Deleting individual messages
  • Stopping and starting MSMQ service
  • Connecting and disconnecting from the network

Don't forget about powershell, take a look on PowerShell Community Extensions

Update

Open powershell and write line by line

[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$queueName = '.\Private$\testQueue'
$queue = new-object -TypeName System.Messaging.MessageQueue -ArgumentList $queueName
$queue.Purge()

Call powershell from cmd

  1. Create txt file.
  2. Insert all lines
  3. Change file extension on "ps1"

The easiest way call script from cmd.

powershell.exe -executionpolicy Unrestricted C:\purgemsmq.ps1

Carnauba answered 3/8, 2012 at 10:6 Comment(5)
Okay, I will take a look. But essentially I want to make a bat file or at least something quick and easy so that an untrained employee can click and fix without knowing any shell or admin tools.Regen
Msmq have no command line utilities. See https://mcmap.net/q/1330903/-msmq-command-line-in-win-7Carnauba
added powershell commands, just open powershell and insert all line by lineCarnauba
Thank you! Can this in any way be automated?Regen
Lets do it. 1) Create txt file. 2) Insert all lines 3) Change file extension on "ps1". 4) The easiest way call script from cmd. powershell.exe -executionpolicy Unrestricted C:\purgemsmq.ps1Carnauba
C
4

THis Code Works:

[Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null 
$Name=(get-wmiobject win32_computersystem).name
$QName=(
"FormatName:Direct=OS:$name\System$;DEADXACT",
"FormatName:Direct=OS:$name\System$;DEADLETTER"
)

foreach ($Q in $Qname){
$MessageQueue = New-Object System.Messaging.MessageQueue($Q)
$MSGCount=$($MessageQueue.GetMessageEnumerator2()).count

IF($MSGCount){
$MessageQueue.Purge()
Write-Host "$Q has been purged of $MSGCount messages." -ForegroundColor green
}
Else{
Write-Host "$Q is clean"}

} 
Cecilia answered 17/3, 2014 at 19:32 Comment(1)
Along with code please try to explain about that as well.Leathers
B
1

PowerShell script to purge all private queues on local machine:

[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$MachineName=(get-wmiobject win32_computersystem).name
[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("$MachineName") | % { $_.Purge(); }

As explained in https://mcmap.net/q/1264612/-purge-msmq-queue-and-reset-iis-from-a-bat-file, easiest way to execute it is:

  • save script to file purge-private-msmq-queues.ps1
  • in the same folder create script file purge-private-msmq-queues.cmd with following content:

    powershell -executionpolicy Unrestricted .\purge-private-msmq-queues.ps1
    
Beichner answered 14/10, 2014 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.