You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Azure函数中调用New-PSSession报错:'ssh.exe'未被识别

问题:本地正常的PowerShell脚本在Azure函数消费计划中执行New-PSSession报错

本地运行同一份PowerShell脚本无任何问题,但在Azure函数(消费计划)中执行以下命令时出现报错:

New-PSSession -HostName $PrimaryValidator -UserName $PrimaryValidatorUserName -KeyFilePath $PrimaryValidatorSSHKey -ConnectingTimeout 20000

错误日志详情

Azure日志返回的完整错误如下:

2025-06-20T22:54:15.457 [Error] ERROR: 发生PowerShell无法处理的错误,远程会话可能已结束。

  Exception             : 
        Type           : System.Management.Automation.Remoting.PSRemotingDataStructureException
        ErrorRecord    : 
            Exception             : 
                Type    : System.Management.Automation.ParentContainsErrorRecordException
                Message : 发生PowerShell无法处理的错误,远程会话可能已结束。
                HResult : -2146233087
            CategoryInfo          : ResourceUnavailable: (:) [], ParentContainsErrorRecordException
            FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException
        Message        : 发生PowerShell无法处理的错误,远程会话可能已结束。
        InnerException : 
            Type        : System.Management.Automation.CommandNotFoundException
            ErrorRecord : 
                Exception             : 
                    Type    : System.Management.Automation.ParentContainsErrorRecordException
                    Message : 术语'ssh.exe'未被识别为 cmdlet、函数、脚本文件或可执行程序的名称,请检查名称拼写,或验证路径是否正确后重试。
                    HResult : -2146233087
                TargetObject          : ssh.exe
                CategoryInfo          : ObjectNotFound: (ssh.exe:String) [], ParentContainsErrorRecordException
                FullyQualifiedErrorId : CommandNotFoundException
            CommandName : ssh.exe
            TargetSite  : 
                Name          : LookupCommandInfo
                DeclaringType : [System.Management.Automation.CommandDiscovery]
                MemberType    : Method
                Module        : System.Management.Automation.dll
            Message     : 术语'ssh.exe'未被识别为 cmdlet、函数、脚本文件或可执行程序的名称,请检查名称拼写,或验证路径是否正确后重试。
            Source      : System.Management.Automation
            HResult     : -2146233087
            StackTrace  : 
       at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)
       at System.Management.Automation.Runspaces.SSHConnectionInfo.StartSSHProcess(StreamWriter& stdInWriterVar, StreamReader& stdOutReaderVar, StreamReader& stdErrReaderVar)
       at System.Management.Automation.Remoting.Client.SSHClientSessionTransportManager.CreateAsync()
       at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.SendNegotiationAsync(RemoteSessionState sessionState)
       at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.HandleStateChanged(Object sender, RemoteSessionStateEventArgs arg)
       at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerStateMachine.RaiseStateMachineEvents()
       at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerStateMachine.ProcessEvents()
        HResult        : -2146233087
    TargetObject          : System.Management.Automation.RemoteRunspace
    CategoryInfo          : OpenError: (System.Management.A…tion.RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingDataStructureException
    FullyQualifiedErrorId : PSSessionOpenFailed
    ErrorDetails          : [meshmap-mainnet-primary.sjiconsulting.com] 发生PowerShell无法处理的错误,远程会话可能已结束。
    InvocationInfo        : 
        MyCommand        : New-PSSession
        ScriptLineNumber : 246
        OffsetInLine     : 19
        HistoryId        : 1
        ScriptName       : C:\home\site\wwwroot\Get-SolanaValidatorStatus\Validator Failover\Move-ActiveSolanaValidator.ps1
        Line             : $PrimarySession = New-PSSession -HostName $PrimaryValidator -UserName $PrimaryValidatorUserName -KeyFilePath $PrimaryValidatorSSHKey -ConnectingTimeout 20000
                           
        Statement        : New-PSSession -HostName $PrimaryValidator -UserName $PrimaryValidatorUserName -KeyFilePath $PrimaryValidatorSSHKey -ConnectingTimeout 20000
        PositionMessage  : 在C:\home\site\wwwroot\Get-SolanaValidatorStatus\Validator Failover\Move-ActiveSolanaValidator.ps1:246 char:19
                           + … rySession = New-PSSession -HostName $PrimaryValidator -UserName $Prim …
                           +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        PSScriptRoot     : C:\home\site\wwwroot\Get-SolanaValidatorStatus\Validator Failover
        PSCommandPath    : C:\home\site\wwwroot\Get-SolanaValidatorStatus\Validator Failover\Move-ActiveSolanaValidator.ps1
        InvocationName   : New-PSSession
        CommandOrigin    : Internal
    ScriptStackTrace      : at <ScriptBlock>, C:\home\site\wwwroot\Get-SolanaValidatorStatus\Validator Failover\Move-ActiveSolanaValidator.ps1: line 246
                            at <ScriptBlock>, <No file>: line 1
                            at <ScriptBlock>, C:\home\site\wwwroot\Get-SolanaValidatorStatus\Validator Failover\Move-MainnetValidator.ps1: line 179
                            at <ScriptBlock>, <No file>: line 1
                            at New-FailoverIfNeeded, C:\home\site\wwwroot\Get-SolanaValidatorStatus\run.ps1: line 225
                            at New-DelinquencyCheckAndFailoverIfNeeded, C:\home\site\wwwroot\Get-SolanaValidatorStatus\run.ps1: line 288
                            at <ScriptBlock>, C:\home\site\wwwroot\Get-SolanaValidatorStatus\run.ps1: line 358
    PipelineIterationInfo : 
          0
          1

问题原因

核心错误是找不到ssh.exe:Azure函数消费计划的运行环境未预装OpenSSH客户端,而New-PSSession通过SSH协议建立远程会话时依赖该工具;本地环境通常已自带OpenSSH客户端,因此脚本在本地可正常执行。

解决方案

  • 方案1:自定义部署包添加OpenSSH客户端

    1. 下载Windows版本的OpenSSH客户端相关文件
    2. 将ssh.exe等文件打包到函数应用的部署目录(如bin文件夹)
    3. 在脚本中指定ssh.exe的完整路径,或把该路径加入环境变量PATH:
      $sshDir = Join-Path $PSScriptRoot "bin"
      $env:PATH += ";$sshDir"
      
  • 方案2:切换到弹性高级计划(Premium Plan)
    弹性高级计划的运行环境基于完整Windows虚拟机,默认预装OpenSSH客户端,无需额外配置即可使用New-PSSession的SSH功能。

  • 方案3:改用其他远程连接方式
    若无法修改环境,可考虑使用Invoke-Command结合其他协议,或直接调用REST API完成目标操作,避免依赖SSH客户端。


内容的提问来源于stack exchange,提问作者Bobby Dore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 21:05:56