locked
Trying to run powershell with PSEXEC RRS feed

  • Question

  • i am trying to run a powershell script from psexec but i keeping getting error saying below..
    also i have to type in exit for it to exit out of this window. i have set the exceution policy to bypass as remotesigned/ Unrestrcited did not work.
    i have also tried to use the psexec \\server cmd /c "echo . | powershell \\remoteserver powershell \\server\script\scriptPs.ps1  \\server\script\inputfile.txt" gives the same response saying
    ...is not recognized as the name of a cmdlet...



    D:\script>psexec \\remoteserver powershell \\server\script\scriptPs.ps1  \\server\script\inputfile.txt

    PsExec v1.98 - Execute processes remotely
    Copyright (C) 2001-2010 Mark Russinovich
    Sysinternals - www.sysinternals.com


    The term '\\server\script\scriptPs.ps1' is not recognized as the name of
     a cmdlet, function, script file, or operable program. Check the spelling of th
    e name, or if a path was included, verify that the path is correct and try agai
    n.
    At line:1 char:36
    + \\server\script\scriptPs.ps1 <<<<  \\server\script\inputfile.txt
        + CategoryInfo          : ObjectNotFound: (\\server\script\scriptPs
       .ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException


     

     

    I can login to the server and do

     powershell \\server\script\scriptPs.ps1  \\server\script\inputfile.txt from command line and it works just fine

    • Edited by CodeSong Tuesday, January 24, 2012 5:03 PM
    Tuesday, January 24, 2012 5:01 PM

Answers

  • Try doing it with a local script, just as a test.  Looks like it might be a credentials issue accessing the remote share where the script lives.
    • Marked as answer by CodeSong Tuesday, January 24, 2012 7:26 PM
    Tuesday, January 24, 2012 6:12 PM

All replies

  • Try using the explicit parameters for the powershell.exe.  They are different from Powershell commands/syntax. Specifically, use -File to parse the script path.  Also, if you don't have the ExecutionPolicy turned down, you'll need this switch: -ExecutionPolicy Bypass.

    PS > powershell.exe -?
    
    PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
        [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]
        [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
        [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
        [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
        [-Command { - | <script-block> [-args <arg-array>]
                      | <string> [<CommandParameters>] } ]
    
    PowerShell[.exe] -Help | -? | /?
    
    -PSConsoleFile
        Loads the specified Windows PowerShell console file. To create a console
        file, use Export-Console in Windows PowerShell.
    
    -Version
        Starts the specified version of Windows PowerShell.
    
    -NoLogo
        Hides the copyright banner at startup.
    
    -NoExit
        Does not exit after running startup commands.
    
    -Sta
        Start the shell using a single-threaded apartment.
    
    -NoProfile
        Does not use the user profile.
    
    -NonInteractive
        Does not present an interactive prompt to the user.
    
    -InputFormat
        Describes the format of data sent to Windows PowerShell. Valid values are
        "Text" (text strings) or "XML" (serialized CLIXML format).
    
    -OutputFormat
        Determines how output from Windows PowerShell is formatted. Valid values
        are "Text" (text strings) or "XML" (serialized CLIXML format).
    
    -WindowStyle
        Sets the window style to Normal, Minimized, Maximized or Hidden.
    
    -EncodedCommand
        Accepts a base-64-encoded string version of a command. Use this parameter
        to submit commands to Windows PowerShell that require complex quotation
        marks or curly braces.
    
    -File
        Execute a script file.
    
    -ExecutionPolicy
        Sets the default execution policy for the session.
    
    -Command
        Executes the specified commands (and any parameters) as though they were
        typed at the Windows PowerShell command prompt, and then exits, unless
        NoExit is specified. The value of Command can be "-", a string. or a
        script block.
    
        If the value of Command is "-", the command text is read from standard
        input.
    
        If the value of Command is a script block, the script block must be enclosed
        in braces ({}). You can specify a script block only when running PowerShell.exe
        in Windows PowerShell. The results of the script block are returned to the
        parent shell as deserialized XML objects, not live objects.
    
        If the value of Command is a string, Command must be the last parameter
        in the command , because any characters typed after the command are
        interpreted as the command arguments.
    
        To write a string that runs a Windows PowerShell command, use the format:
            "& {<command>}"
        where the quotation marks indicate a string and the invoke operator (&)
        causes the command to be executed.
    
    -Help, -?, /?
        Shows this message. If you are typing a PowerShell.exe command in Windows
        PowerShell, prepend the command parameters with a hyphen (-), not a forward
        slash (/). You can use either a hyphen or forward slash in Cmd.exe.
    
    EXAMPLES
        PowerShell -PSConsoleFile SqlSnapIn.Psc1
        PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML
        PowerShell -Command {Get-EventLog -LogName security}
        PowerShell -Command "& {Get-EventLog -LogName security}"
    
        # To use the -EncodedCommand parameter:
        $command = 'dir "c:\program files" '
        $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
        $encodedCommand = [Convert]::ToBase64String($bytes)
        powershell.exe -encodedCommand $encodedCommand
    


    Tuesday, January 24, 2012 5:20 PM
  • i tried to do this

    D:\script>psexec \\remoteserver  cmd /c powershell -File \\server\script\scriptPs.ps1 \\server\script\inputfile.txt -ExecutionPolicy Bypass

    PsExec v1.98 - Execute processes remotely
    Copyright (C) 2001-2010 Mark Russinovich
    Sysinternals - www.sysinternals.com


    The argument '\\server\script\scriptPs.ps1' to the -File parameter does n
    ot exist. Provide the path to an existing '.ps1' file as an argument to the -Fil
    e parameter.
    cmd exited on remoteserver  with error code -196608.

    D:\script>

     

    same msg without cmd /c as well.


    • Edited by CodeSong Tuesday, January 24, 2012 5:38 PM
    Tuesday, January 24, 2012 5:37 PM
  • I'm not sure exactly where your example is going wrong, but here's what works for me:

    PS C:\> .\psexec.exe \\computername -u domain\remoteuser -p password cmd.exe /c 'echo . | powershell.exe -file "c:\users\remoteuser\myscript.ps1" '

    Thanks,
    -Lincoln

     


    Tuesday, January 24, 2012 5:42 PM
  • i have to pass another argument to the PS script and the example you game above is not working correctly.
    Tuesday, January 24, 2012 6:01 PM
  • It works fine when passing parameters, too.  Are you running psexec from a cmd prompt or a powershell prompt?  My example is from a powershell prompt.  Making sure the quoting is the same is important.  Here's how I passed a parameter, note that everything after cmd.exe /c is all a single string:

    PS C:\>.\psexec.exe \\computername -u domain\remoteuser -p password cmd.exe /c 'echo . | powershell.exe -file c:\users\remoteuser\script.ps1 -folder c:\users'

     

    The contents of script.ps1 is

    param($folder)
    dir $folder


    Thanks,
    -Lincoln

    • Proposed as answer by jcmoorman Tuesday, October 1, 2019 9:34 PM
    Tuesday, January 24, 2012 6:10 PM
  • Try doing it with a local script, just as a test.  Looks like it might be a credentials issue accessing the remote share where the script lives.
    • Marked as answer by CodeSong Tuesday, January 24, 2012 7:26 PM
    Tuesday, January 24, 2012 6:12 PM
  • the permission on network share is correct because i can run the same command without psexec on remote machine by logging in.

    i was runing the psexec from command line, i tired from Powershell window like you had given an example and i get the same

     

     

     The argument '\\server\script\script.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
    cmd.exe exited on remoteserver with error code -196608.

    Tuesday, January 24, 2012 6:33 PM
  • Please try it with a simple local script in the user's documents folder, and post the result.  If it works, then you know it's some access issue.  If it doesn't work, then at least you know for sure that's not the problem.

    Thanks,
    -Lincoln


    Edit:  And at least for now, run from powershell prompt, so we know exactly the environment.  Plus the quoting is easier to understand than cmd.
    Tuesday, January 24, 2012 6:37 PM
  •  

     

     

    created a script file D:\script\script.ps1 on remoteserver with

    param($folder)
    dir $folder

     

    on my server where i execute the psexec from


    D:\script>.\psexec.exe \\192.168.*.*  cmd.exe /c 'echo . | powershell.exe -file D:\script\script.ps1 -folder c:\users
    '

    PsExec v1.98 - Execute processes remotely
    Copyright (C) 2001-2010 Mark Russinovich
    Sysinternals - www.sysinternals.com

    Connecting to 192.168.*.*...The argument 'D:\script\script.ps1' to the -File parameter does not exist. Provide the pa
    th to an existing '.ps1' file as an argument to the -File parameter.
    Starting cmd.exe on 192.168.*.*....168.*.*...
    ''echo' is not recognized as an internal or external command,
    operable program or batch file.
    cmd.exe exited on 192.168.*.* with error code 1.

    D:\script>

    Tuesday, January 24, 2012 6:46 PM
  • i ran this again and now i get

     

    PS D:\script> .\psexec.exe \\192.168.*.*  cmd.exe /c 'echo . | powershell.exe -file D:\script\script.ps1 -folder c:\users'
    Program 'PsExec.exe' failed to execute: %1 is not a valid Win32 application
    At line:1 char:13
    + .\psexec.exe <<<<  \\192.168.*.*  cmd.exe /c 'echo . | powershell.exe -file D:\script\script.ps1 -folder c:\users'
    .
    At line:1 char:1
    +  <<<< .\psexec.exe \\192.168.*.*  cmd.exe /c 'echo . | powershell.exe -file D:\script\script.ps1 -folder c:\users'
        + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
        + FullyQualifiedErrorId : NativeCommandFailed

    PS D:\script>

    Tuesday, January 24, 2012 6:50 PM
  • i downloaded the lateset psexec and ran it again it it worked with local script, when i tried to use remote scritp it failed with the same _File paramter again
    Tuesday, January 24, 2012 7:12 PM
  • Maybe this was already addressed, but, if you are running PSExec, you don't need the UNC path to the file.  It's local.  Try using the local path instead of UNC for the remote script.  

    Secondary idea: have you tried Invoke-Command or a new PSSession?

    Tuesday, January 24, 2012 7:13 PM
  • it was a permission issue i ran it with a domain admin permission to get it to work. i was running it with my id and my id had ful control of that share , but not sure what was the issue.
    Tuesday, January 24, 2012 7:26 PM
  • Ok, I should have found this earlier.  It explains your issue: http://forum.sysinternals.com/psexec-remote-share-problem_topic5072.html

     

    To get access to network resources you need explicit logon via -u -p, even if that account is the same as the one running psexec

    Thanks,
    -Lincoln

    Tuesday, January 24, 2012 8:38 PM
  • I was getting the same error message when trying to run my script... And i thought lotus domino was bad with the absolutely useless error messages.  :)

    admin

    Monday, April 30, 2012 5:37 AM
  • I tried running the below and it worked

    for /F %i in (fewboxes.txt) do start psexec \\%i -s -h -i powershell d:\data\peo\somescript.ps1

    the for part is to send it psexec to multiple server which will run the powershell script passed

    The flag that really made the difference was the -s flag

    Thursday, August 4, 2016 1:48 AM