You are on page 1of 28

LogOfFixedComputers = "\\10.100.33.105\d$\KACELogs\fixed.

log"
PatchServer
= "KaceServer.YourDomain.com"
with KACE01-sa.com?

PatchServerIP
= "10.5.1.40"
with 10.100.10.10 ?

should I replace it

should I replace PatchServerIP

Or Patch server is test machine IP?


++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++

We have GPO configured but more than 150 machines are


not installed with kace and some times
Inventory are not updating , and sometimes Kace client
machines are not communicating with
Kace server K1000.
How do we deploy this script to all machines? Via GPO?

What do you recommend to fix thesethanks for sharing


script
++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++

KACEInstallerName
emailServer
SendEmailTo

= "ampagent-x86_KaceServer.YourDomain.com.msi"

= "mail.YourDomain.com"
= "abdulrahima@fro.com"

' I only recommend turning on target version long enough to remediate the
environment when the KACE appliance is not getting the job done
EnableTargetVersion = vbTrue
KACETargetVersion

= "6.4.180"

'End - Update the following variables to fit your environment

Const HKEY_CLASSES_ROOT = &H80000000


Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1, ForWriting = 2, ForAppending = 8

KaceHasAlreadyBeenReInstalled = vbFalse

Set objNTSysInfo = CreateObject( "WinNTSystemInfo" )


Set fso

= CreateObject("Scripting.FileSystemObject")

Set objWMIService = GETOBJECT("winmgmts:\\.\root\cimv2")


set objShell

= CreateObject("Wscript.Shell")

Set objApp

= CreateObject("Shell.Application")

Set objSysInfo

= CreateObject("ADSystemInfo")

Set objRegistry = GetObject ("winmgmts:\\.\root\default:StdRegProv")


strInstallerID

=1

