Change users parental control settings using WMI i

2019-02-14 07:35发布

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 is WpcUserSettings.

I don't understand how to modify the parameters for each user. Do i have to create a ManagmentObject of this class per user or are they already instanciated for each user.

If some one could give me an example code for one user it would really help.

Thanx !

EDIT: Hi again. I used your example to get user account names and SIDS. However when i search WpcUserSettings there is no result, i used WMI Studio to check this class and there is no instance so i can't read or set attributes. Have you got an idea how to fix this ?

FIX: Ok i found the trick. You have to call the method AddUser(SID) of WpcSystemSettings using ManagementObject.InvokeMethod() for each user you want to add into parental control mangement. Then you can enable parental control in WpcUserSettings and do whatever you want.

1条回答
一夜七次
2楼-- · 2019-02-14 08:16

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();
        }
    }
}
查看更多
登录 后发表回答