Share via


__SystemSecurity 類別的 SetSD 方法

SetSD方法會設定使用者所連接之命名空間的安全性描述項。 這個方法需要二進位位元組陣列格式的安全性描述元。 如果您要撰寫腳本,請使用 SetSecurityDescriptor 方法。 如需詳細資訊,請參閱 保護 WMI 命名空間變更安全性安全物件上的存取安全性

如果您是使用 C++ 進行程式設計,您可以使用 SDDL操作二進位安全性描述元,以及 ConvertSecurityDescriptorToStringSecurityDescriptorConvertStringSecurityDescriptorToSecurityDescriptor 的轉換方法。

使用者必須具有 WRITE_DAC 許可權,而且系統管理員預設擁有該許可權。 唯一使用的安全性描述元部分是 ACE (ACE) 的非內嵌存取控制專案, (DACL) 。 藉由在 ACE 中設定 CONTAINER_INHERIT 旗標,安全性描述元會影響子命名空間。 允許和拒絕 ACE 皆允許。

注意

因為 DACL 中同時允許拒絕和允許 ACE,所以 ACE 的順序很重要。 如需詳細資訊,請參閱 DACL 中的 ACE 排序

語法

HRESULT SetSD(
  [in] uint8 SD[]
);

參數

SD [in]

組成安全性描述元的位元組陣列。

傳回值

會傳回 HRESULT ,指出方法呼叫的狀態。 針對腳本和 Visual Basic 應用程式,可以從 OutParameters.ReturnValue取得結果。 如需詳細資訊,請參閱 建構 InParameters 物件和剖析 OutParameters 物件

下列清單列出 SetSD很重要的傳回值。

S_OK

已成功執行方法。

WBEM_E_ACCESS_DENIED

呼叫端沒有足夠的許可權可呼叫這個方法。

WBEM_E_METHOD_DISABLED

嘗試在不支援此方法的作業系統上執行此方法。

WBEM_E_INVALID_OBJECT

SD 不會通過基本有效性測試。

WBEM_E_INVALID_PARAMETER

SD 無效,因為下列其中一項:

  • 缺少 DACL。
  • DACL 無效。
  • ACE 已設定 WBEM_FULL_WRITE_REP 旗標,且 未設定WBEM_PARTIAL_WRITE_REPWBEM_WRITE_PROVIDER 旗標。
  • ACE 已設定 沒有 CONTAINER_INHERIT_ACE旗標 的INHERIT_ONLY_ACE 旗標。
  • ACE 具有未知的存取位集。
  • ACE 具有不在資料表中的旗標集。
  • ACE 的類型不在資料表中。
  • SD 中遺漏擁有者和群組。

如需有關 ACE) 旗標 (存取控制專案的詳細資訊,請參閱 WMI 安全性常數

備註

如需以程式設計方式或手動方式修改命名空間安全性的詳細資訊,請參閱 保護 WMI 命名空間

範例

下列腳本示範如何使用 SetSD 來設定根命名空間的命名空間安全性描述元,並將它變更為 strSD中顯示的位元組陣列。

' Hard-coded security descriptor
strSD = array( 1, 0, 4,129,72, 0, 0, 0, _ 
              88, 0, 0,  0, 0, 0, 0, 0, _
              20, 0, 0,  0, 2, 0,52, 0, _
               2, 0, 0,  0, 0, 2,24, 0, _
              63, 0, 6,  0, 1, 2, 0, 0, _
               0, 0, 0,  5,32, 0, 0, 0, _
              32, 2, 0,  0, 0, 2,20, 0, _
              63, 0, 6,  0, 1, 1, 0, 0, _
               0, 0, 0,  1, 0, 0, 0, 0, _
               1, 2, 0,  0, 0, 0, 0, 5, _
              32, 0, 0,  0,32, 2, 0, 0, _
               1, 2, 0,  0, 0, 0, 0, 5, _
              32, 0, 0,  0,32, 2, 0, 0)

' Connect to WMI and the root namespace.
Set oSvc = CreateObject( _
                         "WbemScripting.SWbemLocator"). _
                         ConnectServer(,"Root\Cimv2")

' Get the single __SystemSecurity object in this namespace.
Set oSecurity = oSvc.Get("__SystemSecurity=@")

' Change the namespace security.
nReturn = oSecurity.SetSD(strSD)
WScript.Echo "ReturnValue " & nReturn

下列 C# 程式碼範例會使用 System.Security.AccessControl.RawSecurityDescriptor 列舉、插入和移除 RawSecurityDescriptor.DiscretionaryAcl 中的新 CommonAce 物件,然後將它轉換回位元組陣列,以透過 SetSD 儲存它。 您可以使用 NTAccount 和 Translate 來擷取 SecurityIdentifier。

 byte[] sdValueByteArray = new Byte[0];

            string accountName = "My User or Group";

            AceFlags aceFlags = AceFlags.ContainerInherit;

            int accessRights = 131107; // Search for Namespace Access Rights Constants and build an Flags enum

            RawSecurityDescriptor rawSecurityDescriptor = new RawSecurityDescriptor(sdValueByteArray, 0);

            NTAccount ntAccount = new NTAccount(accountName);

            IdentityReference identityReference = ntAccount.Translate(typeof(SecurityIdentifier));

            if (identityReference == null)

            {

                string message = string.Format("The IdentityReference of NTAccount '{0}' is null.", accountName);

                throw new Exception(message);

            }

            SecurityIdentifier securityIdentifier = identityReference as SecurityIdentifier;

            if (securityIdentifier == null)

            {

                string message = "The IdentityReference of NTAccount '{0}' is not an SecurityIdentifier.";

                throw new Exception(message);

            }

            CommonAce commonAce;

            foreach (GenericAce genericAce in rawSecurityDescriptor.DiscretionaryAcl)

            {

                commonAce = genericAce as CommonAce;

                if (commonAce == null)

                {

                    continue;

                }

                if (commonAce.SecurityIdentifier.Value.Equals(securityIdentifier.Value, StringComparison.OrdinalIgnoreCase))

                {

                    return;

                }

            }

            commonAce = new CommonAce(aceFlags, AceQualifier.AccessAllowed, (int)accessRights, securityIdentifier, false, null);

            rawSecurityDescriptor.DiscretionaryAcl.InsertAce(rawSecurityDescriptor.DiscretionaryAcl.Count, commonAce);

規格需求

需求
最低支援的用戶端
Windows Vista
最低支援的伺服器
Windows Server 2008
命名空間
所有 WMI 命名空間

另請參閱

WMI 系統類別

__SystemSecurity

__SystemSecurity::GetSD

WMI 安全性常數

Win32_ACE

Win32_SecurityDescriptor

保護 WMI 命名空間