none
Count Messages in Active Outgoing MSMQ Queues with COM MSMQManagement.MessageCount RRS feed

  • Question

  • I'm trying to list the active outgoing queues for MSMQ with a message count of each queue in powershell. I'm getting the queue list with the COM object MSMQApplication.ActiveQueues and the machine name with MSMQApplication.Machine. That much is working.

    What I want is a simple list with the queue path and message count in CSV for the active outgoing queues.

    I do not understand how to initialize MSMQManagement.Init in powershell. I know it needs (2) of (3) values passed to it when initiated, I'm choosing machine name and queue path.

    Here the script:

    # Create a com object's
    $MyMSMQApp = new-object  -ComObject  MSMQ.MSMQApplication
    $MyMSMQmgt = new-object -ComObject MSMQ.MSMQManagement
    
    # Get My Hostname
    $MyHostName = $MyMSMQApp.Machine
    
    # Get list of all active queues
    $All_Active_Queues = $MyMSMQApp.ActiveQueues
    
    # Get list of outgoing queues
    $OutGoingQueues = $All_Active_Queues | where { $_ -notlike "*$MyHostName*" }
    
    # Iterate through the list of queues and
    # get a message count for each
    foreach ($queue in $OutGoingQueues){
    
      # add prefix "FormatName:" to the path
      # (I'm not sure if this is needed)
      $queue = "FormatName:$queue"
    
      # HERE IS THE PROBLEM!!!!
      # Initialize object
      $MyMSMQmgt.Init($MyHostName,$null,$queue)
    
      # Get the Count
      $count = $MyMSMQmgt.MessageCount
    
      # Send some output
      write-host "$queue,$count"
    
    }

    Here is a sample of the queue list in $OutGoingQueues:

    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-salesdata-3
    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-salesdata-4
    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-salesdata-2
    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-salesdata-1
    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-salesdata-5
    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-forecastadjustment
    FormatName:DIRECT=OS:SERVER-A\private$\Customer-A-forecast
    FormatName:DIRECT=OS:SERVER-B\private$\Customer-A-export-1
    FormatName:DIRECT=OS:SERVER-B\private$\Customer-A-reports
    FormatName:DIRECT=OS:SERVER-B\private$\Customer-A-databasemaintenance-1
    FormatName:DIRECT=OS:SERVER-B\private$\Customer-A-Staging-salesdata-1
    FormatName:DIRECT=OS:SERVER-B\private$\Customer-A-Staging-salesdata-1

    Here is one of the errors:

    An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND))
    At C:\Program Files (x86)\test\test6.ps1:27 char:18
    +   $MyMSMQmgt.Init <<<< ($MyHostName,$null,$queue)
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : COMException
    

    Here are the versions:

    PS C:\Program Files (x86)\test> $host.Version

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    2      0      -1     -1

    MSMQ Version 4.1


    Wednesday, June 3, 2015 2:53 AM

Answers

  • The scope of this forum is to ask specific scripting questions. Keep in mind that the respondents are volunteers and there is no service-level agreement.

    Your problem is that you are using some COM objects with methods that are not visible to .NET (and hence PowerShell). You are going to need to edit the script to call these methods indirectly as noted in my article.

    It's a bit much to ask others to do this for you (we are volunteers, after all). Start small and work your way up.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 9, 2015 6:57 PM
    Moderator

