远程执行命令 - 无等待

问题描述:

这是代码:远程执行命令 - 无等待

$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 
} 

我想援引

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

远程服务器$fdt_server上,而不是等待它完成。基本上,我希望它只是运行它,并继续前进。

我该如何做到这一点?我尝试使用-AsJob,但似乎没有工作。

+0

尝试启动进程,该命令有一个等待参数。我想当你不使用它时,脚本将继续运行 – guiwhatsthat

+0

@guiwhatsthat,我试过Start-Command,它在远程计算机中暂时启动了java进程,然后关闭,Powershell脚本在本地计算机中退出。我想让Powershell退出,但是通过PS脚本远程启动后,Java程序会在远程计算机上运行。我也尝试使用Invoke-Command -Session,也不能工作:( – iKnowNothing

使用Invoke-WmiMethod会有诀窍!