-1

I want to run an executable on a remote virtual machine. The executable is already on the remote virtual machine. The cmdlet I use is:

$command = "c:\users\<username>\desktop\Myexecutable.exe"

Invoke-Command -ComputerName Machine98 -ScriptBlock {$command} -Credential <domain_name>\<username>

I anonymized the <value> but I am sure they are corrects.

At the command prompt of Powershell, there is no error message. And the prompt becomes accessible after the cmdlet, so it does not hang.

I am sure the service WS-Management is running and is correctly configured on the remote virtual machine because this cmdlet works:

enter-pssession -computername Machine98 -credential <domain_name>\<username>

Can you help me find what is wrong with my solution or propose another way of achieving things?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Smurf
  • 7
  • 4
  • It sounds like there is no error on the machine you are using to execute the command from but there may be one on the remote machine. Is there a log you can look at on it? It might be worth adding `Start-Transcript` In your scriptblock. – Clayton Lewis Sep 18 '18 at 18:34
  • Another good resource for troubleshooting is `Test-Connection`. See `help Test-Connection -Full` for more details. – lit Sep 18 '18 at 19:08

1 Answers1

0

$Command is defined in the local machine and won't be available inside the scriptblock. When you the variable inside a scriptblock, you have to pass it as an argument.

Invoke-Command -Computer Name -ScriptBlock {
    Param(
        [string]$Command
    )
    & $Command
} -ArgumentList $Command

So here, its just executing an empty ScriptBlock(as the variable $Command is null), hence no error.

Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26