none
System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.InvokeWmiMethod RRS feed

  • Question

  • Hi,

    I am developing a WMI Provider using C# .NET framework. The provider has two WMI methods (see below). After installing the WMI Schema using InstallUtil.exe, and creating and publishing several WMI Objects (I can see them using wbemtest.exe) When I execute a method in one of these WMI objects I get an "System.UnauthorizedAccessException: Acceso denegado. (Excepción de HRESULT: 0x80070005 (E_ACCESSDENIED))" exception.

    Resuming: I can see the objects, I can see the properties and its values, and I can see the methods they have, but I can't execute those methods.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management.Instrumentation;
    using System.Text;

    [assembly: Instrumented(@"root\canales")]
    [assembly: WmiConfiguration(@"root\canales", IdentifyLevel = true, HostingModel = ManagementHostingModel.Decoupled)]

    namespace myNamespace
    {
        [InstrumentationClass(InstrumentationType.Abstract)]
        [ManagedName("Management")]
        public class WMIManagement
        {
        }

        public interface IWMIApplicationServer
        {
            string FullUri { get; }
            int TotalHits { get; }
            int MaxTime { get; }
            int MinTime { get; }
            int AverageTime { get; }
            int TotalTime { get; }
            int LastTime { get; }
            string TimeStamp { get; }
            void Dump();
            void Clear();
        }

        [ManagementEntity()]
        [ManagedName("WMIApplicationServer")]
        public class WMIApplicationServer : WMIManagement, IWMIApplicationServer
        {
            private Contador contador;

            public WMIApplicationServer(Contador cont)
            {
                this.contador = cont;
                InstrumentationManager.Publish(this);
            }

            [ManagementKey]
            public string FullUri
            {
                get { return contador.FullUri; }
            }

            [ManagementProbe]
            public int TotalHits
            {
                get { return contador.TotalHits; }
            }

            [ManagementProbe]
            public int MaxTime
            {
                get { return contador.MaxTime; }
            }

            [ManagementProbe]
            public int MinTime
            {
                get { return contador.MinTime; }
            }

            [ManagementProbe]
            public int AverageTime
            {
                get { return contador.AverageTime; }
            }

            [ManagementProbe]
            public int TotalTime
            {
                get { return contador.TotalTime; }
            }

            [ManagementProbe]
            public int LastTime
            {
                get { return contador.LastTime; }
            }

            [ManagementProbe]
            public String TimeStamp
            {
                get { return contador.TimeStamp; }
            }

            [ManagementTask]
            public void Clear()
            {
                contador.Clear();
            }

            [ManagementTask]
            public void Dump()
            {
                contador.Dump();
            }
        }

        [RunInstaller(true)]
        public class MyInstaller : DefaultManagementInstaller { }
    }

    To execute the method, I developed a C# client and I get the 0x80070005 error, but I get the same exception running from powershell:

    PS > Get-WmiObject -Namespace root\canales -Class WMIApplicationServer | ForEach-Object { Invoke-WmiMethod -Path $_.__Path -Name Clear }

    Invoke-WmiMethod :
    At line:1 char:106
    + Get-WmiObject -Namespace root\canales -Class WMIApplicationServer | ForEach-Object { Invoke-WmiMethod <<<<  -Path
     $_.__Path -Name Clear }
        + CategoryInfo          : NotSpecified: (:) [Invoke-WmiMethod], UnauthorizedAccessException
        + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.InvokeWmiMethod

    I tried with admin privileges (always local access, that means, the client and the WMI instances are in the same machine) in the client still get this errors. I read several blogs, documentation and pages, but I couldn't find any good documentation about WMI security system, that helped me to solve this issue.  I find WMI and the Microsoft security framework as a very dark thing. :S

    Any help about this will be appreciated,

    thanks,

    Simon




    Tuesday, December 10, 2013 4:08 PM