0

I have a program that will query for the state of BizTalk resources, specifically the ReceiveLocations component. When I run the sample program (obtained from C:\Program Files (x86)\Microsoft BizTalk Server 2013 R2\SDK\Samples\Admin\WMI\Enumerate Receive Locations) I get error

Invalid Namespace

On the line :

ManagementObjectCollection QueryCol = Searcher.Get();

Stack Trace

at System.Management.ThreadDispatch.Start()
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementObjectSearcher.Initialize()
at System.Management.ManagementObjectSearcher.Get()
at Microsoft.Samples.BizTalk.WmiAdmin.EnumerateWMIClasses.Main(String[] args) in 
C:\Users\usename\Desktop\BIZTALK\CSharp\EnumRLs.cs:line 114

This is the full program for querying the BizTalk services :

[STAThread]
    static void Main(string[] args)
    {
        // Display help information 
        if (args.Length > 0)
        {
                if (args[0] == "/?") 
                {
                    ....
                    ....
                }
        }

        try 
        {   
            //Create the WMI search object.
            ManagementObjectSearcher Searcher = new ManagementObjectSearcher();

            // create the scope node so we can set the WMI root node correctly.
            ManagementScope Scope = new ManagementScope("root\\"+server+"");
            Searcher.Scope = Scope;
        
            // Build a Query to enumerate the MSBTS_ReceiveLocation instances if an argument
            // is supplied use it to select only the matching RL.
            SelectQuery Query = new SelectQuery(); 
            if (args.Length == 0) 
                Query.QueryString="SELECT * FROM MSBTS_ReceiveLocation";
            else
                Query.QueryString="SELECT * FROM MSBTS_ReceiveLocation WHERE Name = '" + args[0] + "'";

            // Set the query for the searcher.
            Searcher.Query = Query;

            // Execute the query and determine if any results were obtained.
            ManagementObjectCollection QueryCol = Searcher.Get();

            // Use a bool to tell if we enter the for loop
            // below because Count property is not supported
            bool ReceiveLocationFound = false;

            // Enumerate all properties.
            foreach (ManagementBaseObject envVar in QueryCol)
            {
                // There is at least one Receive Location
                ReceiveLocationFound = true;

                Console.WriteLine("**************************************************");
                Console.WriteLine("Receive Location:  {0}", envVar["Name"]);
                Console.WriteLine("**************************************************");

                PropertyDataCollection envVarProperties = envVar.Properties;
                Console.WriteLine("Output in the form of: Property: {Value}");
            
                foreach (PropertyData envVarProperty in envVarProperties) 
                {                   
                    Console.WriteLine(envVarProperty.Name+ ":\t" + envVarProperty.Value);
                }
            }
                
            if (!ReceiveLocationFound) 
            {
                Console.WriteLine("No receive locations found matching the specified name.");
            } 
        }

        catch(Exception excep)
        {
            Console.WriteLine(excep.ToString());
        }
        
        Console.WriteLine("\r\n\r\nPress Enter to continue...");
        Console.Read();
        }

I have tried the code in a separate MVC application where WMI queries are working but i get the same error. The WMI queries in the MVC application are however for querying remote server disk statistics and not BizTalk Server services. I have stepped through the program and the Query object is showing this : https://drive.google.com/file/d/1natMNMkHP2KVUzt60sZSRjU3ecthSPox/view?usp=sharing

What am I missing?

Is there anything specific I need to configure to query BizTalk resources beyond the System.Management namespace or its an issue with the sample code itself?

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Golide
  • 835
  • 3
  • 13
  • 36
  • In your example, where is the variable server set? The namespace for BizTalk (as documented by microsoft looks like "root\\MicrosoftBizTalkServer" can you try changing your scope to that. Also, can you check to see if those namespaces and classes are registered on the machine you are running this code on. Those aren't standard classes so they most likely get installed with BizTalk so if its not a code issue, either they aren't installed or they aren't registered. – Paul G Jun 19 '20 at 00:35
  • @PaulG Variable server is the name of the BizTalk server and im retrieving it from a database table – Golide Jun 22 '20 at 06:54
  • 2
    Ahhh gotcha, flip that. Server should go before the root for example "\\\\machinename\\ROOT\\cimv2" so in your example, try... "\\\\" + server + "\\root\\MicrosoftBizTalkServer". Source the docs here https://learn.microsoft.com/en-us/dotnet/api/system.management.managementscope?view=dotnet-plat-ext-3.1 – Paul G Jun 23 '20 at 00:25
  • @PaulG You should post that as the answer as that is correct. Although you will also have to impersonate the user as per https://learn.microsoft.com/en-us/windows/win32/wmisdk/connecting-to-wmi-remotely-with-c- – Dijkgraaf Jul 13 '20 at 23:52

0 Answers0