Change users parental control settings using WMI in c#

前端 未结 1 1427
离开以前
离开以前 2021-01-06 10:50

I am really new to WMI and COM.

I want to change some parameters to user accounts parental controls and the only API availble is WMI. The WMI provider class to use i

相关标签:
1条回答
  • 2021-01-06 11:08

    The WpcUserSettings wmi class which exist in the root\CIMV2\Applications\WindowsParentalControls namespace does not expose any method to update the data by user, but all properties exposed are read/write except obviously the SID property. you can iterate over the properties for a particular user and change the values.

    So you can make a Wmi query using a sentence like to retrieve all the users SELECT * FROM WpcUserSettings

    or this sentence to modify the properties of a particular user

    SELECT * FROM WpcUserSettings Where SID="the SID of the user to modify"

    then update the values of the properties which you want modify and finally call the Put method to set the new values.

    check this sample app.

    using System;
    using System.Collections.Generic;
    using System.Management;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2\\Applications\\WindowsParentalControls", "SELECT * FROM WpcUserSettings");
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        if (queryObj["SID"] == "The user SID to modify")
                        {
                            //set  the properties here
    
                            queryObj["AppRestrictions"] = true;
                            queryObj["HourlyRestrictions"] = true;
                            queryObj["LoggingRequired"] = false;
                            //queryObj["LogonHours"] = ;
                            //queryObj["OverrideRequests"] = ;
                            queryObj["WpcEnabled"] = true;
                            queryObj.Put();
                        }
                    }
                }
                catch (ManagementException e)
                {
                    Console.WriteLine("An error occurred setting the WMI data: " + e.Message);
                }
                Console.ReadKey();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题