ThinkPaper
asked on
VBS/Wscript check if computer exists (remotely/silently uninstall symantec thru script)
Ok.. this is a run-off from a previous question I had.
https://www.experts-exchange.com/questions/22494186/Wscript-VBScript-how-to-write-an-echo-that-disappears-after-a-while-no-OK-or-Confirm-button-like-a-progress-bar.html
I'm creating a script to remotely and silently remove symantec from all computers. It pulls all computer names from "computer.txt" and stores it in an array. Then for each computer, runs code to change registry keys and delete symantec.
I've been testing it out, but the problem is during testing. As a test, I put MY computer name 3 times in the computer.txt file. When I run it, the first time it hits my computer, it looks ok. But anytime after it, it fails, saying computer doesn't exist. This also occurs even if I put 3 different computer names. What's wrong with my error handling? Am I even doing it correctly? So far all the "executable" code is commented out and replaced with alerts. This is how I'm testing it (so I don't mess up the system =P )
Anyone help, please?
'Remove Symantec Remotely and Silently
'Results are stored in a log file: symantec.log
'--------------- Create Log File ----------------
'Open up the path to save the information into a text file
Dim Stuff, myFSO, WriteStuff, timeStamp
timeStamp = Time()
Set myFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set WriteStuff = myFSO.OpenTextFile("symant ec.log", 2, True)
Dim objShell: Set objShell=CreateObject("Wsc ript.Shell ")
Dim startMsg
startMsg = "STARTING SCRIPT, YO!" & vBCrLF & "You do not neet to click OK until it is done." &_
vbcrlf & "When the script is done, it will say DONE, YO! Check symantec.log for results."
objShell.Popup startMsg, 3
'WScript.Echo startMsg
WriteStuff.WriteLine("Star ting Script, yo!" & vbcrlf)
'-------------------- Grab computer names from Computer.txt and store in array ----------------
strComputers = ""
On Error Resume Next
'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computer.txt"
'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
If objFSO.FileExists(g_strHos tFile) Then
Set objTextStream = objFSO.OpenTextFile(g_strH ostFile, FOR_READING)
Else
WScript.Echo "ERROR: Input file " & g_strHostFile & " not found."
WScript.Quit
End If
'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStrea m
readingInComputer= objTextStream.ReadLine
' Wscript.Echo VbCrLf & readingInComputer
strComputers = strComputers + readingInComputer +","
Loop
objTextStream.Close
arrComputers = Split(strComputers , ",")
'------------------------- ---- Symnantec Piece--------------------- ---------- --
'Pre condition: arrComputers must be populated from Computer.txt
'------- Insert all Available Keys Here-------------------
Dim strSymantecKeys
strSymantecKeys="{BA4B71D1 -898E-4306 -AE87-8BA7 A596F0ED}, {5a633ed0- e5d7-4d65- ab8d-53ed4 3510284},{ a011a1dc-7 f1d-4ea8-b d11-0c5f97 18e428},{4 73af7d0-b8 64-4699-99 74-df570c5 b6dce},{a0 11a1dc-7f1 d-4ea8-bd1 1-0c5f9718 e428},{50e 125d1-88e5 -48ce-80ae -98ec9698e 639},{33CF CF98-F8D6- 4549-B469- 6F4295676D 83}"
'------------------------- ---------- ---------- ---------- --
arrSymantecKeys = Split(strSymantecKeys, ",")
For Each strComputer in arrComputers
'On Error Resume Next
' '---- If computername is blank then exit loop ---
' If strComputer = "" Then
' exit for
' End If
'----------- Set Uninstall Password & LockUnloadSvcs Registry Key Values-------
Const HKEY_LOCAL_MACHINE = &H80000002
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv" )
' ----If getObject fails, server is offline or doesn't exist -------------
If err.number <> 0 then
err.clear
objShell.Popup "ERROR: "& strComputer & " is offline or doesn't exist", 2
'wscript.echo "ERROR: "& strComputer & " is offline or doesn't exist"
'write to log file
WriteStuff.WriteLine(timeS tamp & " " & strComputer & " - ERROR! It is offline or doesn't exist.")
On Error GoTo 0
'------ Else GO ahead and remove Registry Keys ---------------------
Else
On Error GoTo 0
strKeyPath = "SOFTWARE\INTEL\LANDesk\Vi rusProtect 6\CurrentV ersion\Adm inistrator Only\Secur ity"
ValueName = "LockUnloadServices"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": LockUnloadServices set to: " & strValue
End If
ValueName = "UseVPUninstallPassword"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": UninstallPW set to: " & strValue
End If
'----- Run MSIEXEC to remove Symantec -------------
For Each strSymantecKey in arrSymantecKeys
'WScript.Echo "psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
Next
'Print to log file
WriteStuff.WriteLine(timeS tamp & " - " & strComputer & " - Symantec Removed.")
objShell.Popup strComputer & " - Symantec Removed", 2
'WScript.Echo strComputer & " - Symantec Removed"
End If
SET objReg=Nothing
Next
'Write to log file and close it
WriteStuff.WriteLine(vbcrl f & "Script is Done, yo! Click OK to close.")
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
'Let user know the script is done!
WScript.Echo "SCRIPT IS DONE, YO!!!"
https://www.experts-exchange.com/questions/22494186/Wscript-VBScript-how-to-write-an-echo-that-disappears-after-a-while-no-OK-or-Confirm-button-like-a-progress-bar.html
I'm creating a script to remotely and silently remove symantec from all computers. It pulls all computer names from "computer.txt" and stores it in an array. Then for each computer, runs code to change registry keys and delete symantec.
I've been testing it out, but the problem is during testing. As a test, I put MY computer name 3 times in the computer.txt file. When I run it, the first time it hits my computer, it looks ok. But anytime after it, it fails, saying computer doesn't exist. This also occurs even if I put 3 different computer names. What's wrong with my error handling? Am I even doing it correctly? So far all the "executable" code is commented out and replaced with alerts. This is how I'm testing it (so I don't mess up the system =P )
Anyone help, please?
'Remove Symantec Remotely and Silently
'Results are stored in a log file: symantec.log
'--------------- Create Log File ----------------
'Open up the path to save the information into a text file
Dim Stuff, myFSO, WriteStuff, timeStamp
timeStamp = Time()
Set myFSO = CreateObject("Scripting.Fi
Set WriteStuff = myFSO.OpenTextFile("symant
Dim objShell: Set objShell=CreateObject("Wsc
Dim startMsg
startMsg = "STARTING SCRIPT, YO!" & vBCrLF & "You do not neet to click OK until it is done." &_
vbcrlf & "When the script is done, it will say DONE, YO! Check symantec.log for results."
objShell.Popup startMsg, 3
'WScript.Echo startMsg
WriteStuff.WriteLine("Star
'-------------------- Grab computer names from Computer.txt and store in array ----------------
strComputers = ""
On Error Resume Next
'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computer.txt"
'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.Fi
If objFSO.FileExists(g_strHos
Set objTextStream = objFSO.OpenTextFile(g_strH
Else
WScript.Echo "ERROR: Input file " & g_strHostFile & " not found."
WScript.Quit
End If
'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStrea
readingInComputer= objTextStream.ReadLine
' Wscript.Echo VbCrLf & readingInComputer
strComputers = strComputers + readingInComputer +","
Loop
objTextStream.Close
arrComputers = Split(strComputers , ",")
'-------------------------
'Pre condition: arrComputers must be populated from Computer.txt
'------- Insert all Available Keys Here-------------------
Dim strSymantecKeys
strSymantecKeys="{BA4B71D1
'-------------------------
arrSymantecKeys = Split(strSymantecKeys, ",")
For Each strComputer in arrComputers
'On Error Resume Next
' '---- If computername is blank then exit loop ---
' If strComputer = "" Then
' exit for
' End If
'----------- Set Uninstall Password & LockUnloadSvcs Registry Key Values-------
Const HKEY_LOCAL_MACHINE = &H80000002
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv"
' ----If getObject fails, server is offline or doesn't exist -------------
If err.number <> 0 then
err.clear
objShell.Popup "ERROR: "& strComputer & " is offline or doesn't exist", 2
'wscript.echo "ERROR: "& strComputer & " is offline or doesn't exist"
'write to log file
WriteStuff.WriteLine(timeS
On Error GoTo 0
'------ Else GO ahead and remove Registry Keys ---------------------
Else
On Error GoTo 0
strKeyPath = "SOFTWARE\INTEL\LANDesk\Vi
ValueName = "LockUnloadServices"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": LockUnloadServices set to: " & strValue
End If
ValueName = "UseVPUninstallPassword"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": UninstallPW set to: " & strValue
End If
'----- Run MSIEXEC to remove Symantec -------------
For Each strSymantecKey in arrSymantecKeys
'WScript.Echo "psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
Next
'Print to log file
WriteStuff.WriteLine(timeS
objShell.Popup strComputer & " - Symantec Removed", 2
'WScript.Echo strComputer & " - Symantec Removed"
End If
SET objReg=Nothing
Next
'Write to log file and close it
WriteStuff.WriteLine(vbcrl
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
'Let user know the script is done!
WScript.Echo "SCRIPT IS DONE, YO!!!"
Try this version...
'--------------- Create Log File ----------------
'Open up the path to save the information into a text file
Dim Stuff, myFSO, WriteStuff, timeStamp
timeStamp = Time()
Set myFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set WriteStuff = myFSO.OpenTextFile("symant ec.log", 2, True) 'should specify the full path for the log...
Dim objShell: Set objShell=CreateObject("Wsc ript.Shell ")
Dim startMsg
startMsg = "STARTING SCRIPT, YO!" & vBCrLF & "You do not neet to click OK until it is done." &_
vbcrlf & "When the script is done, it will say DONE, YO! Check symantec.log for results."
objShell.Popup startMsg, 3
'WScript.Echo startMsg
WriteStuff.WriteLine("Star ting Script, yo!" & vbcrlf)
'-------------------- Grab computer names from Computer.txt and store in array ----------------
'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computer.txt"
'Read computer names for install from text file.
''''removed - you already have an fso reference Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
'------- Insert all Available Keys Here-------------------
Dim strSymantecKeys
strSymantecKeys="{BA4B71D1 -898E-4306 -AE87-8BA7 A596F0ED}, {5a633ed0- e5d7-4d65- ab8d-53ed4 3510284},{ a011a1dc-7 f1d-4ea8-b d11-0c5f97 18e428},{4 73af7d0-b8 64-4699-99 74-df570c5 b6dce},{a0 11a1dc-7f1 d-4ea8-bd1 1-0c5f9718 e428},{50e 125d1-88e5 -48ce-80ae -98ec9698e 639},{33CF CF98-F8D6- 4549-B469- 6F4295676D 83}"
'------------------------- ---------- ---------- ---------- --
arrSymantecKeys = Split(strSymantecKeys, ",")
If myFSO.FileExists(g_strHost File) Then
arrPCs=Split(myFSO.OpenTex tFile(g_st rHostFile) .ReadAll, vbNewLine)
Else
WScript.Echo "ERROR: Input file " & g_strHostFile & " not found."
WScript.Quit
End If
'Loop through list of computers and perform tasks on each.
For Each PC in arrPCs
''' readingInComputer= objTextStream.ReadLine
' Wscript.Echo VbCrLf & readingInComputer
' strComputers = strComputers + readingInComputer +","
'Loop
'objTextStream.Close
'arrComputers = Split(strComputers , ",")
'------------------------- ---- Symnantec Piece--------------------- ---------- --
'Pre condition: arrComputers must be populated from Computer.txt
On Error Resume Next
' '---- If computername is blank then exit loop ---
' If strComputer = "" Then
' exit for
' End If
'----------- Set Uninstall Password & LockUnloadSvcs Registry Key Values-------
Const HKEY_LOCAL_MACHINE = &H80000002
Set objReg = GetObject("winmgmts:\\" & PC & "\root\default:StdRegProv" )
' ----If getObject fails, server is offline or doesn't exist -------------
If err.number <> 0 then
objShell.Popup "ERROR: "& strComputer & " is offline or doesn't exist", 2
'wscript.echo "ERROR: "& strComputer & " is offline or doesn't exist"
'write to log file
WriteStuff.WriteLine(timeS tamp & " " & strComputer & " - ERROR! It is offline or doesn't exist.")
On Error GoTo 0
'------ Else GO ahead and remove Registry Keys ---------------------
Else
On Error GoTo 0
strKeyPath = "SOFTWARE\INTEL\LANDesk\Vi rusProtect 6\CurrentV ersion\Adm inistrator Only\Secur ity"
ValueName = "LockUnloadServices"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": LockUnloadServices set to: " & strValue
End If
ValueName = "UseVPUninstallPassword"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": UninstallPW set to: " & strValue
End If
'----- Run MSIEXEC to remove Symantec -------------
For Each strSymantecKey in arrSymantecKeys
'WScript.Echo "psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
Next
'Print to log file
WriteStuff.WriteLine(timeS tamp & " - " & strComputer & " - Symantec Removed.")
objShell.Popup strComputer & " - Symantec Removed", 2
'WScript.Echo strComputer & " - Symantec Removed"
End If
SET objReg=Nothing
Next
'Write to log file and close it
WriteStuff.WriteLine(vbcrl f & "Script is Done, yo! Click OK to close.")
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
'Let user know the script is done!
WScript.Echo "SCRIPT IS DONE, YO!!!"
'--------------- Create Log File ----------------
'Open up the path to save the information into a text file
Dim Stuff, myFSO, WriteStuff, timeStamp
timeStamp = Time()
Set myFSO = CreateObject("Scripting.Fi
Set WriteStuff = myFSO.OpenTextFile("symant
Dim objShell: Set objShell=CreateObject("Wsc
Dim startMsg
startMsg = "STARTING SCRIPT, YO!" & vBCrLF & "You do not neet to click OK until it is done." &_
vbcrlf & "When the script is done, it will say DONE, YO! Check symantec.log for results."
objShell.Popup startMsg, 3
'WScript.Echo startMsg
WriteStuff.WriteLine("Star
'-------------------- Grab computer names from Computer.txt and store in array ----------------
'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computer.txt"
'Read computer names for install from text file.
''''removed - you already have an fso reference Set objFSO = CreateObject("Scripting.Fi
'------- Insert all Available Keys Here-------------------
Dim strSymantecKeys
strSymantecKeys="{BA4B71D1
'-------------------------
arrSymantecKeys = Split(strSymantecKeys, ",")
If myFSO.FileExists(g_strHost
arrPCs=Split(myFSO.OpenTex
Else
WScript.Echo "ERROR: Input file " & g_strHostFile & " not found."
WScript.Quit
End If
'Loop through list of computers and perform tasks on each.
For Each PC in arrPCs
''' readingInComputer= objTextStream.ReadLine
' Wscript.Echo VbCrLf & readingInComputer
' strComputers = strComputers + readingInComputer +","
'Loop
'objTextStream.Close
'arrComputers = Split(strComputers , ",")
'-------------------------
'Pre condition: arrComputers must be populated from Computer.txt
On Error Resume Next
' '---- If computername is blank then exit loop ---
' If strComputer = "" Then
' exit for
' End If
'----------- Set Uninstall Password & LockUnloadSvcs Registry Key Values-------
Const HKEY_LOCAL_MACHINE = &H80000002
Set objReg = GetObject("winmgmts:\\" & PC & "\root\default:StdRegProv"
' ----If getObject fails, server is offline or doesn't exist -------------
If err.number <> 0 then
objShell.Popup "ERROR: "& strComputer & " is offline or doesn't exist", 2
'wscript.echo "ERROR: "& strComputer & " is offline or doesn't exist"
'write to log file
WriteStuff.WriteLine(timeS
On Error GoTo 0
'------ Else GO ahead and remove Registry Keys ---------------------
Else
On Error GoTo 0
strKeyPath = "SOFTWARE\INTEL\LANDesk\Vi
ValueName = "LockUnloadServices"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": LockUnloadServices set to: " & strValue
End If
ValueName = "UseVPUninstallPassword"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
'objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
'WScript.Echo strComputer & ": UninstallPW set to: " & strValue
End If
'----- Run MSIEXEC to remove Symantec -------------
For Each strSymantecKey in arrSymantecKeys
'WScript.Echo "psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
Next
'Print to log file
WriteStuff.WriteLine(timeS
objShell.Popup strComputer & " - Symantec Removed", 2
'WScript.Echo strComputer & " - Symantec Removed"
End If
SET objReg=Nothing
Next
'Write to log file and close it
WriteStuff.WriteLine(vbcrl
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
'Let user know the script is done!
WScript.Echo "SCRIPT IS DONE, YO!!!"
ASKER
OH! Ok.. I figured out the problem. I started by printing out err.number and then finally it came up with some useful debug code. It pointed to
Const HKEY_LOCAL_MACHINE = &H80000002
which was sitting inside a for loop. I moved it out and looks like it work..
Now I got another problem!
I'm actually trying to run it now.. so this:
WScript.Echo "psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
should now be:
psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
But this doesn't work. How do I incorporate variables in a command like this...??
Const HKEY_LOCAL_MACHINE = &H80000002
which was sitting inside a for loop. I moved it out and looks like it work..
Now I got another problem!
I'm actually trying to run it now.. so this:
WScript.Echo "psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
should now be:
psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & strSymantecKey & " REMOVE=ALL"
But this doesn't work. How do I incorporate variables in a command like this...??
Try this
psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & Replace(Replace(strSymante cKey ,"{",""),"}","") & " REMOVE=ALL"
psexec \\" & strComputer & " MsiExec.exe /norestart /q/x" & Replace(Replace(strSymante
oops, try this
psexec \\" & strComputer & " MsiExec.exe /norestart /q /x " & Replace(Replace(strSymante cKey ,"{",""),"}","") & " REMOVE=ALL"
psexec \\" & strComputer & " MsiExec.exe /norestart /q /x " & Replace(Replace(strSymante
I would say use: (did you change back to strComputer? Should be PC...?)
psexec \\" & strComputer & " MsiExec.exe /norestart /q /x " & chr(34) & strSymantecKey & chr(34) & " REMOVE=ALL"
psexec \\" & strComputer & " MsiExec.exe /norestart /q /x " & chr(34) & strSymantecKey & chr(34) & " REMOVE=ALL"
ASKER
Hmm.. I think the reason it's not working is that I'm incorporating vbscript with command line code..
I'm going to have to look more into this.. =/
I'm going to have to look more into this.. =/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ah ok got it thanks. =) I'm going to post another question related to this script in a sec...
Cool - I'll keep an eye out.
Thanx for the grade! :^)
Thanx for the grade! :^)
ASKER
comp1
comp2
comp3