我正在尝试编写一些C#代码,以便以编程方式读/写Windows8中的家庭安全设置控件,特别是网络和应用程序筛选器列表。
WMI可以使用如下命令公开这些值:
PS C:\Windows\system32> Get-WmiObject -Class WpcURLOverride -Namespace root/CIMV2/Applications/WindowsParentalControls
__GENUS : 2
__CLASS : WpcURLOverride
__SUPERCLASS :
__DYNASTY : WpcURLOverride
__RELPATH : WpcURLOverride.SID="S-1-5-21-4241459202-2635765079-3956675256-1002",URL="http://block.com"
__PROPERTY_COUNT : 3
__DERIVATION : {}
__SERVER : TEST-BOX
__NAMESPACE : root\CIMV2\Applications\WindowsParentalControls
__PATH : \\TEST-BOX\root\CIMV2\Applications\WindowsParentalControls:WpcURLOverride.SID="S-1-5-21-4241459202-2
635765079-3956675256-1002",URL="http://block.com"
Allowed : 2
SID : S-1-5-21-4241459202-2635765079-3956675256-1002
URL : http://block.com
PSComputerName : TEST-BOX
__GENUS : 2
__CLASS : WpcURLOverride
__SUPERCLASS :
__DYNASTY : WpcURLOverride
__RELPATH : WpcURLOverride.SID="S-1-5-21-4241459202-2635765079-3956675256-1002",URL="http://allow.com"
__PROPERTY_COUNT : 3
__DERIVATION : {}
__SERVER : TEST-BOX
__NAMESPACE : root\CIMV2\Applications\WindowsParentalControls
__PATH : \\TEST-BOX\root\CIMV2\Applications\WindowsParentalControls:WpcURLOverride.SID="S-1-5-21-4241459202-2
635765079-3956675256-1002",URL="http://allow.com"
Allowed : 1
SID : S-1-5-21-4241459202-2635765079-3956675256-1002
URL : http://allow.com
PSComputerName : TEST-BOXThis question提到了复制现有的对象,但我还没有找到一种方法来实现泛型或这些特定的WMI对象。
如何插入或删除条目?感谢您的意见。
发布于 2012-10-13 02:12:28
张贴一个可行的,如果不是理想的,完整性的答案。这些代码片段适用于我的用例,但希望它们能帮助某些人
private static SecurityIdentifier UserSID(string username)
{
var f = new NTAccount(username);
return (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
}
private string PsCmdAddSite(Uri url, bool allow)
{
return string.Format(
@"$o = ([WMIClass] ""root\CIMV2\Applications\WindowsParentalControls:WpcURLOverride"").CreateInstance()
$o.Allowed = {0}
$o.URL = ""{1}""
$o.SID = ""{2}""
$o.Put()", (allow ? "1" : "2"), url, UserSID('TargetUsername'));
}
private static string PsCmdRemoveAllSites()
{
return
@"$allSites = Get-WmiObject -Class WpcURLOverride -Namespace root/CIMV2/Applications/WindowsParentalControls;
foreach ($site in $allSites){Remove-WmiObject -InputObject $site;}";
}
private static void RunPsScript( string scriptText )
{
// create Powershell runspace, logically a single PS session
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
// create a pipeline and feed the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
// execute the script
pipeline.Invoke();
runspace.Close();
}https://stackoverflow.com/questions/11679607
复制相似问题