none
Retrieve List of Websites & Binding Information hosted on windows 2008.2012 server using WMI from C# RRS feed

  • Question

  • Hi,

    Am trying to get the list of Websites & Binding Information hosted on windows 2008,2012 server using WMI from C#.

     ConnectionOptions options = new ConnectionOptions();
                    options.Authentication = AuthenticationLevel.PacketPrivacy;
                    ManagementScope scope = new ManagementScope(@"\\" + machinename.ToString().Trim() + @"\root\WebAdministration", conn);
                    scope.Connect();

                    ManagementPath path = new ManagementPath("Site");

                    ManagementClass Sites = new ManagementClass(scope, path, null);

    How do i get the site name and binding information?

    Regards,
    Raj

    Monday, April 23, 2018 2:56 PM

All replies

  • Use WMI Code Creator to get the code you need.  You can then play around with the WMI objects to narrow down specifically what you need. There is also a newer WMI Explorer on GitHub which could be useful.

    The following MSDN documentation has an example of how to enumerate the applications in IIS and access their name and protocols. From there you can use WMI Code Creator to view the other properties and see their values so you can narrow down what else you'll need to retrieve.

    For sites the Site class has similar data. Name gives you the name and Bindings gives you the binding information. Bindings is an array of BindingElement. BindingElement has the Protocol and BindingInformation properties to provide you the binding details.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 23, 2018 5:42 PM
    Moderator
  • Thanks Michael.

    I tired the above and struck with error on the foreach statement giving "access denied" error.

          ConnectionOptions options = new ConnectionOptions();
                    options.Authentication = AuthenticationLevel.PacketPrivacy;
                    ManagementScope scope = new ManagementScope(@"\\" + machinename.ToString().Trim() + @"\root\WebAdministration", conn);
                    scope.Connect();
                    ObjectQuery obj = new ObjectQuery("SELECT * FROM Site");
                    
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,obj);
                 
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                             MessageBox.Show(queryObj["Name"].ToString());

                    }

    Monday, April 23, 2018 6:33 PM
  • For this to work you'll need to connect to WMI with sufficient privileges (probably admin). In your ConnectionOptions, you'll need to specify the credentials to use for connecting to the remote machine.

    Also note that you'll need to ensure that the remote IIS server is properly configured to allow the user to remotely manage it. Unfortunately the docs on this are more confusing. The above linked docs specify what needs to happen for WMI Provider to be installed but there may be more steps after that. I'd recommend getting the script working locally first and then try a remote machine.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 23, 2018 7:02 PM
    Moderator
  • I am passing administrator user credentials in the connection.Yet am getting the "Access denied" error. 

    Also i tested the WMI script from the remote server using WBEMTest and it works.

    Also i tried changing WMI script code to get the windows services running on the remote server and it works.

    Except the IIS Site data.

    Tuesday, April 24, 2018 6:43 AM
  • Finally it worked.

    ConnectionOptions conn = new ConnectionOptions();
                conn.Username = txtUserName.Text.ToString();
                conn.Password = txtPassword.Text.ToString();
                conn.Impersonation = ImpersonationLevel.Impersonate;
                conn.EnablePrivileges = true;
                conn.Authentication = AuthenticationLevel.PacketPrivacy;
                foreach (string machinename in lstmachine.CheckedItems)
                {

                    ManagementScope scope = new ManagementScope(@"\\" + machinename.ToString().Trim() + @"\root\WebAdministration", conn);
                    scope.Connect();
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM Site"));

                    foreach (ManagementObject queryObj in searcher.Get())
                    {

                        MessageBox.Show(queryObj["Name"].ToString());

                    }


                }

    Tuesday, April 24, 2018 7:31 AM
  • Hello Raj,

    You could mark your reply as answer if the issue has been solved. This can be beneficial to others with the same issues. And if you have any concerns, please do not hesitate to let us know.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 25, 2018 6:53 AM
    Moderator