Search code examples
c#wmiconfigurationmanagersccmwql

C# Modify Security Scopes on existing Task Sequence


I can modify all properties in SMS_TaskSequencePackage except 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);

    }

}  

I don't understand why my StringArray isn't stored. With this method, I can change others properties in string format but not this one. Thanks to help me.


Solution

  • The property SecuredScopeNames seems to be a read only parameter (as can be seen in the documentation for SMS_Package - although it seems to be a bit outdated) that has to be modified via an additonal class SMS_SecuredCategoryMembership.

    You can add a scope like this (see more details 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();
    

    The CategoryID or ScopeId can be taken from SMS_SecuredCategory The ObjectKey is is the Package Id of your Package or TaskSequence Package The ObjectTypeId is probably always 20 for TaskSequencePackages but can be queried from SMS_ObjectName with the PackageID as ObjectKey (very sloq query if done without where clause because it has all objects of all types that are stored in the sccm db)

    This is however not enough for a real modification because it will keep all existing scopes so if you want to get rid of default (or another one) you will also have to call a remove (more detail 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();
    

    With the same three parameters (Default seems to have the reserved scopeID SMS00UNA but it is probably still best to get the details from SMS_SecuredCategory).