首页 > 解决方案 > 在远程计算机上运行 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。

我收到如图所示的错误

在此处输入图像描述

转录:

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.

标签: azurepowershellazure-devopsyamlazure-pipelines

解决方案


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

- 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'

推荐阅读