从远程会话返回的 PowerShell 对象是否不同?

Are PowerShell objects returned from a remote session different?

我有一个使用运行空间查询 Lync 和 Exchange PowerShell 的 C# 应用程序。如果我在可以本地访问 PowerShell commandlet 的服务器上执行应用程序,即安装了管理工具,它就可以工作;但是,如果我远程连接到服务器,我的 foreach 逻辑会失败并出现以下错误:

示例循环 - 第一个循环工作正常,但是当我在 PS 对象中向下钻取时,它在第二个循环失败:

public Collection<PSObject> Workflows;


var wFFilter = _dataService.WorkFlows.ToList();

//foreach (PSObject workflowName in workflowNames)
foreach (dynamic workflowName in wFFilter)
{
    var newWorkflow = new WorkFlowViewModel();
    if (workflowName != null)
    {
        //GetDisplay Name
        newWorkflow.Name = workflowName.Name;

        //Populate IVR options
        foreach (dynamic root in workflowName.DefaultAction.Question.AnswerList)
        {
            if (root.Action.QueueID != null)
            {
                //Do something
            }
        }
    }
}

这使我相信返回 PowerShell 对象的方式有所不同。会是这样吗?我只是想不通为什么这是不同的,以及我如何处理本地和远程返回的对象。

我的PS代码:

private RunspacePool rrp;
public Collection<PSObject> ExecuteSynchronously(string PSCommand, string     RemoteMachineFqdn, int RemoteMachinePort, string RemoteMachinePath,
       bool SslEnabled, string Username, SecureString Password)
    {
        Collection<PSObject> psResult;

        if (rrp == null)
        {
            string shellUri = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
            PSCredential remoteCredential = new PSCredential(Username, Password);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(SslEnabled, RemoteMachineFqdn,
                RemoteMachinePort, RemoteMachinePath, shellUri, remoteCredential);

            connectionInfo.SkipRevocationCheck = true;
            connectionInfo.SkipCACheck = true;
            connectionInfo.SkipCNCheck = true;

            rrp = RunspaceFactory.CreateRunspacePool(1, 10, connectionInfo);
            rrp.Open();
        }

        using (PowerShell powershell = PowerShell.Create())
        {
            powershell.RunspacePool = rrp;
            powershell.AddScript(PSCommand);
            psResult = powershell.Invoke();
        }
        return psResult;

    }

谢谢!非常感谢您对此提供的帮助:)

它们是不同的,因为它们已在远程会话中序列化,然后在您的本地会话中反序列化。序列化会导致对象属性失去保真度,并从对象中删除方法。