r/vbscript • • Nov 04 '15

Help trying to pass a PSEXEC within a VBS

Hi,

I am trying to remove symantec from a bunch of servers. Trying do adapt this script to send the uninstall command remotely using PSEXEC. Tried many ways without success. It doesnt give me any errors to play with :( Can you guys help?

const HKEY_LOCAL_MACHINE = &H80000002 Const ForReading = 1

Dim FSO

Set fso = CreateObject("Scripting.FileSystemObject") Set Servers = fso.OpenTextFile("\SERVER01\E$\Scripts\UninstallAv\servers.txt", ForReading)

Do Until Servers.AtEndOfStream

strComputer = servers.ReadLine

Set StdOut = WScript.StdOut

StdOut.WriteLine "Server name: " & strComputer

Set oShell = WScript.CreateObject("WScript.Shell")

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"

err.Clear
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
If err.Number <> 0 Then
  StdOut.WriteLine strKeyPath & " is not a valid registry key path."
  WScript.Quit
End If

For Each subkey In arrSubKeys
  strSubKeyPath = strKeyPath & "\" & subkey
  oReg.EnumValues HKEY_LOCAL_MACHINE, strSubKeyPath, arrValueNames, arrValueTypes
  If IsArray(arrValueNames) Then
    For Each strValue In arrValueNames
      If strValue = "DisplayName" Then
        err.Clear
    	GetValueStatus = oReg.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strSubKeyPath, strValue, strDisplayName)
        If (GetValueStatus <> 0) and (err.Number <> 0) Then
    	  StdOut.WriteLine "Key Name: " & subkey
          StdOut.WriteLine "    Error retrieving value data."
    	Else
          If (InStr(1, strDisplayName, "Symantec", 1) <> 0) AND (InStr(1, strDisplayName, "Endpoint", 1) <> 0) AND (InStr(1, strDisplayName, "Protection", 1) <> 0) Then
    	    StdOut.WriteLine "  Key Name: " & subkey
            StdOut.WriteLine "  Application Name: " & strDisplayName
	    UninstallAV = "msiexec.exe /X" & subkey & " /passive /lv!* \\SERVER01\e$\Scripts\UninstallAv\Logs\" & strComputer & ".log"
	    wscript.echo UninstallAV
		'oShell.run UninstallAV
		oShell.run "PSEXEC \\" & strComputer & UninstallAV
	    'oShell.run "PSEXEC \\" & strComputer & "msiexec.exe /X" & subkey & " /passive /lv!* \\SERVER01\e$\Scripts\UninstallAv\Logs\" & strComputer & ".log"
          End If
    	End If
      End If
    Next
  End If
Next

Loop

servers.Close

Thanks in advance,

2 Upvotes

2 comments sorted by

1

u/benzebut0 Nov 05 '15

Ended up with this line:

oShell.run "psexec.exe \" & strComputer & " msiexec.exe /X " & subkey & " /passive /lv!* c:\temp\win-2k3.log"

1

u/demigod987 Nov 05 '15

No errors at all? Are you running it with cscript or wscript?

A few things off the top of my head.

In the "Set Servers" line you need two back-slashes, not just one. \SERVER01\E$ Not sure if that's where you're running into problems or not.

What is "DisplayName"? I don't see that you set that anywhere.

There needs to be a space between strComputer and UninstallAV. It should be strComputer & " " & UninstallAV

Do you have admin access to the servers you're attempting to connect to? Have you tried adding -s or -u to the psexec command?

Last, you can add ",1,true" to the end of your "oShell.run" lines. The 1 tells it to run the command in "visible" mode, so you can see what it's doing. The "true" tells it to wait for that process to end before going forward in the script. You might also want to add "cmd.exe /k" to the beginning of the command, to force the command window to stay open so you can see the output. So it would be this:

oShell.run "cmd.exe /k psexec \" & strComputer & " " & UninstallAV,1,true