Sign in to follow this  
Followers 0
Tigerweld

Using PSExec

13 posts in this topic

Can anyone tell me why this won't run?  I know I have to be close to makeing it work.

$ipAddress = InputBox("IP or Host", "Enter IP or Hostname","","")
$sRemoteLocalPath = "c:\temp\"
$LocalFile = "uninstall_java.bat"

Run('PsExec \\' & $ipAddress & ' "' & $sRemoteLocalPath & $LocalFile & '"', '', @SW_HIDE)

Share this post


Link to post
Share on other sites



Tigerweld, can you please provide a little more insight as to what you mean by "won't run"? The code seems to be working just fine for me; you simply did not provide a way to capture the return from psexec. Is that what you are after, or are you receiving an error?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

After I enter the ip address for the input box nothing happens.  No error or anything.  Running the script from a windows 2008 server on a windows 7 workstation with domain admin rights.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

PSExec is a command line tool. If you want some visual representation of what it is doing, you have to code that in. Try this to illustrate what I mean:

$ipAddress = InputBox("IP or Host", "Enter IP or Hostname","","")
$sRemoteLocalPath = "c:\temp\"
$LocalFile = "uninstall_java.bat"

Run('C:\PsExec.exe \\' & $ipAddress & ' ' & $sRemoteLocalPath & $LocalFile & ' > C:\Temp\My.log', "", @SW_HIDE)

Notice that it outputs to a log file - you'll find this in the temp directory on the machine you're connecting to. You can choose another location for the log file, but this should illustrate what I'm getting at.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

It still isn't working.  I can run just the psexec from a comand prompt and it works, but running psexec inside Autoit does not work.  Maybe it cannot be done.  Thanks for helping.

Share this post


Link to post
Share on other sites

It can be done. The script that I provided works for me. I would suggest you begin by removing the variables from your script. Something like this:

Run('C:\PsExec.exe \\10.10.10.1 C:\Temp\Uninstall_java.bat > C:\Temp\My.log', "", @SW_SHOW)

If that works, it has something to do with the way you're dropping your variables in there.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

What do you get if you don't use @SW_HIDE and use @ComSpec /k?

Run(@ComSpec & ' /k C:\PsExec.exe \\' & $ipAddress & ' ' & $sRemoteLocalPath & $LocalFile & ' > C:\Temp\My.log')

Never use @SW_HIDE until you know the command is working the way you want it to. Otherwise, you're just guessing as to what is happening.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

JLogan, that works when I do that.  So how can I add my variables?  Is there a way to place the path in just one varible and use that?

Share this post


Link to post
Share on other sites

BrewMan it gives me a 'psexec.exe' is not recognized as an internal or external command.

Share this post


Link to post
Share on other sites

Use the full path to where the program is located, that was an example.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Add the variables in one at a time to figure out which one is causing you the problem. Go from this:

1.Run('C:\PsExec.exe \\10.10.10.1 C:\Temp\Uninstall_java.bat > C:\Temp\My.log', "", @SW_SHOW)

To this:

1.Run('C:\PsExec.exe \\' & $ipAddress & ' C:\Temp\Uninstall_java.bat > C:\Temp\My.log', "", @SW_SHOW)

If this works, add the next variable:

1.Run('C:\PsExec.exe \\' & $ipAddress & ' ' & $sRemoeteLocalPath & 'Uninstall_java.bat > C:\Temp\My.log', "", @SW_SHOW)

It's called troubleshooting ;)

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I also like to do logging of the run command, just before the execution, so I can check it's syntax.

ConsoleWrite('C:\PsExec.exe \\' & $ipAddress & ' ' & $sRemoeteLocalPath & 'Uninstall_java.bat > C:\Temp\My.log' & @CRLF)

Usually, something small, like forgetting a "", or omitting a space, somewhere.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Same here, jdelaney. I typically do it in a MsgBox, but same concept. Can't count the number of times I have banged my head against my desk over what turned out to be one too many or one too few spaces in a line.


