Jump to content
Sign in to follow this  
Tim H.

psexec with XCOPY = Parse Error Code 4

Recommended Posts

From within an AutoIt Script, I'm trying to use psexec with system authority to copy files from one place on a remote machine to another. When I issue the necessary command outside of the script (at a command prompt), the command executes properly. Here's the exact one-line command:

psexec \\vmhartlti01 -s XCOPY "C:\temp\hs\Hyperspace POC.lnk" "C:\Documents and Settings\All Users\Application Data\Landesk\ManagementSuite\Launchpad\Dept Apps\" /Y

When I try to run the exact same command from within an AutoIt Script, an XCOPY parse error 4 flashes up for a millisecond in the console and the command fails. The command appears to be formed properly in the message box popup.

$cmd = 'psexec \\vmhartlti01 -s XCOPY "C:\temp\hs\Hyperspace POC.lnk" "C:\Documents and Settings\All Users\Application Data\Landesk\ManagementSuite\Launchpad\Dept Apps\" /Y'
MsgBox(4096, "test", $cmd)
$XCopyStatus = Run($cmd, "c:\temp")

The Run command returns the XCOPY PID rather than zero, further obfuscating the problem. I've been working on this for a while now and I think I must be missing something. Does anyone see the problem?

Thanks for your help.

Share this post

Link to post
Share on other sites

Also, if it were me I would reformat your string as such:

$cmd = "psexec \\vmhartlti01 -s XCOPY " & Chr(34) & "C:\temp\hs\Hyperspace POC.lnk" & Chr(34) & " " & Chr(34) & "C:\Documents and Settings\All Users\Application Data\Landesk\ManagementSuite\Launchpad\Dept Apps\"  & Chr(34) &  "/Y"

(I hope I did that right) :sweating:

Share this post

Link to post
Share on other sites

Jos: I tried both Shellexec() and "@comspec /c " Shellexec tells me there's no file type associatied in the registry (there isn't for psexec) and adding "@comspec /c " in front results in the same: Parsing error code 4

Tripredacus: I'm not sure what adding the extra quotes (Chr(34)) is meant to do, but I tried it anyway with the same result. I expected to see the extra quotes in the message box, but didn't.

Any other thoughts?

Share this post

Link to post
Share on other sites

Is PSEXEC located in c:Temp?

In other words: When you run the command from the CMD prompt, what is the active directory?

... and what is the script directory?

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
Live for the present,
Dream of the future,
Learn from the past.

Share this post

Link to post
Share on other sites

Jos: I was running the script by double-clicking it on my desktop. While I was playing, I got the command to work by using a target path that contained no spaces and, consequently, fewer quotes. That had me scratching my head since the syntax with the longer target path looked right. Then I saw your reply.

Given your question, I put the script and a copy of psexec.exe in c:temp, opened a command session there, typed in the script's name, hit enter and "presto chango" it worked!

Next, I figured either psexec had to be in the same directory as the script or running the script from a path with spaces in it was causing the problem. So, I deleted the psexec executable from the c:temp directory and tried it again thinking the long target path with spaces might be the problem. But no, It failed again. Lastly I changed the non-test command string in my script to include the full path to the psexec executable like this:

$cmd = 'C:SysinternalsPsExec ' & $ComputerName & ' -s XCOPY ' & '"' & $LocalSource & $aFile[$n] & '" "' & $LocalPath & '" /Y' (hard to read with the mixed quotes and apostrophes)

and double-clicked it on my desktop. That worked too. So, something about relying on psexec being in my path environment was causing the problem. I went and looked at my path environment to find that psexec's directory was not in the path. So, I double-checked to see if there was a copy in either C:Windows or C:WindowsSystem32. There wasn’t. Installing PSTools must put an entry into the Windows registry so the executables are available from anywhere. Apparently though, that's not good enough. Any idea why that would be?

Anyway, problem solved for now. I appreciate your help!

Edited by Tim H.

Share this post

Link to post
Share on other sites

That was indeed what I was going to get at ones you define the directories used.

We often see the mix-up with WorkingDir and "the path to get to the program".

The command looks now indeed logical to me. :)



SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
Live for the present,
Dream of the future,
Learn from the past.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By 31290
      Hi everyone, 
      Hope you are doing fine by the time being :/
      Well, seems that I'm running into an issue while trying to add an Active domain security group to a remote computer that has a space in it.
      I've been searching for quite a while now and seems that my search did not get well. 

      The ideal solution for me would be to use Psexec.exe as I can run it with my admin credentials whereas using WMIServices, I don't know how to launch that with these credentials.
      Here's what I wrote so far:
      $sADGroupName as a space in it and there's nothing I can do about, we have to respect a naming convention but it would be "Admins REMOTEMACHINENAME"
      Func f_AddADGroup2localAdmin() SplashTextOn("", "Adding " & $sADGroupName & " to the Local Administrators Group.", 1000, 100, -1, -1, 33, -1, -1, 700) $sCommand = $sResources & 'PsExec -accepteula \\' & $sServName & ' net localgroup Administrators MYDOMAIN\"' & $sADGroupName & '" /add' RunAsWait($sTechGID, "MYDOMAIN", $sTechPWD, 4, $sCommand, @SW_SHOW) SplashOff() ; f_MoveADObject() End Func What is weird is when I output the $sCommand variable, the space is in here but it seems not to be passed in my psexec command when I run it.
      Another thing I saw is that 4 times out of 10, psexec does not even launch.
      So I was also wondering if there could be another way to add the "spaced" group with my admin credentials on a remote server other than psexec.
      Thanks all in advance for the lights you may provide to me and keep safe!
    • By bmy007ro
      I have this psexec command working fine 
      Local $sMachine = InputBox("Input PC name", "Enter Computer Name")
      psexec \\$sMachine -u $sDomain\$sUserName -p $sPassword  \\NetworkFLD\FILENAME.cmd > C:\Temp\TT\My.log
      I wanted to converted to AutoIT script
      Cloud you help, please !?
    • By Duck
      I'm attempting to capture the output from the command line tool PSEXEC. I'm using AutoIT to run an instance of PSEXEC against a remote PC to audit Local Admins in my environment using net.exe (C:\Windows\System32> net localgroup administrators). However the usual trick I use to capture command line output does not appear to work well with PSEXEC, as the bottom portion of the output is missing from the return. Any ideas or recommendations are greatly appreciated.  
      Here is what I'm working with: 
      ;This script will read from a list of hosts and report who has local admin privileges on the machine #RequireAdmin Global $fileName = @ScriptDir & '\test.txt' ;hostlist, one host per line readHostList() ;Read list of hosts Func readHostList() Local $file = FileOpen($fileName, 0) While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop ConsoleWrite($line & @CRLF) ;MsgBox(0,0,$line) getLocalAdmins($line) WEnd FileClose($file) EndFunc ;run PSEXEC to list local admins Func getLocalAdmins($remotePC) Local $testFile = @ScriptDir &'\test234.txt' FileOpen($testFile, 1) Local $psexec = 'psexec \\' & $remotePC & ' net localgroup administrators' FileWriteLine($testFile, _RunCmd($psexec) ) FileClose($testFile) EndFunc ;Used to return CLI output Func _RunCmd($sCommand) Local $nPid = Run(@Comspec & " /c" & $sCommand, @SystemDir, @SW_Hide, 8), $sRet = "" If @Error then Return "ERROR:" & @ERROR ProcessWait($nPid) While 1 $sRet &= StdoutRead($nPID) If @error Or (Not ProcessExists ($nPid)) Then ExitLoop WEnd Return $sRet EndFunc  
      ## If i manually run the command on the remote PC via PSEXEC I will get the following output: 
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Starting net on PCNAME... on PCNAME...
      net exited on PCNAME with error code 0.
      Alias name     administrators
      Domain\Domain Admins
      Comment        Administrators have complete and unrestricted access to the computer/domain
      The command completed successfully.
      ## The returned output from running the above script is as follows:
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Alias name     administrators
      Connecting to PCNAME...
      Starting PSEXESVC service on PCNAME...
      Connecting with PsExec service on PCName...
      Starting net on PCNAME..
      net exited on PCNAME with error code 0.
      **Note to test this script PSEXEC must be in the system dir or the path in the script changed 
      PSEXEC tool: https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
    • By vindicta
      Hello Everyone,
      Please pardon my noobiness. I am working on backup restore script where a progress bar is required. I have tried using following code which works fine with folders but not really with single file because robocopy
      Func CopyFolder($srcDir, $destDir, $part) ;======> $part shows whats being copied (e.g. Desktop, Documents etc.) If StringRight($srcDir, 1) = "\" Then $srcDir = StringLeft($srcDir, StringLen($srcDir) - 1) If Not FileExists($destDir) Then DirCreate($destDir) $srcSize = DirGetSize($srcDir, $DIR_EXTENDED) $1_percent = $srcSize[0] / 100 $cmd = 'C:\Windows\System32\robocopy.exe "' & $srcDir & '" "' & $destDir & '" /E /Z /XO /XX /FFT /XJ /NDL /NJH /FP /ETA /NP /V /R:10 /W:5 /LOG+:"' & $logFile & '" /XF desktop.ini thumbs.db' Run(@ComSpec & " /c " & $cmd, "", @SW_HIDE) Do Sleep(300) Until ProcessExists("robocopy.exe") ProgressOn($part, $1_percent & "%", "", 0, 0, 16) Do $destSize = DirGetSize($destDir, $DIR_EXTENDED) ProgressSet(Round($destSize[0] / $1_percent, 2), Round($destSize[0] / (1024 * 1024), 2) & " MB of " & Round($srcSize[0] / (1024 * 1024), 2) & " MB copied", Round($destSize[0] / $1_percent, 2) & "%") Sleep(200) Until ProcessExists("robocopy.exe") = 0 ProgressOff() FileWriteLine($logFile, @CRLF & @CRLF) EndFunc ;==>CopyFolder Part of the problem is that I need to generate logs of files being copied. One particular problem is firefox profile directory. the directory is named as oi3jdslk.default or asdwdx.new and I need to copy places.sqlite from this directory.
      Xcopy manages to copy FROM this directory but not TO this directory because of wildcards
      Then I came across this neat script 
      Func CopyFile($fromFile, $todirectory) Local $FOF_RESPOND_YES = 16 Local $FOF_SIMPLEPROGRESS = 256 $winShell = ObjCreate("shell.application") $winShell.namespace($todirectory).CopyHere($fromFile, $FOF_RESPOND_YES) EndFunc ;==>CopyFile It helped me copy other single files but not the one required from firefox profile folder. Here is how I am trying to process that
      $path = @UserProfileDir & "\AppData\Roaming\Mozilla\Firefox\" & $ffDir & "\places.sqlite" CopyFile($path, $BUFolder & "Mozilla") ;===> $BUFolder = "E:\" & @username & " - Backup\" #cs ====================================================== $ffDir is another variable derived from this function. This variable is the gets the default firefox profile to copy places.sqlite from $ffDir is returned as Profiles\xbsen2s.default (Firefox randomizes the profile directory for extra security(?). At least that's what they say. #ce ====================================================== Func FFProfile() Local $iniFile = @UserProfileDir & "\AppData\Roaming\Mozilla\Firefox\profiles.ini" Local $ddNumber Local $ddLine Local $Line If FileExists($iniFile) Then FileOpen($iniFile, 0) For $i = 1 To _FileCountLines($iniFile) $Line = FileReadLine($iniFile, $i) If StringInStr("DEFAULT=1", $Line) Then $ddNumber = $i - 1 ExitLoop ElseIf StringInStr("Name=default", $Line) Then $ddNumber = $i + 2 EndIf Next $ddLine = FileReadLine($iniFile, $ddNumber) $ffDir = StringReplace(StringTrimLeft($ddLine, 5), "/", "\") $ffInstall = 1 Else $ffInstall = 0 EndIf FileClose($iniFile) EndFunc ;==>FFProfile I really like how natural the progress bar from second copy script look like and would like to keep the progress bar uniform across the program. I am willing to learn if anyone can point me in right direction and help me achieve this.

      Any help is highly appreciated!!
    • By WannaBeGut
      I just wrote a script that should copy data into my cloud using xcopy (cmd), but I want the Status Bar I have in my GUI to change it's text like that:
      working. -> working.. -> working... -> working. ...
      I would also like to make a button which interrupts xcopy (simply closing it should do the trick), but I don't know how to call ProcessClose("xcopy.exe") using a button, while I'am waiting for RunWait to finish.
      $command = 'xcopy "' & @DesktopDir & '\text.txt"' & ' "\\ADMIN-CLOUD\private\" /EECHIY' $SW_STATE = @SW_HIDE RunWait(@ComSpec & " /c " & '"' & $command & '"', @DesktopDir, $SW_State) Please tell me if you need any further information! 
  • Create New...