All replies

  • It is odd that you are creating scripts in a protected 32 bit location. "C:\Program Files (x86)\test\test6.ps1"

    Q-mgr will throw an exception on empty Qs.  Catch the exception and move on.


    \_(ツ)_/

    Wednesday, June 3, 2015 5:52 AM
  • This has  management CmdLets: http://pscx.codeplex.com/


    \_(ツ)_/

    Wednesday, June 3, 2015 5:53 AM
  • jrv That's not actually the directory, I tried to scrub a lot before posting. I understand that I will need to catch the error on empty queues, thanks for the heads-up.

    I would prefer to learn how to initialize MSMQManagement.Init in powershell as opposed to using Codeplex CmdLets. We have a lot of proprietary systems and the more I learn about this the better off we are. I am glad you mentioned Codeplex, I'll dig around their to see what I can learn.

    I'll be sure to post anything I come up with.

    Thanks!

    Wednesday, June 3, 2015 3:12 PM
  • If you are learning then I recommend using C++ to as there are many examples.  THiswill get youfamiliar with the intendedusage.

    Looking at the codeplex source will also be useful.


    \_(ツ)_/

    Wednesday, June 3, 2015 3:34 PM
  • Here is a good learning tool:

    http://www.informit.com/articles/article.aspx?p=131272&seqNum=6

    You can execute this code under VBA in Office Excel.   The debugger is useful.  This avoids issues of COM discovery in PowerShell.


    \_(ツ)_/

    Wednesday, June 3, 2015 3:37 PM
  • jrv I do not want to avoid using powershell. I managed to get these initialized and returning data with powershell:

    MSMQApplication.ActiveQueues

    MSMQApplication.PrivateQueues

    MSMQApplication.BytesInAllQueues

    MSMQApplication.Machine

    Am I running towards a dead-end? I believe I can get the properties for MSMQManagement through powershell once I understand the syntax of initializing MSMQManagement.Init with the machine name and queue path as parameters.

    The informit link is nice though, thanks for that.

    Wednesday, June 3, 2015 4:22 PM
  • Here is an update. I put the catch in but not specifying the empty folder error yet (because I'm not sure what it is). I'm thinking that MSMQ.MSMQManagement.Init (aka $mgmt.Init) needs to reference objects instead of variables so I'm attempting to create those.

    # Initialize COM Object
    $MyMSMQApp = new-object  -ComObject  MSMQ.MSMQApplication
    
    # Get My Hostname
    $MyHostName = $MyMSMQApp.Machine
    
    # Get list of all active queues
    $All_Active_Queues = $MyMSMQApp.ActiveQueues
    
    # Get list of outgoing queues
    $OutGoingQueues = $All_Active_Queues | where { $_ -notlike "*$MyHostName*" }
    
    Function GetMessageCount
    {
        try
        {
    
            # Initialize COM object
            $mgmt = new-object -ComObject MSMQ.MSMQManagement
    
            # Create Objects
            $MachineObject = New-Object System.Object
            $MachineObject | Add-Member -MemberType NoteProperty -Name Machine -Value "$MyHostName"
            $FormatNameObject = New-Object System.Object
            $FormatNameObject | Add-Member -MemberType NoteProperty -Name FormatName -Value "$queue"
            
            # HERE IS THE PROBLEM!!!
            $mgmt.Init($MachineObject.Machine,$FormatNameObject.FormatName)
    
        }
        catch
        {
            "ERROR $_"
        }
    
    }
    
    foreach ($queue in $OutGoingQueues){
    
      # add prefix "FormatName:" to the path
      $queue = "FormatName:$queue"
      
      GetMessageCount
      
    }

    Here is errors:

    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    ERROR An unhandled COM interop exception occurred: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEME
    NTNOTFOUND))
    

    I verified from within the function that $MachineObject.Machine $FormatNameObject.FormatName have info.

    The problem is MSMQ.MSMQManagement.Init (aka $mgmt.Init)

    Tuesday, June 9, 2015 4:03 PM
  • Many COM objects do not have a type library accessible from .NET (PowerShell), so you can't just call the methods directly.

    Asking others to fix your script to account for this is beyond the scope of this forum.

    For background on this detail, see this article:

    Windows IT Pro: Translating Active Directory Object Names Between Formats


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 9, 2015 4:31 PM
    Moderator
  • Bill_Stewart What is the scope of this forum? Is there a better place for this question?
    Tuesday, June 9, 2015 6:45 PM
  • The scope of this forum is to ask specific scripting questions. Keep in mind that the respondents are volunteers and there is no service-level agreement.

    Your problem is that you are using some COM objects with methods that are not visible to .NET (and hence PowerShell). You are going to need to edit the script to call these methods indirectly as noted in my article.

    It's a bit much to ask others to do this for you (we are volunteers, after all). Start small and work your way up.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 9, 2015 6:57 PM
    Moderator