√-1 2^3 ∑ π, and it was delicious!

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  
Followers 0

  • Similar Content

    • Duck
      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.
      -------------------------------------------------------------------------------
      admin
      Administrator
      Alias name     administrators
      Domain\Domain Admins
      Comment        Administrators have complete and unrestricted access to the computer/domain
      Members
      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
    • ur
      By ur
      I have enabled admin sharing in the remote machine so that I can copy files to the remote machine use PSEXEC to execute our application remotely.
        Now, if the machine is poweredoff we need to poweriton. So I checked whether the remote machine, it is of both VirtualBox or VMWare one. For VirtualBox or VMware, I have found command line utities to poweron.   But to autologon by skipping the login screen.Can anyone suggest how to do the same.If it is a physical machine, then there is no need to poweron but autologon is required.   I am trying for windows machines.Win 7 at present.   For VirtualBox I checked below link with help og guestadditions but not working. http://www.virtualbox.org/manual/ch09.html#autologon   The below procedure will save the password in that machine and it will logon automatically.   http://www.howtogeek.com/112919/how-to-make-your-windows-8-computer-logon-automatically/   But it will give access to everyne.But my opinion is he should access only if have credentias.I mean through any commandline from another machine he should trigger logon also by passing credentials.     Similarly this also   http://www.cnet.com/how-to/automatically-log-in-to-your-windows-10-pc/   http://superuser.com/questions/28647/how-do-i-enable-automatic-logon-in-windows-7-when-im-on-a-domain   http://pcsupport.about.com/od/windows7/ht/auto-logon-windows-7.htm   http://www.computerperformance.co.uk/windows7/windows7_auto_logon.htm   https://technet.microsoft.com/en-us/magazine/ee872306.aspx   Please guide me how to proceed.
    • parrishjason
      By parrishjason
      I am trying to spawn a cmd.exe shell on a remote machine using psexec then proceed to running commands on that machine and reading the output. I.e. running pwd. 

      Unfortunately, the code I have now will just immediately exit cmd on the remote system 
      I'm trying to use the current code
      #include <Constants.au3> $pid = Run('C:\Users\test\Desktop\psexec.exe \\192.168.1.123 -u test -p "P@$$word1" -h -s cmd',@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) StdinWrite($pid,"pwd") StdinWrite($pid,@CRLF) Local $data Sleep(2000) $data &= StdoutRead($pid) ConsoleWrite("Debug:" & $data & @LF) StdinWrite($pid,"cd ") StdinWrite($pid,"C:\users\test2") StdinWrite($pid,@CRLF) StdinWrite($pid) $data &= StdoutRead($pid) ConsoleWrite("Debug:" & $data & @LF) http://stackoverflow.com/questions/19206834/command-prompt-and-autoit-stdinwrite <- credits to this stack overflow post
      Unfortunately, on my end, my cmd just starts/stops with this prompt
       
      Connecting with PsExec service on 192.1.123...Starting cmd on 192.168.1.123... cmd exited on 192.168.1.123 with error code 0. Any ideas how I can keep my shell open over psexec and still interact with it using AutoIT?

      Any feed back would be amazing! Thanks!
    • ur
      By ur
      When I run a program in remote vm virtualbix machine windows 7 64 bit with psexec from my current machine.   It is working fine in system context. C:\Users\kirud01>"C:\Software\application packaging\PsTools\PsExec.exe" -s -i -d "\\erwin-pc" -c -f "C:\Build\delete.exe"   But when I run the same in user context i.e., without -s parameter. The screen is getting freezed in the remote machine. Could you please help me on this. If possible any alternatives for PSEXEC in AutoIT code itself.
    • MikhsilSV
      By MikhsilSV
      script.au3:
      RunWait("\\Server\share\SomeApp.exe") WinWaitActive("Test click inactive desktop", "Button&2") Send("!2")  
      On admin machine executing:
      psexec \\RemotePC -s \\server\share\script.exe Buttons remains unclicked
      edit: by running usual way fails too