Back in September, I posted on using Powershell Community Extensions to manage MSMQ queues. Community Extensions are cool in how they provide you with simple comandlets to get and create the queues but they lack advanced features that native System.Messaging.MessageQueue class posesses. Here’s an example of managing MSMQ queues using System.Messaging from Powershell, complete with queue listing, checking for existance, deleting, creating, and setting permissions. This works on Powershell 1.0 and does not require the Community Extensions installed. As always, make sure you have enabled script execution (set-executionpolicy unrestricted). Enjoy!
#thanks to SteveC http://stackoverflow.com/questions/765954/setting-permissions-on-a-msmq-queue-in-script/915127#915127
#thanks to Scott Saad http://stackoverflow.com/questions/1309461/automated-msmq-setup-with-powershell/1312688#1312688
#thanks to dahlbyk http://stackoverflow.com/questions/1048954/equivalent-to-cs-using-keyword-in-powershell/1049010#1049010
echo "Loading System.Messaging..."
[Reflection.Assembly]::LoadWithPartialName( "System.Messaging" )
$msmq = [System.Messaging.MessageQueue]
$queueList = ( "breadline", "milkline", "paycheckline")
echo ""
echo "(Re)creating the queues and setting permissions to Everyone/FullControl:"
foreach($qName in $queueList){
$qName = ".\private$\" + $qName
if($msmq::Exists($qName)){
echo (" " + $qName + " already exists and will be deleted and recreated")
$msmq::Delete($qName)
}
$q = $msmq::Create( $qName )
$q.UseJournalQueue = $TRUE
$q.MaximumJournalSize = 1024 #kilobytes
$q.SetPermissions("Everyone", [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Set)
}
echo "All queues processed."
echo ""
echo "Listing existing private queues:"
echo ""
foreach($q in $msmq::GetPrivateQueuesByMachine(".")){
echo (" " + $q.QueueName)
}
echo ""
echo "Done."
