Modification of SMSAutomation DLL from the SMS 2003 SDK 3.1 to prevent duplicate items with the same name
I have gotten back into scripting and programming for SMS again. This time in C#
Programmatic Problem: SMS has a restriction that requires a unique name in the MMC. However, programmatically you can create as many items as you want because they are based on a unique instance ID (eg. CollectionID)
The following Code Fragment is from the Collections.cs. Modifying the Constructor. I put a try catch block in to see if the exact name already exists. If it does exist, return nothing. If it doesn't continue with the constructor. This works great with all constructors, Packages, Collections, Programs, Advertisements, etc.
// **********************************************************************
// Constructor - Create a new collection
// **********************************************************************
internal SMSCollection(SMSProvider oProvider, string sCollectionName)
{
// Create a new SMS_Collection instance
m_oProvider = oProvider;
try //Does collection Exist?
{
ManagementObjectCollection oQueryResults =
m_oProvider.Connection.ExecuteQuery(
"SELECT * FROM SMS_Collection WHERE NAME=\"" + sCollectionName + "\"");
foreach (ManagementObject mo in oQueryResults)
{
//collection Exists. Stop Creation
throw new Exception("Found collection. Halting Creation.");
}
}
catch (Exception e)
{
//collection Does Not Exist. Continue.
ManagementObject oCollection = m_oProvider.Connection.CreateInstance("SMS_Collection");
oCollection.SetPropertyValue(
"Name", sCollectionName);
oCollection.SetPropertyValue(
"OwnedByThisSite", true);
string sRelPath = oCollection.Put(new PutOptions(m_oProvider.Context)).RelativePath;
// Load the new collection
oCollection = m_oProvider.Connection.GetObject(sRelPath, m_oProvider.Context);
Load(oCollection,
true);
}
}