2009-08-20 37 views
6

我正在爲我支持的應用程序配置新的測試服務器。它使用大約35個不同的MSMQ隊列,並且手動創建這些隊列顯然不是很有趣。特別是由於該應用程序的生產版本也在移動服務器,所以我將重新做到這一點。我正在尋找的是創建這些隊列的自動化方法,並且Powershell(基於我對其有限的知識)似乎是要走的路。使用Powershell自動執行MSMQ設置

有沒有人有關於如何完成此任務的提示?

回答

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); 
    } 

} 

你可以這樣調用此cmdlet從Powershell的是這樣的:

PS>Get-MyCmdlet -SomeParam 'whatever you want' 

然後,使用MSMQ,網上有很多關於如何從C#中完成這個任務的示例:

Here's just one of them....

+0

完美。我想我會嘗試這條路線,因爲我還不熟悉Powershell,看起來這將是一個很好的介紹。謝謝! – RubyHaus 2009-08-21 12:38:54

+0

對於這個特定的任務,Cmdlet似乎有點矯枉過正。幾行腳本,沒有理由編譯任何東西或安裝任何東西(您必須使用Cmdlet)。 – 2009-08-21 15:37:00

+0

但Cmdlet應該做什麼? – 2011-08-24 15:47:19

0

沒有理由不能在PowerShell中使用System.Messaging。只需加載正確的程序集,就可以創建/刪除/鉤住你的內容。創建一個自定義的cmdlet當然是一個有趣的選擇,但可能是矯枉過正的取決於你想要什麼(一個簡單的腳本可能只是做伎倆)。

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($_) } 
6

如果您使用的是PowerShell Community Extensions(PSCX),它用於創建和管理MSMQ的cmdlet:

  • 清除,MSMQueue
  • GET-MSMQueue
  • 新MSMQueue
  • Test-MSMQueue