1

我正在尝试编写一些 C# 代码以编程方式读/写 Windows 8 中的家庭安全控件,特别是 Web 和应用程序筛选器列表。

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-BOX

这个问题提到了复制现有对象,但我还没有找到使用通用或这些特定 WMI 对象的方法。

如何插入或删除条目?感谢您的输入。

4

1 回答 1

2

发布一个可行的(如果不是理想的)完整的答案。这些片段特别适用于我的用例,但希望它们可以帮助某人

  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();
  }
于 2012-10-12T18:12:28.080 回答