Script to deregister AppSense Computers from Deployment Group

I recently had a requirement to deregister a Computer from an existing AppSense Group before an uninstallation of appsense agent in preperation for a Operating System upgrade.  This script was used to automate the process , rather than any manual intervention by manually deregistering them in management console.

Save the below as DeleteMachine.js

NOTE: WARNING, USE AT YOUR OWN RISK

/////////////////////////////////////////////////////////////////////////////////////
//
// This script will delete a locally installed CCA from it’s Management Server. It’s
// important that the following points are considered:
//
// 1. The CCA Service must be stopped before running this script.
// 2. The account that executes the script must be able to authenticate with the
// Management Server’s ‘\ManagementServer’ IIS web directory. This can be tested
// by browsing to http://servername/ManagementServer with Microsoft Internet
// Explorer. If the browser prompts for credentials, then ensure that
// ‘Local Intranet’ is displayed within the status panel.
// 3. Ensure that the account that executes the script has read permissions on the
// ‘\ManagementSever’ IIS web directory.
//
// To run the script, call:
// cscript //nologo DeleteMachine.js
//
/////////////////////////////////////////////////////////////////////////////////////

DeleteLocalCCA ();

/////////////////////////////////////////////////////////////////////////////////////
// Deletes the local CCA from it’s management server.
/////////////////////////////////////////////////////////////////////////////////////
function DeleteLocalCCA ()
{
var webSite = GetManagementServerWebSite();
var machinesUrl = webSite + “/ManagementServer/DataAccess/Machines.asmx”;
var machineId = GetLocalMachineId(machinesUrl);

if (machineId == null || webSite == null)
{
return;
}

var httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
if (DeleteMachine(httpRequest, machinesUrl, machineId))
{
return;
}
}

/////////////////////////////////////////////////////////////////////////////////////
// Deletes the machine from the Management Server database. Note that the
// caller must have permissions within IIS to call this method.
/////////////////////////////////////////////////////////////////////////////////////

function DeleteMachine (httpRequest, url, machineId)
{
var requestContent = “<?xml version=’1.0′ encoding=’utf-8′ ?>\n”;
requestContent = requestContent + “<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'”;
requestContent = requestContent + ” xmlns:xsd=’http://www.w3.org/2001/XMLSchema'”;
requestContent = requestContent + ” xmlns:soap=’http://www.w3.org/2003/05/soap-envelope’>\n”;
requestContent = requestContent + ” <soap:Body>\n”;
requestContent = requestContent + ” <DeleteMachine xmlns=’http://www.AppSense.com/ManagementServer/DataAccessWebService/Groups/Machines’>\n”;
requestContent = requestContent + ” <machineKey>” + machineId + “</machineKey>\n”;
requestContent = requestContent + ” </DeleteMachine>\n”;
requestContent = requestContent + ” </soap:Body>\n”;
requestContent = requestContent + “</soap:Envelope>\n”;

// Send the request to the Management Server
try
{
httpRequest.open (“POST”, url, false);
httpRequest.setRequestHeader (“Content-Type”, “application/soap+xml; charset=utf-8”);
httpRequest.send(requestContent);
}
catch (exception)
{
return false;
}

var document = new ActiveXObject(“Msxml2.DomDocument”);
if (document.loadXML(httpRequest.responseText))
{
document.setProperty(“SelectionNamespaces”, “xmlns:soap=’http://www.w3.org/2003/05/soap-envelope’ xmlns:method=’http://www.AppSense.com/ManagementServer/DataAccessWebService/Groups/Machines'”);

var soapFault = document.selectSingleNode (“//soap:Fault”);
if (null != soapFault)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}

////////////////////////////////////////////////////////////////////////////////////////
//Returns the machine id of the locally installed CCA.
////////////////////////////////////////////////////////////////////////////////////////

function GetLocalMachineId (url)
{
var wshShell = new ActiveXObject(“WScript.Shell”);
var machineId = null;
var computerdomain;
var netbios;
var includesummary;
var dns

//Get details about the machine to then get Machine Key
try
{
computerdomain = wshShell.RegRead (“HKLM\\System\\CurrentControlSet\\Services\\TcpIp\\Parameters\\Domain”);
}
catch (exception) {}

try
{
netbios = wshShell.RegRead (“HKLM\\System\\CurrentControlSet\\Services\\TcpIp\\Parameters\\Hostname”);
}
catch (exception) {}

dns = netbios + ‘.’ + computerdomain
includesummary = true

var requestContent = “<?xml version=’1.0′ encoding=’utf-8′ ?>\n”;
requestContent = requestContent + “<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'”;
requestContent = requestContent + ” xmlns:xsd=’http://www.w3.org/2001/XMLSchema'”;
requestContent = requestContent + ” xmlns:soap=’http://www.w3.org/2003/05/soap-envelope’>\n”;
requestContent = requestContent + ” <soap:Body>\n”;
requestContent = requestContent + ” <GetMachineFromDNSandNETBIOS xmlns=’http://www.AppSense.com/ManagementServer/DataAccessWebService/Groups/Machines’>\n”;
requestContent = requestContent + ” <dns>” + dns + “</dns>\n”;
requestContent = requestContent + ” <netbios>” + netbios + “</netbios>\n”;
requestContent = requestContent + ” <withSummary>” + includesummary + “</withSummary>\n”;
requestContent = requestContent + ” </GetMachineFromDNSandNETBIOS>\n”;
requestContent = requestContent + ” </soap:Body>\n”;
requestContent = requestContent + “</soap:Envelope>\n”;

// Send the request to the Management Server
try
{
var httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
httpRequest.open (“POST”, url, false);
httpRequest.setRequestHeader (“Content-Type”, “application/soap+xml; charset=utf-8”);
httpRequest.send(requestContent);
}
catch (exception) {}

var document = new ActiveXObject(“Msxml2.DomDocument”);
if (document.loadXML(httpRequest.responseText))
{
document.setProperty(“SelectionNamespaces”, “xmlns:soap=’http://www.w3.org/2003/05/soap-envelope’ xmlns:method=’http://www.AppSense.com/ManagementServer/DataAccessWebService/Groups/Machines'”);

var machineKeyNode = document.selectSingleNode (“//MachineKey”);
if (machineKeyNode)
{
return (machineKeyNode.text)
}
else
{
return null;
}
}
else
{
return null;
}
}

////////////////////////////////////////////////////////////////////////////////////////
//Returns the web site of the Management Server
////////////////////////////////////////////////////////////////////////////////////////
function GetManagementServerWebSite ()
{
var wshShell = new ActiveXObject(“WScript.Shell”);
var webSite = null;

try
{
webSite = wshShell.RegRead (“HKLM\\Software\\AppSense Technologies\\Communications Agent\\WebSite”);
}
catch (exception) {}

if (null == webSite)
{
try
{
machineId = wshShell.RegRead (“HKLM\\Software\\Wow6432Node\\AppSense Technologies\\Communications Agent\\WebSite”);
}
catch (exception) {}
}

return webSite;
}

Leave a Reply