Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Created October 27, 2018 09:02
Show Gist options
  • Save jcefoli/094df61fe43e0b5b6025a4f1a2e58956 to your computer and use it in GitHub Desktop.
Save jcefoli/094df61fe43e0b5b6025a4f1a2e58956 to your computer and use it in GitHub Desktop.
Create Clustered Private MSMQ Queues And Grant Windows User Permissions
<#
.SYNOPSIS
Create Clustered Private MSMQ Queues And Grant Windows User Permissions
.NOTES
Version: 1.0
Author: jcefoli
Creation Date: 10/27/2018
Run this on the active MSMQ cluster node. It may fail if run on the inactive node, as it will not be able to access the queues
#>
# -----------------------------------------#
# Set These Variables #
# -----------------------------------------#
$clusterName = 'MyMSMQCluster' # MSMQ Cluster Hostname
$userToGrantPermissions = "domain\username" # User to grant full permissions to
# Enter a key/value pair of the queue names to create, and a boolean value: true = transactional; $false = nontransactional
$qNames = @{
"transactionalqueue" = $true;
"nontransactionalqueue" = $false;
}
# -----------------------------------------#
# Load .NET Assembly and Set Variables
[Reflection.Assembly]::LoadWithPArtialName("System.Messaging") | Out-Null
$env:_CLUSTER_NETWORK_NAME_ = $clusterName
# Functions
function New-Queue ([string] $queuepath,[bool] $transactional)
{
if (([System.Messaging.MessageQueue]::Exists($queuepath))){
switch ($transactional) {
$true {"[EXISTS] $queuepath | Transactional"}
$false {"[EXISTS] $queuepath | Nontransactional"}
}
}
else{
[System.Messaging.MessageQueue]::Create($queuepath,$transactional)
switch ($transactional) {
$true {"[CREATE] $queuepath | Transactional"}
$false {"[CREATE] $queuepath | Nontransactional"}
}
}
}
function Set-MsmqPermission ([string] $queuepath,[string] $account, [string] $accessright)
{
if (!([System.Messaging.MessageQueue]::Exists($queuepath))){
throw "$queuepath could not be found. Did you create it first?"
}
$q=New-Object System.Messaging.MessageQueue($queuepath)
$q.SetPermissions($account,[System.Messaging.MessageQueueAccessRights]::$accessright,
[System.Messaging.AccessControlEntryType]::Set)
}
# Loop Through Hashtable, Create Queues, and Set Permissions
foreach ($qName in $qNames.Keys) {
Try {
New-Queue "$clusterName\private$\$qName" $qNames[$qName]
Set-MsmqPermission "$clusterName\private$\$qName" $userToGrantPermissions "FullControl"
Set-MsmqPermission "$clusterName\private$\$qName" "Administrators" "FullControl"
}
Catch {
$_
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment