65.9K
CodeProject 正在变化。 阅读更多。
Home

PowerShell: 创建 MSMQ

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2014年10月29日

CPOL
viewsIcon

20975

PowerShell: 创建 MSMQ

在工作中,我经常使用 MSMQ,最近也经常使用 NServiceBus,幸运的是,它负责创建所有需要的队列。但是,当您确实需要创建很多队列时,这可能是一项耗时的任务,所以我决定看看我新学的 PowerShell 技能是否可以自动化这个过程(请记住我仍在学习,所以这可能不是最好的/最当前的方式,事实上我知道对于 Windows 8.1/Windows Server,有一个更新更强大的 API。但目前,这就是我所想到的

[CmdletBinding()]
Param(
   [Parameter(Mandatory=$True,Position=1)]
   [string]$queueName,
 
   [Parameter(Mandatory=$True,Position=2)]
   [bool]$isTransactional,
 
   [Parameter(Mandatory=$True,Position=3)]
   [bool]$isJournalEnabled,
 
   [Parameter(Mandatory=$True,Position=4)]
   [string]$userName,
 
   [Parameter(Mandatory=$True,Position=5)]
   [bool]$isAdminUser
)
 
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
 
function printUsage() 
{
    Write-Host "Usage is CreateQueue.ps1 -queueName SomeQueuName 
        -isTransactional $true -isJournalEnabled $true 
        -userName mclocal\barbers -isAdminUser $true"
} 
 
try {
 
    $fullQueueName = ".\private$\" + $queueName
 
 
    If ([System.Messaging.MessageQueue]::Exists($fullQueueName))
    {
        Write-Host($fullQueueName + " queue already exists")
    }
    else
    {
        $newQ = [System.Messaging.MessageQueue]::Create($fullQueueName, 
            $isTransactional)
        if ($isJournalEnabled)
        { 
            $newQ.UseJournalQueue = $True
        }         
        
        if ($isAdminUser)
        { 
            Write-Host("ADMIN")
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::FullControl, 
                    [System.Messaging.AccessControlEntryType]::Allow)        
        }
        else
        { 
            Write-Host("NOT ADMIN")
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::GenericWrite, 
                [System.Messaging.AccessControlEntryType]::Allow)
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::PeekMessage, 
                [System.Messaging.AccessControlEntryType]::Allow)
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::ReceiveJournalMessage, 
                [System.Messaging.AccessControlEntryType]::Allow)
        }
    }
}
catch [Exception] {
   Write-Host $_.Exception.ToString()
  printUsage
}

这将允许您创建您选择名称的私有队列,您还可以选择以下选项:

  • 是否为事务队列
  • 是否启用日志记录
  • 是否为管理员用户队列

希望对您有所帮助!

© . All rights reserved.