How can I delete computers from my Notification Server database based on the criteria of not receiving Inventory in a certain amount of time?
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 (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
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())
IItem item = new Item();
Console.WriteLine("Found " + list.Count + " Resources");
foreach (Guid guid in list)
item = Item.GetItem(guid);
Console.WriteLine("Deleting " + item.Name);
catch (Exception ex)
Console.WriteLine("Error deleting " + item.Name);