[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 815
  • Last Modified:

Copying files to a remote machine using Psexe

I have a script I'm building, but slowly coming along, where I want to copy a vbs file to say c:\windows\system32 on around 4 remote servers (for starters) and then use Psexec to launch the vbscript.
I was trying to copy a file using the -c switch but I think I'm confused on that one. None the less, I attempd to use Psexec to do that because I haven't used the Copy methods within VBScript along with passing in Credentials (i.e. Set objLoc = WbemScripting.SWbemLocator, etc, etc.) to connect to the Server and make that copy that way. Any ideas on how to do that with a CopyFile Method? My goal is to uninstall Symantec AntVirus via two scripts. This and the one I'm trying to copy. The script below is work in progress so bare with me.
Thanks for your help Experts,
Wally
Option Explicit

Dim objFSO, objShell, objLocator, objWMIService, objRegService, objRegistry
Dim objInputFile, objOutputFile
Dim strDst, strSrc, strScriptPath, strServer, strServers, strFilePath
Dim strDomain, strUsername, strPassword

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

strScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
strServers = WScript.Arguments.Item(0)
strFilePath = strScriptPath & "\" & strServers

strSrc = strScriptPath & "\" & "Uninstall_SAV.vbs"

Set objInputFile = objFSO.OpenTextFile(strFilePath)
Set objOutputFile = objFSO.OpenTextFile(strScriptPath & "\" & "SAVCE_Uninstall_Results.csv", 2, True)

'Set objRegService = objLocator.ConnectServer(strServer, "root\default", strDomain & "\" & strUsername, strPassword)
'Set objRegistry = objRegService.Get("StdRegProv")

If IsEmpty(strServers)Then
	WScript.Echo "###########################################################"
	WScript.Echo "#     Usage: Init_SAVCE_Uninstall.vbs ServerList.txt      #"
	WScript.Echo "#                                                         #"
	WScript.Echo "#     Important: You need to run this VBScript and        #"
	WScript.Echo "#                your serverlist.txt file from the        #"
	WScript.Echo "#                the same Directory path.                 #"
	WScript.Echo "#                                                         #"
	WScript.Echo "###########################################################"
	WScript.Quit
End If

strDomain = InputBox("Please enter your Domain name: ")
strUsername = InputBox("Please enter your Username: ")
strPassword = InputBox("Please enter your Password: ")

'Set objLocator = CreateObject("WbemScripting.SWbemLocator")
'Set objWMIService = objLocator.ConnectServer(strServer, "root\cimv2", strDomain & "\" & strUsername, strPassword)

Do While objInputFile.AtEndOfLine <> True
	strServer = ""
	strServer = objInputFile.ReadLine
	strDst = "\\" & strServer & "\c$\Windows\System32"
	
	If objFSO.FileExists(strSrc) Then
	WScript.Echo "Cmd /c C:\Psexec -accepteula \\" & strServer & " -u " & strDomain & "\" & strUsername & _
						" -p " & strPassword & " -c " & "cmd Copy " & strSrc & " " & strDst
	objShell.Run "Cmd /c C:\Psexec -accepteula \\" & strServer & " -u " & strDomain & "\" & strUsername & _
						" -p " & strPassword & " -c " & strSrc, True
'		objShell.Run "C:\Psexec \\" & strServer & " -u " & strDomain & "\" & strUsername & _
'						" -p " & strPassword & " -c " & "cmd " & strSrc, True
		'objFSO.CopyFile strSrc, strDst, True
	Else
		WScript.Echo "Source file that needs to be copied is missing."
		WScript.Quit
	End If
Loop

Open in new window

0
wally_davis
Asked:
wally_davis
  • 5
  • 4
1 Solution
 
RobSampsonCommented:
Hi Wally,

You shouldn't need to copy the source script to the remote servers.

If you made strSrc a network location that your specified account has access to, you should be able to just use:
      strSrc = "\\server\share\secondscript.vbs"
      ' .....other stuff in between
      objShell.Run "cmd /c C:\psexec -accepteula -e -u " & strDomain & "\" & strUsername & _
                                    "\\" & strServer & " wscript " & strSrc, 1, True

The short answer though, is that you cannot use credentials in the File operations.  You would need to run the script itself in an alternate profile to perform those tasks.

Regards,

Rob .
0
 
wally_davisAuthor Commented:
Hey Rob. Can't thank you enough for all your help. PSExec in my opinion is a very strange little program.
I ask it to run a VBScript on the Remote Server and I can see the wscript.exe in the "Processes" tab under the Task Manager. However, this is a simple test to just run the script which has one line --> MsgBox "TESTING TESTING" but it never runs the WScript. Now, here's the line of code I run from the Cmd shell as a test but it just hangs.
--> C:\>psexec \\reta2003x32 -accepteula -e -u domain\username -p Password -i 0 wscript  uninstall_savce.vbs //nologo
So, I kill it from the other side (Remote Server) and then back on the Client side where I run Psexec it shows that "WScript exited on SOMESERVER with error code 1. Which of course is what I expected.
It's obvious it's hanging for some reason and I think you'll probably be able to pinpoint it. I just wish that Mr. Russonovich Wrote a better Support Manual to show you more detail on the inner workings.

0
 
wally_davisAuthor Commented:
FYI, I've tried numerous variations of the command. Here's another one:

C:\>psexec \\SERVERA -accepteula -e -u domain\username -p pass -i 0 wscript c:\Uninstall_SavCe.vbs
wscript exited on SERVERA with error code 1.

C:\>psexec -accepteula -e -u domain\username -p pass-i 0 \\SERVERA wscript c:\Uninstall_SavCe.vbs
wscript exited on SERVERA  with error code 1.

C:\>psexec -accepteula -e -u domain\username -p pass -i \\SERVERA wscript c:\Uninstall_SavCe.vbs
wscript exited on SERVERA  with error code 1.

C:\>psexec -accepteula -e -u domain\username -p pass \\SERVERA wscript c:\Uninstall_SavCe.vbs
wscript exited on SERVERA with error code 1.

C:\>psexec \\SERVERA -accepteula -e -u domain\username -p pass wscript c:\Uninstall_SavCe.vbs

C:\>psexec \\SERVERA -accepteula -e -u domain\username -p pass "wscript Uninstall_SavCe.vbs"
PsExec could not start wscript Uninstall_SavCe.vbs on SERVERA:
The system cannot find the file specified.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
RobSampsonCommented:
Hi, why -i 0?

Just -i should do it.

psexec -accepteula -e -i -u domain\username -p pass \\SERVERA wscript c:\Uninstall_SavCe.vbs

If that doesn't work interactively because the profile is not being loaded, try

psexec -accepteula -i -u domain\username -p pass \\SERVERA wscript c:\Uninstall_SavCe.vbs

The -i is definately what you need though, that is the interactive switch.

Regards,

Rob.
0
 
wally_davisAuthor Commented:
Hi Rob. Back again. I'm really trying to figure out what to do with this issue where "Psexec" hangs when I attempt to use the interactive -i switch. I have some long code I want to execute but than I ran across this article about stopping and deleting the PSEXESVC and psexesvc.exe file. I also noticed that this file has the same date/time stamp of the time I ran PSEXEC file. I don't want to delete it if its needed. That file is on my four (4) test Servers c:\windows directory. Is this file getting created when I run the psexec.exe file and does it cause issues whereby it prevents the Remote Session (psexec) from ending?
Question 2: What file types will the PSEXEC file process?
Question 3:  am I having problems because I'm trying just to execute DOS Internal commands? i.e. Dir, Cls, etc. And if so, what is a good test to verify that Psexec will execute and exit properly with a Code of 0?
Question 4: upon completion of execution, I can assume that an Exit code of 0 or 1 should be output to the screen correct?
-----------------------------------
Here's the line of code I now wish to run but I just want to start out with #1 as a test and then progress to #2.
# 1 - psexec \\server -i cmd "dir c:\"
------------------------------------
I want to execute this command to remove Symantec CE from all of our servers.
# 2 -       objShell.Run "cmd /C " & strPsExecPath & " \\" & strServer & " -accepteula " & " -i -u " & _
                                     strDomain & "\" & strUsername & " -p " & strPassword & " MsiExec.exe /norestart /q /x " & strSavSubKey & " REMOVE=ALL" , 1, True
0
 
RobSampsonCommented:
Hi, first off, I'm pretty sure PSExeSvc.exe gets created on the remote machine by PSExec automatically, but I haven't looked into it.  You could test that by renaming it, then seeing is PSExec recreates it.

PSExec will process any command, as long as that command has been tested to work.

When executing DOS commands (those that are DOS operable only) you must get PSExec to execute the DOS prompt, by preceding the command with cmd /c to close the DOS box after the command, or cmd /k to leave the DOS box open.

For example, this:
psexec \\server -i cmd "dir c:\"

should be
psexec -i -u domain\user -p password -i -accepteula \\server cmd /k "dir c:\"

Note that I have had issues when placing the parameters AFTER the server name.  The way that works most reliable is
psexec <parameters> <server> <cmd>

The second command should be the following, as long as strPsExecPath and strSavSubKey do NOT have spaces in them:
objShell.Run "cmd /C " & strPsExecPath & " -accepteula -i -u " & _
                                     strDomain & "\" & strUsername & " -p " & strPassword & " \\" & strServer & " MsiExec.exe /x " & strSavSubKey & " REMOVE=ALL /norestart /qn" , 1, True

Of course, you should test whether the command works at the command line first.  If you use this, you can get the command that would be executed by VBScript in an InputBox, from which you can copy and paste it to the command line (minus the first cmd /c bit).

strCommand = "cmd /C " & strPsExecPath & " -accepteula -i -u " & strDomain & "\" & strUsername & " -p " & strPassword & " \\" & strServer & " MsiExec.exe /x " & strSavSubKey & " REMOVE=ALL /norestart /qn"
InputBox "Title", "Prompt", strCommand

Once that input box appears, copy and paste that command into a DOS prompt, and see if it is correct.

Regards,

Rob.
0
 
wally_davisAuthor Commented:
Rob, you got me on the right track and finally I've got something that works. This is the syntax I have to run the Uninstall from remote servers.

errLevel = objShell.Run("cmd /c " & strPsExecPath & "\\" & strServer & _
        " -accepteula " & " -i -u " & strDomain & "\" & strUsername & _
      " -p " & strPassword & " MsiExec.exe /norestart /v c:\WF_SAV_Uninstall /q/x " & _ strSavSubKey & " REMOVE=ALL" , 1, True)
0
 
wally_davisAuthor Commented:
Rob, you got me on the right track and finally I've got something that works. This is the syntax I have to run the Uninstall from remote servers. Thank you for walking me through the process.
0
 
RobSampsonCommented:
Great. Thanks for letting me know. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now