Thursday, October 13, 2011

Using SCCM client center functionality to import local policy

Tired of waiting for collection evaluation and policy updates when advertising programs in SCCM? There is a function in SCCM client center that imports local machine policies, but it requires that an elevated user runs the program and there is not batch functionality.

By referencing the smsclictr.automation.dll this functionality can be reused to build workflow activities/batch programs that calls the same logic as SCCM client center uses. Example code:


SMSClient client = new SMSClient(computerName);
client.Connection.Connect();
if (client.Connection.mScope.IsConnected)
{
//Connect to the clients managementPoint SQL server
SqlConnectionStringBuilder sqlConnectionBuilder = new SqlConnectionStringBuilder("Integrated Security=True");
sqlConnectionBuilder.DataSource = client.ManagementPoint;
sqlConnectionBuilder.InitialCatalog = "SMS_" + client.SiteCode;

SqlConnection sqlConnection = new SqlConnection(sqlConnectionBuilder.ToString());
try
{
sqlConnection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT PolicyID, Body FROM Policy WHERE (PolicyID LIKE '%" + advertisementID + "%') ORDER BY PolicyID";
command.Connection = sqlConnection;

SqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
byte[] policyBody = dataReader[1] as byte[];
client.SoftwareDistribution.ImportSCCMPolicy(policyBody, false);
Logging.Log("Policy imported on computer '" + computerName + "' (AdvertisementID: " + advertisementID + ")", System.Diagnostics.EventLogEntryType.Information);
break;
}
sqlConnection.Close();
}
catch (Exception ex)
{
Logging.Log("Failed to get policy from SQL for advertisement '" + advertisementID + "'. Error: " + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
}
}

6 comments:

  1. Very nice, it's very usefull! May I ask You, how could I use the SCCM Client center's dll to start remote processes?

    Regards,
    Csontikka

    ReplyDelete
    Replies
    1. Hi Csontikka!

      I haven't tried to do that through the SCCM Client Center dll. Have you looked at psexec (sysinternals) for starting remote processes?

      /Tomas

      Delete
  2. Unfortunatelly all the incoming ports are closed expect of the port of the SCCM. This is why bexec and psexec are not options for me :(

    ReplyDelete
  3. Great job, nice one it is very beneficial for regulars, i also take many more decision about SCCM @ sccm client center

    ReplyDelete
  4. is it possible to import a local policy for a software deployment with Configuration Manager Current Branch

    ReplyDelete
  5. I mean a import a local policy related to an existing software update deployment

    ReplyDelete