使用Powershell自动进行MSMQ设置

Rub*_*aus 6 c# powershell msmq

我正在为我支持的应用程序配置新的测试服务器.它使用大约35个不同的MSMQ队列,并且手动创建这些队列显然不是很有趣.特别是因为应用程序的生产版本也在移动服务器,所以我将重新做到这一点.我正在寻找的是一种创建这些队列的自动化方法,而Powershell(基于我对它的有限了解)似乎是要走的路.

有没有人有关于我如何实现这一目标的任何提示?

Sco*_*aad 6

也许是这样的.它有点冗长,但它有助于证明PowerShell可以在没有CmdLet的情况下完成此任务.

# Loads the assembly into PowerShell (because it's not a pre-loaded one)
[Reflection.Assembly]::LoadWithPartialName( "System.Messaging" ) | Out-Null

# This is just an array which could also just be a file
$queueList = ( ".\q1", ".\q2", ".\q3", ".\q4" )

# Create the queues by piping the list into the creation function
# $_ refers to the current obect that the ForEach-Object is on
$queueList | ForEach-Object { [System.Messaging.MessageQueue]::Create( $_ ) }
Run Code Online (Sandbox Code Playgroud)


Kei*_*ill 6

如果您使用的是PowerShell社区扩展(PSCX),则它具有用于创建和管理MSMQ的cmdlet:

  • 清除-MSMQueue
  • GET-MSMQueue
  • 新MSMQueue
  • 测试MSMQueue


BFr*_*ree 1

我认为您应该采取的方法是创建您自己的 Powershell cmdlet (Commandlet)。基本上,您从基类继承,重写一个方法,这就是当您从 Powershell 调用该 cmdlet 时调用的方法。这样您就可以在 C# 中执行您需要执行的操作,并且只需从 Powershell 中调用它即可。想象一下这样的事情:

编辑:忘记链接到 MSDN 以创建 cmdlet:http://msdn.microsoft.com/en-us/library/dd878294 (VS.85).aspx

[Cmdlet(VerbsCommunications.Get, "MyCmdlet")]
public class MyCmdlet : Cmdlet
{
    [Parameter(Mandatory=true)]
    public string SomeParam {get; set;}

    protected override void ProcessRecord()
    {
         WriteObject("The param you passed in was: " + SomeParam);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,您可以从 Powershell 中调用此 cmdlet,如下所示:

PS>Get-MyCmdlet -SomeParam 'whatever you want'
Run Code Online (Sandbox Code Playgroud)

然后,要使用 MSMQ,网上有许多关于如何在 C# 中完成此操作的示例:

这只是其中之一......