Skip to content

Instantly share code, notes, and snippets.

@zachbonham
Created June 16, 2011 17:26
Show Gist options
  • Save zachbonham/1029744 to your computer and use it in GitHub Desktop.
Save zachbonham/1029744 to your computer and use it in GitHub Desktop.
Manage Msmq Queues
' http://zachbonham.blogspot.com
'
' use at your own risk. :)
'
' assuming that your default scripting engine is CScript.exe
' cscript //H:CScript
'
'
' Assuming administrator priviledges
'
Option Explicit
const MQ_RECEIVE_ACCESS = 1
const MQ_DENY_NONE = 0
const MQ_NO_TRANSACTION = 0
const MQ_MTS_TRANSACTION = 1
const MQ_SINGLE_MESSAGE = 3
const ForReading = 1
const TristateTrue = -1
const TristateFalse = 0
const g_receiveTimeout = 1000
Main
public sub Main()
dim action
dim queueName
dim isTransactional
dim fileName
dim machineName
if WScript.Arguments.Count < 2 then
WScript.Echo "Usage:"
WScript.Echo " Administers *private* MSMQ queues. Does NOT support remote admin of queues unless using a remoting tool such as psexec.exe."
WScript.Echo ""
WScript.Echo " create queue:"
WScript.Echo " cscript msmq.vbs /action:create /queue:%queuename% [/transactional:true/false]"
WScript.Echo " cscript msmq.vbs /action:create /queue:%queuename%"
WScript.Echo " delete queue:"
WScript.Echo " cscript msmq.vbs /action:delete /queue:%queuename%"
WScript.Echo " purge queue:"
WScript.Echo " cscript msmq.vbs /action:purge /queue:%queuename% [/transactional:true/false]"
WScript.Echo " send message:"
WScript.Echo " cscript msmq.vbs /action:send /file:%filename% /queue:%queuename% [/machine:%machinename%] [/transactional:true/false]"
WScript.Echo ""
Wscript.Echo "/action:create - create a queue"
Wscript.Echo "/action:delete - delete a queue"
Wscript.Echo "/action:purge - delete all messages from a queue"
Wscript.Echo ""
Wscript.Echo "/queue:%queuename% - %queuename% is the name of the queue to create."
Wscript.Echo ""
Wscript.Echo "/transactional:%true/false% - true/false, is the queue a transactional queue. Default is non-transactional."
Wscript.Echo ""
Wscript.Echo "/file:%filename% - %filename% is complete file to the file to send. Currently doesn't support unicode."
Wscript.Echo ""
Wscript.Echo "/machine:%machinename% - %machineName% is the netbios name of the server to send to. Defaults to the local machine."
else
action = WScript.Arguments.Named("action")
queueName = Wscript.Arguments.Named("queue")
isTransactional = WScript.Arguments.Named("transactional")
fileName = WScript.Arguments.Named("file")
machineName = wscript.arguments.Named("machine")
if isTransactional = "" then isTransactional = False
if machineName = "" then machineName = "."
if action = "create" then
CreateQueue queueName, isTransactional
elseif action = "delete" then
DeleteQueue queueName
elseif action = "purge" then
PurgeQueue queueName, isTransactional
elseif action = "send" then
SendMessage fileName, machineName, queueName, isTransactional
else
WScript.Echo "Unknown action: " & action
end if
end if
end sub
public sub CreateQueue(queueName, isTransactional)
WScript.Echo "Creating queue: queueName=" & queueName & ", transactional=" & CBool(isTransactional)
dim queueInfo
set queueInfo = CreateObject("MSMQ.MSMQQueueInfo")
queueInfo.PathName = ".\private$\" & queueName
queueInfo.Label = queueName
queueInfo.Create CBool(isTransactional)
end sub
public sub DeleteQueue(queueName)
WScript.Echo "Deleting queue: queueName=" & queueName
dim queueInfo
set queueInfo = CreateObject("MSMQ.MSMQQueueInfo")
queueInfo.PathName = ".\private$\" & queueName
queueInfo.Delete
end sub
public sub PurgeQueue(queueName, isTransactional)
WScript.Echo "Purging queue: queueName=" & queueName
dim messageCount
dim queueInfo
dim queue
dim transactionLevel
transactionLevel = MQ_NO_TRANSACTION
if isTransactional then transactionLevel = MQ_SINGLE_MESSAGE
messageCount = 0
set queueInfo = CreateObject("MSMQ.MSMQQueueInfo")
queueInfo.PathName = ".\private$\" & queueName
set queue = queueInfo.Open (MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
do while true
dim msg
set msg = queue.Receive(transactionLevel, False, False, g_receiveTimeout, False)
if msg is nothing then
WScript.Echo "Removed " & messageCount & " messages. No more messages."
queue.Close
exit sub
end if
messageCount = messageCount + 1
loop
end sub
'This is mainly used for testing the purge method of this script. It doesn't currently support Unicode, but could.
'
public sub SendMessage(fileName, machineName, queueName, isTransactional)
WScript.Echo "Sending message: fileName=" & fileName & ", machineName=" & machineName & ", queueName=" & queueName & ", isTransactional=" & isTransactional
dim transactionLevel
dim message
dim formatName
dim destinationQueue
set destinationQueue = CreateObject("MSMQ.MSMQDestination")
set message = CreateObject("MSMQ.MSMQMessage")
transactionLevel = MQ_NO_TRANSACTION
if isTransactional then transactionLevel = MQ_SINGLE_MESSAGE
formatName = "DIRECT=OS:" & machineName & "\private$\" & queueName
destinationQueue.FormatName = formatName
message.Body = GetFileContents(filename)
message.Send destinationQueue
end sub
private function GetFileContents(filename)
dim fso
dim file
dim stream
dim contents
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile(filename)
set stream = file.OpenAsTextStream(ForReading, TristateFalse)
contents = stream.ReadAll()
GetFileContents = contents
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment