Delete AppSense Client from backend during SCCM Task Sequence

When reinstalling/refreshing system with AppSense Agents which are registered at the AppSense management server there are part of a specific configuration/deployment group. When AppSense Agents have to be upgraded by new version a new management group/config can set. The issue here is, when reinstalling a system the old AppSense config will be installed.
The only solution is the delete it from the backend before the new AppSense communication agent is connecting again. In this way it will be automatically registered in the correct management/config. As commandline parameter you can set an AppSense GroupID, Below is described how to trigger a delete command from a SCCM Task Sequence to delete the client from the AppSense backend. Script was based on the forum post of lmwinbur at http://www.apug.info. Change it from Java to VBS so running it from a SCCM task sequence its a bit easier ;-)

Create a SCCM package containing an install.cmd and the script AppSenseDeleteAgent.vbs.

  1. Change the AppSense management URL in install.cmd
  2. Change the username & password within the VBS. Be aware the account is in plain text, again like my previous post you can encode it with Encode a Script from http://gallery.technet.microsoft.com.

Install.cmd contents:

@ECHO OFF
CLS
SET PACKPATH=%~p0
SET PACKDRV=%~d0
SET PACKDIR=%PACKDRV%%PACKPATH%

REM Check and if exist delete AppSense client from backend
start /wait cscript.exe //nologo “%PACKDIR%AppSenseDeleteAgent.vbe” “http://FQDN-APPSENSE-MANAGEMENTSERVER:80/” “C:\Windows\Temp\AppSenseAgent_Removal.log”

VBS script contents:

‘AppSenseDeleteAgent.vbs

‘Declare our working variables
Dim sMsg
Dim sURL
Dim sEnv

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set WshShell = WScript.CreateObject(“WScript.Shell”)

‘Get Arguments
sURL = WScript.Arguments.Item(0)
strLogfile = WScript.Arguments.Item(1)

‘Set Logfiles
strTempFile = “C:\Windows\Temp\AppSenseAgentRemovalTemp.log”

sURL =  sURL & “ManagementServer/DataAccess/Machines.asmx”

‘Get Computername & Domain
computerdomain = WshShell.RegRead(“HKLM\System\CurrentControlSet\Services\TcpIp\Parameters\Domain”)
netbios = WshShell.RegRead(“HKLM\System\CurrentControlSet\Services\TcpIp\\Parameters\Hostname”)

‘Check if logfile exits if so delete file first
If objFSO.FileExists(strLogfile) Then
            objFso.deleteFile(strLogfile)
End If
If objFSO.FileExists(strTempFile) Then
            objFso.deleteFile(strTempFile)
End If

‘Create Log file
Set objFile = objFSO.CreateTextFile _
    (strLogfile, ForWriting)

‘**Start Encode**

‘Set and Instantiate working objects
Set objHTTP = CreateObject(“MSXML2.XMLHTTP”)

‘Log General information AppSense
objFile.WriteLine “AppSense Management server: ” & sURL
objFile.WriteLine “Computername: ” & netbios
objFile.WriteLine “Domain: ” & computerdomain

‘Create SOAP envelope for submission to the Web Service of AppSense
sEnv = “<?xml version=””1.0″” encoding=””utf-8″”?>”
sEnv = sEnv & “<soap:Envelope xmlns:xsi=””http://www.w3.org/2001/XMLSchema-instance”” xmlns:xsd=””http://www.w3.org/2001/XMLSchema”” xmlns:soap=””http://schemas.xmlsoap.org/soap/envelope/””>”
sEnv = sEnv & ” <soap:Body>”
sEnv = sEnv & ” <GetMachineFromDNSandNETBIOS xmlns=’http://www.AppSense.com/ManagementServer/DataAccessWebService/Groups/Machines’>”
sEnv = sEnv & ” <dns>” & netbios & “.” & computerdomain & “</dns>”
sEnv = sEnv & ” <netbios>” & netbios & “</netbios>”
sEnv = sEnv & ” <withSummary>true</withSummary>”
sEnv = sEnv & ” </GetMachineFromDNSandNETBIOS>”
sEnv = sEnv & ” </soap:Body>”
sEnv = sEnv & “</soap:Envelope>”

‘Invoke the web service ‘use this code snippet to invoke a web service which requires authentication
ObjHTTP.Open “Post”, sURL, False, “DOMAIN\ACCOUNT“, “PASSWORD

‘Invoke a web service that doesn’t require any user authentication
‘ObjHTTP.Open “Post”, sURL, False
ObjHTTP.setRequestHeader “Content-Type”, “text/xml”
ObjHTTP.send(sEnv)

‘Log send information
objFile.WriteLine “Request AppSense Client information from server ” & sURL
objFile.WriteLine (sEnv)

‘Log response information
objFile.WriteLine “Reveived AppSense Client information from server ” & sURL
objFile.WriteLine ObjHTTP.ResponseText

‘Check if Machinekey is part of the response if not client is not with AppSense Server
Set objFile1 = objFSO.CreateTextFile _
    (strTempFile, ForWriting)
objFile1.WriteLine ObjHTTP.ResponseText
Set objInputFile = objFSO.OpenTextFile(strTempFile)
strToFind = “<MachineKey>”
tmpStr = objInputFile.ReadLine
If InStr(tmpStr, strToFind) <= 0 Then
    objFile.WriteLine “AppSense Client not found on server ” & sURL 
Else
    objFile.WriteLine “AppSense Client found on server ” & sURL
 Call DeleteAppSenseAgent(sURL)

End If

objFile1.Close

Function DeleteAppSenseAgent(strURL)
set xmldoc = CreateObject(“Msxml2.DomDocument”)
xmldoc.loadXML(ObjHTTP.ResponseText)

Set Node = xmldoc.documentElement.selectSingleNode(“//MachineKey”)

‘Invoke delete client on AppSense backend
sEnv = “<?xml version=””1.0″” encoding=””utf-8″”?>”
sEnv = sEnv & “<soap:Envelope xmlns:xsi=””http://www.w3.org/2001/XMLSchema-instance”” xmlns:xsd=””http://www.w3.org/2001/XMLSchema”” xmlns:soap=””http://schemas.xmlsoap.org/soap/envelope/””>”
sEnv = sEnv & ” <soap:Body>”
sEnv = sEnv & ” <DeleteMachine xmlns=’http://www.AppSense.com/ManagementServer/DataAccessWebService/Groups/Machines’>”
sEnv = sEnv & ” <machineKey>” & (Node.text) & “</machineKey>”
sEnv = sEnv & ” </DeleteMachine>”
sEnv = sEnv & ” </soap:Body>”
sEnv = sEnv & “</soap:Envelope>”

‘Invoke a web service that doesn’t require any user authentication
‘ObjHTTP.Open “Post”, sURL, False
ObjHTTP.Open “Post”, sURL, False, “DOMAIN\USER”, “PASSWORD”
ObjHTTP.setRequestHeader “Content-Type”, “text/xml”
ObjHTTP.send(sEnv)

objFile.WriteLine “Send delete AppSense Client command to server ” & sURL
objFile.WriteLine (sEnv)

objFile.WriteLine “Received delete conformation message from server ” & sURL
objFile.WriteLine ObjHTTP.ResponseText
End Function

‘clean up code
Set ObjHTTP = Nothing
Set xmlDoc = Nothing

Leave a Reply

Your email address will not be published. Required fields are marked *