在远程计算机上运行 PowerShell 脚本在 Azure Devops 中不起作用

问题描述

我正在尝试通过 ssh 在远程机器上运行 Powershell 脚本。我已经配置了 Azure 版本,但出现了一个非常奇怪的错误

这是我的配置文件的样子

steps:
- task: SSH@0
  displayName: 'Run shell script on remote machine'
  inputs:
    sshEndpoint: 'Dev SSH service'
    failOnStdErr: false
    runoptions: script
    scriptPath: '$(Build.sourcesDirectory)/Pipelines/Scenarios/test.ps1'
    readyTimeout: '20000'

这是我的 Powershell 脚本的样子:

Write-Host "Hello,World!"

远程计算机通过 PowerShell 配置了 ssh。

我收到如图所示的错误

enter image description here

转录:

tr -d '\015' <./test.ps1> ./test.ps1._unix

##[error]At line:1 char:14

##[error]+ tr -d '\015' <./test.ps1> ./test.ps1._unix

##[error]The '<' operator is reserved for future use.

##[error]    + CategoryInfo          : ParserError: (:) [],ParentContainsErrorRecordException

##[error]    + FullyQualifiedErrorId : RedirectionNotSupported

##[error]Error: Command tr -d '\015' <./test.ps1> ./test.ps1._unix exited with code 1.

解决方法

我解决了这个问题。我创建了一个 System.Debug 变量。我看到了 SSH0 是如何工作的,最后我只是将脚本复制到远程机器并通过设置 Run = commands 来执行它。

- task: CopyFilesOverSSH@0
  displayName: 'Copy scripts to on remote machine'
  inputs:
     sshEndpoint: 'Dev SSH service'
     sourceFolder: '$(Build.SourcesDirectory)\Pipelines\Scenarios'
     contents: '**.ps1' 
     targetFolder:  'C:\Test\Scenarios'
     cleanTargetFolder: false
     overwrite: true
     failOnEmptySource: false
     flattenFolders: true

 - task: SSH@0
   inputs:
   sshEndpoint: 'Dev SSH service'
   runOptions: 'commands'
   commands: 'C:\Test\Scenarios\test.ps1'
   readyTimeout: '20000'

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...