2017-04-17 80 views
0

我无法通过Invoke-Command远程运行以下PowerShell脚本。PowerShell脚本与Invoke-Command不起作用

Invoke-Command从服务器:

Invoke-Command -Session $session -Command { 
    Param($env:FileLocation, $file) 
    & "$env:FileLocation\$file" 
} -ArgumentList ($env:FileLocation, $file) > ./windowsResult.txt 

远程脚本:

$Process = Start-Process "$env:DATABASE_HOME\bin\psql.exe" -ArgumentList "-p "5433" -U "postgres" -d "TESTDB" -f $env:LOC_SCRIPT\DBTestScript.2.sql" -Wait -PassThru 

if ($Process.ExitCode -eq 0) { 
    Write-Host "Success!!!!" 
} else { 
    Write-Host "Error while executing DB Scripts" 
} 

当执行它的工作原理和数据库是越来越成功更新该远程脚本。

但是用Invoke-Command调用它时没有。

有人可以帮助我吗?

+2

请勿使用'env:'限定符作为参数:'{param($ FileLocation,$ file)&“$ FileLocation \ $ file”}' –

+0

我们需要使用env,因为我们不知道放置在远程机器上的脚本的位置。我们正在使用环境变量来读取脚本的位置。 –

+0

是的,在客户端(你仍然应该把'$ env:FileLocation'传递给'ArgumentList'),但是在远程scriptblock中你可以自己定义参数变量的名字。 –

回答

0

为什么不在脚本中注入脚本并远程执行它?

$block={ 

    $Process = Start-Process "$env:DATABASE_HOME\bin\psql.exe" -ArgumentList "-p "5433" -U "postgres" -d "TESTDB" -f $env:LOC_SCRIPT\DBTestScript.2.sql" -Wait -PassThru 

    if ($Process.ExitCode -eq 0) { 
     Write-Host "Success!!!!" 
    } else { 
     Write-Host "Error while executing DB Scripts" 
    } 
} 

Invoke-Command -Session $session -ScriptBlock $block 

如果需要块接受的参数,按照你的语法,但不使用$env:前缀

我希望我的理解正确,你需要达到的目标。