Delete obsolete SMS clients
An easy way to delete all the obsolete SMS clients...
Introduction
Deleting the obsolete SMS clients can really be a pain in the ass. Especially at environments where a few 100 SMS clients get obsolete every day.
Using the code
I used the
System.Management
to query for and delete the obsolete SMS clients.
Query for obsolete SMS clients: private void ThreadQueryObsolete()
{
...
ManagementClass systemclass = new ManagementClass("SMS_R_System");
ManagementScope oMs = new ManagementScope("\\\\" + tbxSMSServername.Text.Trim() + "\\root\\SMS\\Site_" + tbxSMSSiteCode.Text.Trim());
systemclass.Scope = oMs;
ObjectQuery oQuery = new ObjectQuery("SELECT ResourceId, NetbiosName FROM SMS_R_System WHERE Obsolete = 1");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach (ManagementObject oReturn in oReturnCollection)
{
string[] obsoletes = new string[2];
obsoletes[0] = oReturn["ResourceId"].ToString();
obsoletes[1] = oReturn["NetbiosName"].ToString();
obsoleteList.Add(obsoletes);
ListViewItem item = new ListViewItem(oReturn["NetbiosName"].ToString(), 0);
SetListViewItems(item);
}
...
}
Delete the obsolete SMS clients: private void ThreadDeleteObsolete()
{
...
ManagementClass systemclass = new ManagementClass("SMS_R_System");
ManagementScope oMs = new ManagementScope("\\\\" + tbxSMSServername.Text.Trim() + "\\root\\SMS\\Site_" + tbxSMSSiteCode.Text.Trim());
systemclass.Scope = oMs;
ManagementObject system = systemclass.CreateInstance();
ObjectQuery oQuery = new ObjectQuery("SELECT ResourceId, NetbiosName FROM SMS_R_System WHERE ResourceId = '" + ((string[])obsoleteList[i])[0] + "' AND NetbiosName = '" + ((string[])obsoleteList[i])[1] + "' AND Obsolete = 1");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach (ManagementObject oReturn in oReturnCollection)
{
system["ResourceId"] = oReturn["ResourceId"].ToString();
system.Delete();
}
...
}
History
Version 1.0 - Initial version.