2

Running this command

    $processesToSpawn = 3

    for($i= 1; $i -le $processesToSpawn; $i++){

        psexec \\computername -i 2 -u "username" -p "password" "c:\myapp.exe"
    }

Produces the following error

    psexec : 
At C:\mypsScript.ps1:75 char:13
+             psexec \\computername  -i 2 -u "username" -p " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

It executes and runs the first iteration of the loop fine, and opens the program that i want in the RDP session, but fails immediately after the first iteration.

Edit: The is only happens in the IDE, running the script from a PS command line runs fine. So consider this question solved.

Additionally, i had to add the -d switch to the PSExec command (Don't wait for process to terminate (non-interactive).)

MrBliz
  • 165
  • 1
  • 6
  • `Invoke-Command` doesn't work? – Colyn1337 Nov 16 '15 at 14:30
  • I needed it to run on the remote desktop, under the context of the remote desktop user, so no it doesn't for that scenario. I've solved it now. – MrBliz Nov 16 '15 at 14:32
  • 1
    `Invoke-Command` can work with other user credentials. Check out the `-Credential` parameter and `Get-Credential` cmdlet. – Colyn1337 Nov 16 '15 at 14:35
  • Yes i know it can, but to my knowledge it can't execute a program to run on the desktop of another computer. – MrBliz Nov 16 '15 at 14:37
  • 1
    I think you're referencing a `Session`. Invoke-Command uses a remote session to operate in. It's bad juju from a legal and compliance stand point to be executing programs and operations in another users session with their credentials. Even for service accounts (which should never be used for an interactive logon). – Colyn1337 Nov 16 '15 at 14:40
  • 1
    Sadly, it's the way the company i'm working for rolls. Multiple people logon to a remote desktop using the same creds, when they log on, they're logging on because they want to see this particular process running in a console window on that machine. I am indeed referencing a session. – MrBliz Nov 16 '15 at 14:44

2 Answers2

0

I believe the PSEXEC call in your script needs to end with the /c, I use the script as below.

C:\PSTools\psexec /accepteula -u domain\user -p psexec \\10.0.0.xxx cmd.exe /c "E:\teste\psexec\xxxx.bat"

Hope this helps!

0

Probably, the problem is that psexec does not wait for your process/app to complete.

Could you try the following code to see if it works?

psexec \\computername -i 2 -u "username" -p "password" cmd.exe /c "c:\myapp.exe"

(see http://forum.sysinternals.com/psexec-finishes-before-child_topic27463.html)

Andrey Sapegin
  • 1,201
  • 2
  • 12
  • 27