C# 修改现有任务序列的安全范围

C# Modify Security Scopes on existing Task Sequence

我可以修改 SMS_TaskSequencePackage 中的所有属性,除了 SecuredScopeNames

public void setSecurityScopes(WqlConnectionManager connection, string packageID, string newSecurityScopes)
{
    try
    {
        // Take the new security scopes (Security Scopes are stored in strings' array)
        string[] newScopes = { newSecurityScopes };

        // Get the instance with WQLConnectionManager
        IResultObject securityScopesToChange = connection.GetInstance(@"SMS_TaskSequencePackage.PackageID='" + packageID + "'");
        securityScopesToChange["SecuredScopeNames"].StringArrayValue = newScopes;


        // Apply the new Security Scopes
        securityScopesToChange.Put();

    }
    catch (SmsException ex)
    {
        MessageBox.Show("Failed to set TS Security Scopes. Error: " + ex.Message);

    }

}  

我不明白为什么我的 StringArray 没有被存储。 使用这种方法,我可以更改字符串格式的其他属性,但不能更改此属性。 谢谢你的帮助。

属性 SecuredScopeNames 似乎是一个只读参数(as can be seen in the documentation for SMS_Package - 尽管它似乎有点过时)必须通过附加 class SMS_SecuredCategoryMembership.

您可以像这样添加范围(查看更多详细信息here):

    // Create a new instance of the scope assignment.
    IResultObject assignment = sccmConnection.CreateInstance("SMS_SecuredCategoryMembership");

    // Configure the assignment
    assignment.Properties["CategoryID"].StringValue = scopeId; // CategoryID from SMS_SecuredCategory
    assignment.Properties["ObjectKey"].StringValue = objectKey; // PackageID
    assignment.Properties["ObjectTypeID"].IntegerValue = objectTypeId; // can get it from SMS_ObjectName with objectkey (probably fixed values)

    // Commit the assignment
    assignment.Put();

CategoryID或ScopeId可以取自SMS_SecuredCategory ObjectKey 是您的包或 TaskSequence 包的包 ID 对于 TaskSequencePackages,ObjectTypeId 可能始终为 20,但可以使用 PackageID 作为 ObjectKey 从 SMS_ObjectName 查询(如果不使用 where 子句完成,查询非常慢,因为它具有存储在 sccm 数据库中的所有类型的所有对象)

然而,这对于真正的修改是不够的,因为它会保留所有现有的范围,所以如果你想摆脱默认(或另一个),你还必须调用删除(更多细节here):

    // Find the existing scope assignement that matches our parameters.
    IResultObject assignment = sccmConnection.GetInstance("SMS_SecuredCategoryMembership.CategoryID='" + scopeId + "',ObjectKey='" + objectKey + "',ObjectTypeID=" + objectTypeId.ToString());

    // Make sure we found the scope.
    if(assignment == null)
        throw new Exception("Unable to find matching scope, object, and object type.");
    else
        assignment.Delete();

使用相同的三个参数(默认似乎具有保留的 scopeID SMS00UNA,但最好还是从 SMS_SecuredCategory 获取详细信息)。