How to efficiently delete a large number of un-managed computers from the Notification Server

book

Article ID: 181062

calendar_today

Updated On:

Products

Management Platform (Formerly known as Notification Server)

Issue/Introduction

 

Resolution

Question

In article 17560, the NScript is used to delete computers from the Notification Server that are member of a specific collection. How could a similar script be used to remove unmanaged computers from the Notification Server, given the right-click option on the reports is too slow and inefficient for the task?

Answer
In order to delete computers that are not members of a collection (unmanaged), we will move the specified SQL statement in 17560 into the body of the NScript.

This will allow us to modify the SQL query directly and control which computer resources will be deleted (regardless of their managed or unmanaged state).

Note: In order to specificy the computer resources to be deleted, you will need to modify the SQL query in red to suit your need. Do not use this script to remove anything but computer resources from the Notification Server.

Here is the modified script:

/* Copy this contents into notepad and save the file as DeleteComputerResources.cs */

using System;
using System.Collections;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

using Altiris.NS;
using Altiris.NS.StandardItems;
using Altiris.NS.ItemManagement;
using Altiris.NS.Security;
using Altiris.NS.ContextManagement;

namespace NScript
{
 public class DeletecomputerResources
 {
  [STAThread]
  static void Main(string[] args)
  {

   SecurityContextManager.SetContextData();
   Guid collectionGuid;

   using (DatabaseContext context = DatabaseContext.GetContext())
   {
    string sql = string.Format(@"select c.Guid from [vComputerResource] c
                        INNER JOIN
                        (select [ResourceGuid], max([ModifiedDate]) as LatestInventoryDate from dbo.ResourceUpdateSummary
                        group by [ResourceGuid]) as dt
                        ON c.Guid = dt.ResourceGuid
                        inner join vResourceEx rx on rx.Guid = c.Guid
                        where rx.IsManaged = 1 and rx.IsLocal = 0
                        and dt.LatestInventoryDate <dateadd(dd,-60,getdate())
");
   
    ArrayList list = new ArrayList();
    int i = 0;

    using (SqlDataReader reader = (SqlDataReader) context.CreateCommand(sql).ExecuteReader())
    {
     while (reader.Read())
     {
      list.Add(reader.GetGuid(0));
     }
    }

    Console.WriteLine("{0.EN_US} computer(s) to be deleted", list.Count);

    if (list.Count == 0)
     return;

    foreach (Guid guid in list)
    {
     Item.DeleteItem(guid);
     if (++i % 20 == 0)
     {
      Console.WriteLine("{0.EN_US} done..",i);
     }
    }

    Console.WriteLine("Deletion of {0.EN_US} computer(s) complete.",i);

   }
  }
 }
}

Attachments

36422_Delete unmanaged_computers_v1.cs.txt get_app