For Each objItem in objWMIService.ExecQuery( "Select * from


Win32_ComputerSystem", , 48 )
strComputerDomain = objItem.Domain
Next

InstallLocation

= "\\" & strComputerDomain & "\NETLOGON\"

InstallSource

= "\\" & strComputerDomain & "\NETLOGON\" & KACEInstallerName

InstallDestination = objShell.ExpandEnvironmentStrings( "%systemroot


%\system32\KACEHealthCheck\" )

'** Initiate Logging so that we have metrics


start = timer
startDate = now()

'Create the log file


Set objResult = Fso.OpenTextFile(InstallDestination & objNTSysInfo.ComputerName
&"-LastRun.log" , ForWriting, True)
objResult.writeline "Start time = " & StartDate
objResult.writeline "Running Script on Machine - " & objNTSysInfo.ComputerName

'If we are not running as local system, abort


'Because we could do more harm than good depending on UAC settings
'if the user running the script is less than an administrator equivalent
'We are using the SID to stay language agnostic

cmd = "cmd /c for /f ""tokens=3"" %a in ('whoami /user ^| findstr /i s-1-5') do


@echo %a"
set ReturnedOutput = objShell.exec(cmd)
UserSID = ReturnedOutput.StdOut.ReadLine()

objResult.writeline "Running as - """ & UserSID & """"

If (not (UserSID = "S-1-5-18")) Then


objResult.writeline "Not running as SYSTEM, aborting script. - " &
objNTSysInfo.ComputerName
wscript.quit
End If

'If we are connected, end the script


If (IsKACEConnected) Then
ConnectedEndScript
End If

'Wait until the domain appears to be available - check every 5 minutes


'Once the domain becomes available, then we have a chance of connecting to KACE
objResult.writeline "KACE is not connected. Begin waiting for NETLOGON. - " &
(timer - start)
Do While (Not (fso.FolderExists(InstallLocation)))
WScript.Sleep(1000 * 60 * 1)
'objResult.writeline "Looping - waiting for domain" 'Used to proove we are
background looping
Loop

'Check if the KACE server is available on TCP 52230


'If it is not available, but the network is available,
'then there may be a firewall issue, or the KACE server may be down, give it time to
come up
'Give it 90 minutes to resolve (it can take the KACE server 60+ minutes to reboot)
objResult.writeline "NETLOGON found. Begin waiting for " & PatchServer & " on TCP
52230. - " & (timer - start)

Timeout = 0
Do While (not(TCPPing(PatchServer, 52230)))
WScript.Sleep(1000 * 60 * 1)
Timeout = Timeout + 1
If (Timeout >= 90) Then
SendEmailAndQuitScript("Computer can talk to the domain, but not the KACE
server. There may be a firewall or KACE server issue.")
End if
'objResult.writeline "Looping - waiting for patch server" 'Used to proove we are
background looping
Loop

objResult.writeline PatchServer & " was found. - " & (timer - start)

'Since we have access to the domain, and therefore the install files...
'and the KACE server appears to be available...
'If KACE is not installed, install it
objResult.writeline "Checking if the KACE agent is installed - " & (timer - start)
if (not(KaceAgentInstalled)) then
objResult.writeline "The KACE agent is not installed, installing it. - " & (timer start)
InstallKACE
else
objResult.writeline "The KACE agent is installed. - " & (timer - start)
end if

'And finally, if the KACE agent is not registered, re-install KACE

objResult.writeline "Checking if the KACE agent is registered - " & (timer - start)
if (not(KaceAgentRegistered)) Then
objResult.writeline "The KACE service is not registered, re-installing KACE. - " &
(timer - start)
RemoveKACE
InstallKACE
else
objResult.writeline "The KACE agent is registered. - " & (timer - start)
End If

'Since we have access to the domain, the KACE server appears to be available,
'the KACE agent is installed, and the KACE service is registered...
'If we are connected, abort the script
If (IsKACEConnected) Then
ConnectedEndScript
End If

objResult.writeline "KACE is not connected. - " & (timer - start)

'Restart the KACE service, it may not be broken, the service may require a bounce
'because the KACE server may not have been available when the computer started
up originally
objResult.writeline "Restarting the KACE service as an attempted fix. - " & (timer start)
ReStartKACEService
If (IsKACEConnected) Then
ConnectedEndScript

End If

objResult.writeline "KACE is not connected. - " & (timer - start)

'If these environmental variables exist as global variables, then


'the ampagent may not work because its communications is being sent
'through a proxy. So remove the variable and report the condition to the help desk.
'It may take a reboot for this step to actually be effective
objResult.writeline "Checking for http_proxy and https_proxy - " & (timer - start)

httpProxyRemoved = vbFalse
httpsProxyRemoved = vbFalse
httpProxy = "Not Found"
httpsProxy = "Not Found"

If NOT (objShell.ExpandEnvironmentStrings("%http_proxy%") = "%http_proxy%")


Then
httpProxy = objShell.ExpandEnvironmentStrings("%http_proxy%")
objResult.writeline "Removing global environmental variable http_proxy: " &
httpProxy & " - " & (timer - start)
objShell.RegDelete ("HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment\HTTP_PROXY")
httpProxyRemoved = vbTrue
else
objResult.writeline "http_proxy variable not found. - " & (timer - start)
End If

If NOT (objShell.ExpandEnvironmentStrings("%https_proxy%") = "%https_proxy%")


Then
httpsProxy = objShell.ExpandEnvironmentStrings("%https_proxy%")
objResult.writeline "Removing global environmental variable https_proxy: " &
httpsProxy & " - " & (timer - start)
objShell.RegDelete ("HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment\HTTPS_PROXY")
httpsProxyRemoved = vbTrue
else
objResult.writeline "https_proxy variable not found. - " & (timer - start)
End If

'sending e-mail about removing the http/https proxy from the global environmental
variables
IF (httpsProxyRemoved or httpProxyRemoved) Then
'Let the help desk know that the global variables were changed
objResult.writeline "Sending email that global environmental variables were
updated. - " & (timer - start)
SendEmailAndContinue("INFO: KACE removed the HTTP_Proxy and/or
HTTPS_Proxy values from HKLM\System\CurrentControlSet\Control Session
Manager\Environment. Settings removed were http_Proxy:" & httpProxy & "
https_proxy:" & httpsProxy)

'Restarting the agent because it has new proxy setting


ReStartKACEService

If (IsKACEConnected) Then
ConnectedEndScript
End If

End If

'Maybe the KUID is the issue


'Backup the KUID, erase it, restart the service
objResult.writeline "Generating a new KACE KUID as attempted fix - " & (timer start)

PathKuid6432 =
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Dell\Kace\MachineId"
PathKuid

= "HKEY_LOCAL_MACHINE\SOFTWARE\Dell\Kace\MachineId"

'Grab the current KUID


on error resume next
ValueKuid6432 = objShell.RegRead(PathKuid6432)
ValueKuid

= objShell.RegRead(PathKuid)

on error goto 0
KUID = ValueKuid & ValueKuid6432

objResult.writeline "Stopping KACE service - " & (timer - start)


StopKaceService

objResult.writeline "Backing up the KACE KUID " & KUID & " - " & (timer - start)
objShell.Run "REG EXPORT
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Dell\Kace " & InstallDestination &
"KUID6432.reg /y", 0, true
objShell.Run "REG EXPORT HKEY_LOCAL_MACHINE\SOFTWARE\Dell\Kace " &
InstallDestination & "KUID.reg /y", 0, true

objResult.writeline "Erasing the KACE KUID " & KUID & " - " & (timer - start)
objShell.Run "REG DELETE
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Dell\Kace /f", 0, true
objShell.Run "REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Dell\Kace /f", 0, true

StartKaceService

objResult.writeline "Checking if KACE is connected. - " & (timer - start)


If (IsKACEConnected) Then
objResult.writeline "KACE was broken, generating a new KUID fixed it. Old KUID: "
& KUID & " - " & (timer - start)

Set objLog = Fso.OpenTextFile(LogOfFixedComputers, ForAppending, True)


objLog.writeline startDate & " - " & objNTSysInfo.ComputerName & " - New KUID
fixed it - old KUID: " & KUID

'Record the date that the machine was fixed


todaysDate = Date()
Set objFixed = Fso.OpenTextFile(InstallDestination & objNTSysInfo.ComputerName
&"-fixed.log" , ForAppending, True)
objFixed.writeline todaysDate
objFixed.Close

ConnectedEndScript
End If

'The new KUID did not fix the problem, restore the original KUID

objResult.writeline "A new KUID did not fix the problem - " & (timer - start)
objResult.writeline "Will restore the original KACE KUID " & KUID & " - " & (timer start)

objResult.writeline "Stopping KACE service - " & (timer - start)


StopKaceService

on error resume next


objResult.writeline "Restoring the original KACE KUID " & KUID & " - " & (timer start)
objShell.Run "reg import " & InstallDestination & "KUID6432.reg", 0, true
objShell.Run "reg import " & InstallDestination & "KUID.reg", 0, true
on error goto 0

'If we are here, just re-install and hope for the best
objResult.writeline "Re-installing KACE because it is broke. - " & (timer - start)
RemoveKACE
InstallKACE

'If we are here, then all attempts to fix KACE have failed (the InstallKACE routine
ends the program if it fixes the agent)
'Send an alert and quit the script
SendEmailAndQuitScript("KACE is in a failed state. Please contact Dell KACE
support at 1-888-522-3638.")

'We should never get to this point - ever


objResult.writeline "Script abnormally ended. - " & (timer - start)

wscript.quit

Function IsKACEConnected
objResult.writeline "Waiting 5 seconds then testing if KACE is connected. - " &
(timer - start)
wscript.sleep 5000

IsKACEConnected=vbFalse
set shExec = objShell.Exec("netstat -an")
Do While Not shExec.StdOut.AtEndOfStream
if (InStr(shExec.StdOut.ReadLine(),LEFT(PatchServerIP & ":52230" &
Space(10),23) & "ESTABLISHED")) Then
IsKACEConnected=vbTrue
End If
Loop
End Function

Sub InstallKACE
objResult.writeline "Installing KACE - " & (timer - start)
if (-not(KaceHasAlreadyBeenReInstalled)) then
KaceHasAlreadyBeenReInstalled = vbTrue
fso.Copyfile InstallSource, InstallDestination
objShell.Run "msiexec.exe /i " & InstallDestination & KACEInstallerName & "
/qn", 1, true
objResult.writeline "KACE installation has completed. - " & (timer - start)

'Give KACE some time for the service to get up and running
objResult.writeline "Giving up to 10 seconds for the services to start. - " &
(timer - start)
TimeOut=0
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent'"
Do While ((objWMIService.ExecQuery(strWMIQuery).Count < 1) and (TimeOut
< 10))
TimeOut = TimeOut + 1
Wscript.Sleep 1000
Loop

'Give KACE some time to connect to the server


objResult.writeline "Waiting for the KACE service to connect with the server, up
to 120 seconds. - " & (timer - start)
TimeOut=0
Do While ((NOT IsKACEConnected) and (TimeOut < 24))
TimeOut = TimeOut + 1
Loop

'If we exited the loop because we saw a connection, record that fact
if (TimeOut < 24) Then
objResult.writeline "We saw a momentary connection on port 52230. - " &
(timer - start)
else
objResult.writeline "A connection on port 52230 did not happen. - " & (timer
- start)
End If

'If we fixed KACE by installing the agent, record that fact and end the script
If (IsKACEConnected) Then
objResult.writeline "The KACE agent has been installed and is now
operational. - " & (timer - start)
Set objLog = Fso.OpenTextFile(LogOfFixedComputers, ForAppending, True)
objLog.writeline startDate & " - " & objNTSysInfo.ComputerName

'Record the date that the machine was fixed


todaysDate = Date()
Set objFixed = Fso.OpenTextFile(InstallDestination &
objNTSysInfo.ComputerName &"-fixed.log" , ForAppending, True)
objFixed.writeline todaysDate
objFixed.Close

'End the script


ConnectedEndScript
End If

Else
objResult.writeline "KACE has already been installed by this script, install will
not be performed again. - " & (timer - start)
End If
End Sub

Sub SendEmailAndQuitScript(ErrorMessage)

objResult.writeline "Opening an SR with HelpDesk if appropriate. - " & (timer start)

lastFixDate = ReadLastLineOfFile(InstallDestination &


objNTSysInfo.ComputerName & "-FailedToFix.log")
todaysDate = Date()

'If the last fix or attempted fix was not today, send an email
If NOT (strComp(lastFixDate,todaysDate,vbTextCompare) = 0) Then
strWMIQuery = "Select * from Win32_OperatingSystem"
For Each Caption in objWMIService.ExecQuery(strWMIQuery)
OSCaption = Caption.Caption
Next

MessageHeader =
"Computer with KACE issue: " &
objNTSysInfo.ComputerName & vbCr
MessageHeader = MessageHeader & "Computer Location: "
objSysInfo.SiteName & vbCr
MessageHeader = MessageHeader & "Computer OS: "

&
& OSCaption &

vbCr

Set objMessage = CreateObject("CDO.Message")


objMessage.Subject = "ERROR: KACE: " & objNTSysInfo.ComputerName & ":
Health check"
objMessage.From = "KACEHealthScript@YourDomain.com"
objMessage.To = SendEmailTo
objMessage.TextBody = MessageHeader & ErrorMessage
objMessage.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

objMessage.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = emailServer
objMessage.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
objResult.writeline ErrorMessage & " - " & (timer - start)
objResult.writeline "ERROR: KACE is in a failed state, an error Email has been
sent. Script aborting. - " & (timer - start)
End If

'Record the date of the attempted fix


Set objFixed = Fso.OpenTextFile(InstallDestination & objNTSysInfo.ComputerName
&"-FailedToFix.log" , ForAppending, True)
objFixed.writeline todaysDate
objFixed.Close

objResult.writeline "Script has completed. - " & (timer - start)


Wscript.Quit
End Sub

Sub RemoveKACE
objResult.writeline "Un-installing KACE. - " & (timer - start)
if (-not(KaceHasAlreadyBeenReInstalled)) then
on error resume next

If (not (fso.FileExists(InstallSource))) then

SendEmailAndQuitScript("KACE is in a failed state. KACE installer was not


found in NETLOGON. Auto-repair is not possible.")
End if

' we search for Dell KACE to keep the search language agnostic
objShell.Run "WMIC product where ""Name LIKE '%%Dell KACE%%'"" call
uninstall /nointeractive", 0, true
objShell.Run "SC delete ampagent", 0, true
objShell.Run "REG DELETE
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\AMPAgent /f", 0, true

GetKaceRegistryKey
LoopCount = 1
Do While ((len(strInstallerID) > 4) and (Loopcount < 5))
Loopcount = LoopCount + 1
kaceregkey = "HKEY_CLASSES_ROOT\Installer\Products\" & strInstallerID
objShell.Run "REG DELETE " & kaceregkey & " /f", 0, true
GetKaceRegistryKey
Loop

fso.DeleteFolder objShell.ExpandEnvironmentStrings( "%ProgramFiles


%\Dell\Kace"), True
fso.DeleteFolder objShell.ExpandEnvironmentStrings( "%ALLUSERSPROFILE
%\Dell\Kace"), True
fso.DeleteFolder "C:\Program Files (x86)\Dell\Kace", True
fso.DeleteFolder "C:\Program Files\Dell\Kace", True
fso.DeleteFolder "C:\ProgramData\Dell\Kace", True
fso.DeleteFolder "C:\Documents And Settings\All Users\Dell\Kace", True

on error goto 0
Else
objResult.writeline "KACE was previously installed by this script, un-install will
not be done. - " & (timer - start)
End If
End Sub

Sub GetKaceRegistryKey
strInstallerID = 1

strInstallerKeyPath = "Installer\Products"
objRegistry.EnumKey HKEY_CLASSES_ROOT, strInstallerKeyPath,
arrProductsSubKeys
For Each objIProductsSubkey In arrProductsSubkeys
strValueInstaller = "ProductName"
strSubInstallerPath = strInstallerKeyPath & "\" & objIProductsSubkey
objRegistry.GetStringValue
HKEY_CLASSES_ROOT,strSubInstallerPath,strValueInstaller,strProductName
If (Instr(strProductName, "KACE")) Then
strInstallerID = objIProductsSubkey
End If
Next
End Sub

Sub ReStartKACEService
StopKACEService
StartKACEService
End Sub

Sub StopKACEService
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent'"
For Each Service in objWMIService.ExecQuery(strWMIQuery)
objResult.writeline "Stopping the KACE service. - " & (timer - start)
Service.StopService
TimeOut=0
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent' and
state='Stopped'"
Do While ((objWMIService.ExecQuery(strWMIQuery).Count < 1) and (TimeOut
< 20))
TimeOut = TimeOut + 1
Wscript.Sleep 500
Loop
Next
End Sub

Sub StartKACEService
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent'"
For Each Service in objWMIService.ExecQuery(strWMIQuery)
objResult.writeline "Setting the KACE service to 'Automatic'. - " & (timer - start)

Service.ChangeStartMode("Automatic")

objResult.writeline "Starting the KACE service. - " & (timer - start)


Service.StartService
TimeOut=0
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent' and
state='Running'"
Do While ((objWMIService.ExecQuery(strWMIQuery).Count < 1) and (TimeOut
< 20))
TimeOut = TimeOut + 1
Wscript.Sleep 500
Loop

objResult.writeline "Give KACE up to 90 seconds to connect. - " & (timer - start)


TimeOut=0
Do While ((-not(IsKACEConnected)) and (TimeOut <= 18))
TimeOut = TimeOut + 1
Loop
Next
End Sub

Function TCPPing( address, port )


Set xhr = WScript.CreateObject("MSXML2.ServerXMLHTTP")
xhr.SetTimeouts 8000,8000,8000,8000
On Error Resume Next
xhr.Open "GET", "http://" & address & ":" & port, False

xhr.Send
Select Case Err.Number
' ok, tcp connect but no web response, 401 auth failure
Case 0, -2147012744, -2147024891
'msg = "OK"
TCPPing = vbTrue
Case -2147012867
'msg = "Connection Rejected"
TCPPing = vbFalse
Case -2147012894
'msg = "Timed out"
TCPPing = vbFalse
Case -2147012889
'msg = "Could not resolve address"
TCPPing = vbFalse
Case -2147467259
'msg = "Cannot test that port with this tool"
TCPPing = vbFalse
Case Else
'msg = "Unknown error " & Err.Number
TCPPing = vbFalse
End Select
On Error Goto 0
Set xhr = Nothing
End Function

Sub ForceKACEInventory
'This routine assumes that KACE is currently healthy and connected

objResult.writeline "Running KACE inventories. - " & (timer - start)


RunKbotExe = """" & KbotPath & "\runkbot.exe"""

' Launch the inventory


objResult.writeline "Executing RunKbot 4 0 - " & (timer - start)
objShell.Run RunKbotExe & " 4 0",0,true

'Wait 25 seconds for kbot to launch


Wscript.Sleep 25000

'Wait for up to 180 seconds for kbot to finish running


TimeOut = 0
Do While ((KInventoryRunning) and (TimeOut < 180))
TimeOut = TimeOut + 1
Wscript.Sleep 1000
Loop

' Look for managed installs


objResult.writeline "Executing RunKbot 6 0 - " & (timer - start)
objShell.Run RunKbotExe & " 6 0",0,true
End Sub

Function ReadLastLineOfFile (File)


If fso.FileExists(File) Then
Set objFile = fso.OpenTextFile(File, ForReading)

Do Until objFile.AtEndOfStream
strNextLine = objFile.ReadLine
If Len(strNextLine) > 0 Then
strLine = strNextLine
End If
Loop

objFile.Close
ReadLastLineOfFile = strLine
Else
ReadLastLineOfFile = "Never"
End If
End Function

Function KInventoryRunning
KInventoryRunning = False
strObject = "winmgmts://"
For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
If UCase( Process.name ) = UCase( "KINVENTORY.EXE" ) Then
KInventoryRunning = True

Exit Function
End If
Next
End Function

Function KaceAgentRegistered
KaceAgentRegistered = vbFalse
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent'"

For Each Service in objWMIService.ExecQuery(strWMIQuery)


KaceAgentRegistered = vbTrue
Next
End Function

Function KaceAgentInstalled
KaceAgentInstalled = vbFalse

' we search for Dell KACE to keep the search language agnostic
strWMIQuery = "Select * from Win32_Product Where Name LIKE '%%Dell KACE%
%'"

For Each Service in objWMIService.ExecQuery(strWMIQuery)


KaceAgentInstalled = vbTrue
Next
End Function

Sub UpdateKACEVersion
'This routine assumes that KACE is currently healthy and connected

' We use amptools to report the version as opposed to looking at the


ampagent.exe metadata
' because the metadata headers are in whatever language the OS is in, and thus
hard to search
objResult.writeline "Checking KACE Agent Version. - " & (timer - start)
AmpCommand = """" & KbotPath & "\amptools.exe"" get version"

Set objWshScriptExec = objShell.Exec(AmpCommand)


Set objStdOut = objWshScriptExec.StdOut

While Not objStdOut.AtEndOfStream


KACEVersion = objStdOut.ReadLine
Wend

objResult.writeline "Installed KACE Agent version is '" & KACEVersion & "'. - " &
(timer - start)

' Install the target version if it is not already installed


If (KACEVersion = KACETargetVersion) Then
objResult.writeline "KACE Agent is up to date. - " & (timer - start)
Else
' Upgrade the agent. I do not use runkbot 5 0 because you can not upgrade
between some versions

objResult.writeline "Target KACE Agent version is: " & KACETargetVersion & ".
Re-installing KACE agent. - " & (timer - start)
RemoveKACE
InstallKACE
End If
End Sub

Sub SendEmailAndContinue(Message)
objResult.writeline "Opening an SR with HelpDesk if appropriate. - " & (timer start)

lastFixDate = ReadLastLineOfFile(InstallDestination &


objNTSysInfo.ComputerName & "-FailedToFix.log")
todaysDate = Date()

'If the last fix or attempted fix was not today, send an email
If NOT (strComp(lastFixDate,todaysDate,vbTextCompare) = 0) Then
strWMIQuery = "Select * from Win32_OperatingSystem"
For Each Caption in objWMIService.ExecQuery(strWMIQuery)
OSCaption = Caption.Caption
Next

MessageHeader =
"Computer with KACE issue: " &
objNTSysInfo.ComputerName & vbCr
MessageHeader = MessageHeader & "Computer Location: "
objSysInfo.SiteName & vbCr
MessageHeader = MessageHeader & "Computer OS: "
vbCr

&
& OSCaption &

Set objMessage = CreateObject("CDO.Message")


objMessage.Subject = "INFO: KACE: " & objNTSysInfo.ComputerName
objMessage.From = "KACEHealthScript@YourDomain.com"
objMessage.To = SendEmailTo
objMessage.TextBody = MessageHeader & Message
objMessage.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = emailServer
objMessage.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
objResult.writeline Message & " - " & (timer - start)
End If
End Sub

Sub ConnectedEndScript
objResult.writeline "KACE is connected. - " & (timer - start)
ForceKACEInventory

'If we have access to the target version installer file


'and we are not at the target version, upgrade the locally installed version
If (fso.FileExists(InstallSource)) then
If (EnableTargetVersion) Then

EnableTargetVersion = vbFalse
UpdateKACEVersion
End If
End If

objResult.writeline "Ending script. - " & (timer - start)


wscript.quit
End Sub

Function KbotPath
'Get path of the ampagent service
'A vbEmpty string is returned if the service does not exist
strWMIQuery = "Select * from Win32_Service Where Name = 'ampagent'"
For Each Service in objWMIService.ExecQuery(strWMIQuery)
ThePath = Replace (Service.PathName,"\ampagent.exe","",1,1,vbTextCompare)
KbotPath = Mid(ThePath,2,Len(ThePath)-2)
Next
End Function

You might also like