微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Powershell 远程会话在使用自定义 cmdlet 并且存在第 3 方软件时中断

如何解决Powershell 远程会话在使用自定义 cmdlet 并且存在第 3 方软件时中断

我们有一个在本地工作的自定义 PS cmdlet,但在远程会话中它似乎破坏了会话,我必须退出会话。除非远程计算机上安装了特定的 3rd 方软件包,否则不会出现此问题; (该包是必需的,不在我的控制范围内)。我被困住了,想请教您的见解。

最初,我是在 C# 和 Runspace(.Net 框架 4.5.2)的上下文中执行此操作的。这是报告“WSMan 提供程序主机进程未返回正确响应”。由于该消息不太有用,因此我通过使用交互式 shell 和 enter-pssession 进行了简化,从等式中消除了 .Net。

示例#1,远程桌面到远程计算机,cmdlt (Get-ToolVersion) 工作正常

Magnus@192.168.19.7# Get-ToolVersion VISA 
Tool    Version
VISA  15.0.0.49

示例#2,在远程会话中也能正常工作......未安装 IFF 3rd-party VISA

PS C:\Users\Tom> enter-pssession 192.168.1.6 -credential Tom
[192.168.19.6]: PS C:\Users\Tom\Documents> cd C:\Austin\Installers\PowerShellScripts\
[192.168.19.6]: PS C:\Austin\Installers\PowerShellScripts> Import-Module D:\Austin\Bin\TerToolUtilities.dll -Function Get-ToolVersion
[192.168.19.6]: PS C:\Austin\Installers\PowerShellScripts> Get-ToolVersion QUD
Tool Version
QUD  1.00.52

示例#3 在远程会话中,我收到错误消息“WSMan 提供程序主机进程没有返回正确的响应。”

PS C:\Users\Tom> enter-pssession 192.168.1.7 -credential Tom
[192.168.19.7]: PS C:\Users\Tom\Documents> cd C:\Austin\Installers\PowerShellScripts\
[192.168.19.7]: PS C:\Austin\Installers\PowerShellScripts> Import-Module D:\Austin\Bin\TerToolUtilities.dll -Function Get-ToolVersion
[192.168.19.7]: PS C:\Austin\Installers\PowerShellScripts> Get-ToolVersion VISA
Processing data from Remote Server 192.168.1.7 Failed with the following error message: The WSMan provider host process did not return a proper response.  A provider in the host process may have behaved improperly. For more information,see the about_Remote_Troubleshooting Help topic.
PS C:\Users\Tom> exit-pssession

示例#4 在远程会话中,使用 cmdlet 查询不同的工具(不是 VISA),它运行一次并且会话“关闭或损坏”

PS C:\Users\Tom> enter-pssession 192.168.1.7 -credential Tom
[192.168.19.7]: PS C:\Users\Tom\Documents> cd C:\Austin\Installers\PowerShellScripts\
[192.168.19.7]: PS C:\Austin\Installers\PowerShellScripts> Import-Module D:\Austin\Bin\ToolUtilities.dll -Function Get-ToolVersion
[192.168.19.7]: PS C:\Austin\Installers\PowerShellScripts> Get-ToolVersion QUD
Tool Version
QUD  1.00.52

[192.168.19.7]: PS C:\Austin\Installers\PowerShellScripts> Get-ToolVersion QUD
Command 'Get-ToolVersion QUD' was not run as the session in which it was intended to run was either closed or broken
PS C:\Users\Tom>

自定义 cmdlet 试图找到问题,我删除了 cmdlet 但这并没有解决错误

using System;
[Cmdlet(VerbsCommon.Get,"ToolVersion")]
[OutputType(typeof(ToolVersion))]
public class GetToolVersionCmdlet : Cmdlet
{
    private class ToolVersion
    {
        public string Tool { get; set; }
        public string Version { get; set; }
    }

    protected override void ProcessRecord()
    {
        try
        {
            string version = null;

            //   <DELETED>

            WriteObject(new ToolVersion { Tool = ToolName,Version = version });    
        }
        catch (Exception ex)
        {
            WriteObject(new ToolVersion { Tool = ToolName,Version = "" });
        }
    }

环境 PS 版本和 WinRM 配置在本地和远程计算机上是相同的。 操作系统不同;本地是 Windows 10,远程是 Windows Server 2019。 虽然无数关于“WSMan 提供程序主机进程没有返回正确响应”的报告都归因于内存配置,但我没有找到任何证据。

Magnus@192.168.19.7# $psversiontable
Name                           Value
Psversion                      5.1.14393.0
PSEdition                      Desktop
PSCompatibLeversions           {1.0,2.0,3.0,4.0...}
BuildVersion                   10.0.14393.0
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


Magnus@192.168.19.7# winrm get winrm/config/winrs
Winrs
    AllowRemoteShellAccess = true
    IdleTimeout = 7200000
    MaxConcurrentUsers = 2147483647
    MaxShellRunTime = 2147483647
    MaxProcessesPerShell = 2147483647
    MaxMemoryPerShellMB = 2147483647
    MaxShellsPerUser = 2147483647

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。