Answer
This script is based in the Latest Inventory Date contained in the database for all the computers. By default, this script will delete computers that have not sent Inventory information for the last 30 days. If you want to change the default value of deleting computers that have not reported inventory in 30 days, open the file with notepad and locate the following line:
set @days = 30
Change 30 to the amount of days that you want to delete (for example 45).
This is an NS script and must be run in the Program Files\Altiris\Notification Server\bin folder using the command line: NScript.exe %File_Name.cs%. See the Readme file attached for more details of how to run this script. Change the .txt extension to .cs for the DeleteComputersFromTheNS file in order to run the script.
// Delete Computers that have not reported inventory in 30 days
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Altiris.NS.Utilities;
using Altiris.NS.ContextManagement;
using Altiris.NS.ItemManagement;
class Class1
{
static void
{
Altiris.NS.Security.SecurityContextManager.SetContextData();
using (DatabaseContext context = DatabaseContext.GetContext())
{
string sql = @"
declare @days int
set @days = 30
select cr.[Guid], dt.[LatestInventoryDate]
from [vComputerResource] cr
inner join (
select [ResourceGuid], max([ModifiedDate]) as LatestInventoryDate
from dbo.ResourceUpdateSummary
group by [ResourceGuid]
) as dt on cr.[Guid] = dt.[ResourceGuid]
inner join vResourceEx rx on rx.[Guid] = cr.[Guid]
where rx.[IsManaged] = 1
and dt.[LatestInventoryDate] < dateadd(dd, @days * -1, getdate())
order by LatestInventoryDate desc";
ArrayList list = new ArrayList();
using (IDataReader reader = context.CreateCommand(sql).ExecuteReader())
{
while (reader.Read())
{
list.Add(reader.GetGuid(0));
}
}
IItem item = new Item();
Console.WriteLine("Found " + list.Count + " Resources");
foreach (Guid guid in list)
{
try
{
item = Item.GetItem(guid);
Console.WriteLine("Deleting " + item.Name);
item.Delete();
}
catch (Exception ex)
{
Console.WriteLine("Error deleting " + item.Name);
Console.WriteLine(ex.Message);
}
}
}
}
}