Azure DevOps SSH@0任务连接Windows主机报错:无法切换至/home目录
解决Azure DevOps连接Windows远程主机的SSH/WinRM问题
一、修复SSH@0任务的/home目录错误
SSH@0任务默认适配Linux环境,连接Windows SSH服务器时会默认查找Linux的/home目录,导致报错。解决方法是指定Windows端的shell并切换到正确的用户目录:
- 修改YAML配置,指定使用PowerShell并切换到Windows用户目录:
- task: SSH@0 inputs: sshEndpoint: 'windows endpoint' runOptions: 'inline' inline: | cd C:\Users\<你的用户名> # 这里写要执行的PowerShell命令,比如查看目录 Get-ChildItem shell: 'powershell.exe'
- 如果习惯用cmd,把
shell设为cmd.exe,脚本改用cmd命令:
- task: SSH@0 inputs: sshEndpoint: 'windows endpoint' runOptions: 'inline' inline: | cd C:\Users\<你的用户名> dir shell: 'cmd.exe'
二、解决PowerShell任务ssh命令的"Host key verification failed"错误
这个问题是因为ADO代理机器未信任Windows主机的SSH密钥,直接在PowerShell任务里先添加主机密钥到信任列表:
# 先扫描并添加Windows主机的SSH密钥到known_hosts ssh-keyscan <Windows主机IP或域名> >> ~/.ssh/known_hosts # 再执行你的ssh命令 ssh <用户名>@<Windows主机IP或域名> "Get-ChildItem C:\"
三、解决WinRM的"The WinRM client cannot process the request"错误
用WinRM连接时,先确认Windows主机和ADO配置都到位:
- Windows主机端执行
winrm quickconfig,按提示完成基础配置,再开启防火墙规则:
netsh advfirewall firewall set rule group="Windows Remote Management" new enable=yes
- 在Azure DevOps里创建**Windows Remote Management (WinRM)**类型的服务连接,填对主机地址、端口(默认HTTP用5985,HTTPS用5986)、认证方式(用户名密码或证书)
- 如果用自签名证书,执行WinRM命令时加
-SkipCACheck跳过证书验证:
$cred = Get-Credential $session = New-PSSession -ComputerName <Windows主机IP> -Credential $cred -SkipCACheck Invoke-Command -Session $session -ScriptBlock { Get-ChildItem }
内容的提问来源于stack exchange,提问作者GraconBease
相关产品推荐
相关产品推荐

