从powershell命令在远程服务器上执行批处理文件

时间:2019-01-11 14:59:41

标签: powershell batch-file teamcity remote-server

我目前正在尝试实现this问题中提到的功能,但是我没有取得任何成功。我试图在TeamCity服务器上运行此程序以复制一些文件,然后运行批处理文件。机器人复制程序可以正常运行,但是Invoke-Command的执行似乎失败了,但是它并没有以我尝试过的任何方式引发任何错误。

这是我的代码:

$deploymentComputerName = 'server.name.com'
$robocopyPath = 'C:\Windows\System32\robocopy.exe'

try {
  $deploymentShare = "\\" + $deploymentComputerName + "\folder"
  $deploymentPackageDirectory = 'c:\local\route'
  $invokeRoute = "C:\Route\batchFile.bat"

  &$robocopyPath /E /PURGE $deploymentPackageDirectory $deploymentShare /r:0 /XF "LibSassHost.dll" "LibSassHost.Native-64.dll" "libsass.dll"

  Write-Host "Attempting to run it with the FIRST method"
  Invoke-Command -ComputerName $deploymentComputerName -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c '$invokeRoute'"}
  Invoke-Command -ComputerName $deploymentComputerName -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c $invokeRoute"}

  Write-Host "Attempting to run it with the SECOND method - " $invokeRoute
  Invoke-Command -ScriptBlock { "$deploymentShare\batchFile.bat" } -ComputerName $deploymentComputerName

  Write-Host "Finished trying to run the methods"    
} catch {
  Write-Host "Error running powershell file"
}

0 个答案:

没有答案