Professional Documents
Culture Documents
log"
PatchServer
= "KaceServer.YourDomain.com"
with KACE01-sa.com?
PatchServerIP
= "10.5.1.40"
with 10.100.10.10 ?
should I replace it
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"
KaceHasAlreadyBeenReInstalled = vbFalse
= CreateObject("Scripting.FileSystemObject")
= CreateObject("Wscript.Shell")
Set objApp
= CreateObject("Shell.Application")
Set objSysInfo
= CreateObject("ADSystemInfo")
=1
InstallLocation
InstallSource
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
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
'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
httpProxyRemoved = vbFalse
httpsProxyRemoved = vbFalse
httpProxy = "Not Found"
httpsProxy = "Not Found"
'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)
If (IsKACEConnected) Then
ConnectedEndScript
End If
End If
PathKuid6432 =
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Dell\Kace\MachineId"
PathKuid
= "HKEY_LOCAL_MACHINE\SOFTWARE\Dell\Kace\MachineId"
= objShell.RegRead(PathKuid)
on error goto 0
KUID = ValueKuid & ValueKuid6432
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
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)
'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.")
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
'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
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)
'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
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
Sub RemoveKACE
objResult.writeline "Un-installing KACE. - " & (timer - start)
if (-not(KaceHasAlreadyBeenReInstalled)) then
on error resume next
' 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
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")
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
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'"
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%
%'"
Sub UpdateKACEVersion
'This routine assumes that KACE is currently healthy and connected
objResult.writeline "Installed KACE Agent version is '" & KACEVersion & "'. - " &
(timer - start)
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)
'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 &
Sub ConnectedEndScript
objResult.writeline "KACE is connected. - " & (timer - start)
ForceKACEInventory
EnableTargetVersion = vbFalse
UpdateKACEVersion
End If
End If
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