Search code examples
powershellcommand-lineinvoke-command

Remote Execute command - No Wait


This is the code:

$remotelastexitcode = Invoke-Command -ComputerName $fdt_server -Credential $Cred -ScriptBlock {
    Param($Rjava_path, $Rfdt_path, $Rfdt_log_folder, $Rfdt_log_filename)

    Invoke-Expression -Command:"cmd.exe /c 'start /B $Rjava_path -jar $Rfdt_path -S 1>> $Rfdt_log_folder\$Rfdt_log_filename.txt 2>>&1 & exit 0'";

    $LASTEXITCODE
} -ArgumentList $java_path, $fdt_path, $fdt_log_folder, $fdt_log_filename -ErrorAction Stop

# Force the result to an array, and then test just the last line, which will be
# the last exit code. Anything echoed before that will be ignored.
if (@($remotelastexitcode)[-1] -ne 0) {
    exit 1
}

I would like to invoke

cmd.exe /c 'start /B $Rjava_path -jar $Rfdt_path -S 1>> $Rfdt_log_folder\$Rfdt_log_filename.txt 2>>&1'

on the remote server $fdt_server, but not to wait for it to finish. Basically, I want it to just run it and keep going.

How can I achieve this? I tried using -AsJob but does not seem to work.


Solution

  • Using Invoke-WmiMethod does the trick!