none
how to call psexec from powershell

    Question

  • I can't get this to work properly without the the initial error,

    PS C:\scripts> psexec \\pc01 java -version
    PsExec.exe :
    At line:1 char:7
    + psexec <<<<  \\pc01 java -version
        + CategoryInfo          : NotSpecified: (:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     

    Tried double quotes etc.

    Note that psexec does actually get called and runs ok despite the error above !

    any ideas ?

    Tuesday, October 08, 2013 2:21 PM

Answers

  • To clarify my somewhat brief answer above, the reason Powershell is generating that error message is because at least some of PSEXEC's output (or the output from the command run by PSEXEC) is being written to the error stream. Powershell sees output to the error stream and thinks that something went wrong so it generates that error. As I said, the trick is to combine the error and output streams with "2>&1", but if you let Powershell redirect the output it still sees the error stream output and still generates the message. If you let cmd.exe handle the combining of the streams Powershell only see output to the normal output stream and doesn't raise objections. The --% operator tells Powershell to ignore everything else on the line and pass it to the executable being called, in this case cmd.exe, which takes it and combines the streams. I hope that clarifies why this works.
    • Marked as answer by EdTech2012 Thursday, October 24, 2013 1:22 PM
    Tuesday, October 08, 2013 9:46 PM
  • Like this:

    start-process -Wait`
                 -PSPath 'c:\temp\PsExec.Exe' `
                 -ArgumentList '\\pc01 java -version' `
                 -RedirectStandardError c:\temp\error.log `
                 -RedirectStandardOutput c:\temp\output.log
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by EdTech2012 Tuesday, October 22, 2013 8:36 AM
    Tuesday, October 08, 2013 10:09 PM
  • Ok thanks all,

    for those of us stuck on powershell 2 so can't use the --% operator this should work

    $output = cmd /s /c "psexec.exe \\$ComputerName java -version 2>&1"
    :-)
    • Marked as answer by EdTech2012 Thursday, October 24, 2013 1:25 PM
    Thursday, October 24, 2013 1:25 PM

All replies

  • try ./psexec

    Dom


    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 / Forefront Client Security / Forefront Identity Manager

    Tuesday, October 08, 2013 2:28 PM
  • I recently had an occasion to call psexec in a script. The trick I think is to get the error stream going to the output stream. I read several helpful posts on it but ended up doing it this way. Using the "--%" operator Powershell basicaly ignores the "2>&1" at the end of the command line so that cmd.exe parses it.

    For example (taken from my script, so modify to suit):

       $output = cmd /c E:\tools\psexec.exe "\\$ComputerName" "C:\$file" --% /s 2>&1

    Give something like that a try.

    HTH

    Tuesday, October 08, 2013 9:08 PM
  • To clarify my somewhat brief answer above, the reason Powershell is generating that error message is because at least some of PSEXEC's output (or the output from the command run by PSEXEC) is being written to the error stream. Powershell sees output to the error stream and thinks that something went wrong so it generates that error. As I said, the trick is to combine the error and output streams with "2>&1", but if you let Powershell redirect the output it still sees the error stream output and still generates the message. If you let cmd.exe handle the combining of the streams Powershell only see output to the normal output stream and doesn't raise objections. The --% operator tells Powershell to ignore everything else on the line and pass it to the executable being called, in this case cmd.exe, which takes it and combines the streams. I hope that clarifies why this works.
    • Marked as answer by EdTech2012 Thursday, October 24, 2013 1:22 PM
    Tuesday, October 08, 2013 9:46 PM
  • Like this:

    start-process -Wait`
                 -PSPath 'c:\temp\PsExec.Exe' `
                 -ArgumentList '\\pc01 java -version' `
                 -RedirectStandardError c:\temp\error.log `
                 -RedirectStandardOutput c:\temp\output.log
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by EdTech2012 Tuesday, October 22, 2013 8:36 AM
    Tuesday, October 08, 2013 10:09 PM
  • Like this:

    start-process -Wait`
                 -PSPath 'c:\temp\PsExec.Exe' `
                 -ArgumentList '\\pc01 java -version' `
                 -RedirectStandardError c:\temp\error.log `
                 -RedirectStandardOutput c:\temp\output.log
    


    ¯\_(ツ)_/¯

    Thanks, this was the only answer that worked for me :)

    Tuesday, October 22, 2013 8:35 AM
  • To clarify my somewhat brief answer above, the reason Powershell is generating that error message is because at least some of PSEXEC's output (or the output from the command run by PSEXEC) is being written to the error stream. Powershell sees output to the error stream and thinks that something went wrong so it generates that error. As I said, the trick is to combine the error and output streams with "2>&1", but if you let Powershell redirect the output it still sees the error stream output and still generates the message. If you let cmd.exe handle the combining of the streams Powershell only see output to the normal output stream and doesn't raise objections. The --% operator tells Powershell to ignore everything else on the line and pass it to the executable being called, in this case cmd.exe, which takes it and combines the streams. I hope that clarifies why this works.

    That is very interesting but I can't find anywhere else referring to the --% operator ? Can you provide any more info / links ? It doesn't appear to work for me :-(
    • Edited by EdTech2012 Tuesday, October 22, 2013 2:14 PM
    Tuesday, October 22, 2013 2:13 PM
  • That is very interesting but I can't find anywhere else referring to the --% operator ? Can you provide any more info / links ? It doesn't appear to work for me :-(

    That operator requires PS 3.0.

    EDIT: Reference: http://technet.microsoft.com/en-us/library/hh847835.aspx

    EDIT2: More info: http://technet.microsoft.com/en-us/library/hh847892%28v=wps.620%29.aspx


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)




    Tuesday, October 22, 2013 2:20 PM
  • Ok thanks all,

    for those of us stuck on powershell 2 so can't use the --% operator this should work

    $output = cmd /s /c "psexec.exe \\$ComputerName java -version 2>&1"
    :-)
    • Marked as answer by EdTech2012 Thursday, October 24, 2013 1:25 PM
    Thursday, October 24, 2013 1:25 PM
  • I have always preferred this because it works in all versions and eliminates most issues that can occur:

    start-process -Wait`
                 -PSPath 'c:\temp\PsExec.Exe' `
                 -ArgumentList '\\pc01 java -version' `
                 -RedirectStandardError c:\temp\error.log `
                 -RedirectStandardOutput c:\temp\output.log
    


    ¯\_(ツ)_/¯

    Thursday, October 24, 2013 2:19 PM