简体   繁体   English

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

[英]Are PowerShell objects returned from a remote session different?

I have a C# application that queries Lync and Exchange PowerShell using Runspaces. 我有一个使用Runspaces查询Lync和Exchange PowerShell的C#应用​​程序。 If I execute the application on a server that has local access to the PowerShell commandlets ie the management tools are installed, it works; 如果我在对PowerShell命令集具有本地访问权(即已安装管理工具)的服务器上执行该应用程序,则它将起作用; however, if I connect to the server remotely, my foreach logic fails with the following error: 但是,如果我远程连接到服务器,我的foreach逻辑将失败,并显示以下错误:

Foreach循环错误

Example loop - the first loop works fine, however it fails at the 2nd one as I drill down in the PS objects: 示例循环-第一个循环工作正常,但是当我在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
            }
        }
    }
}

This leads me to believe there is something different in the way the PowerShell object is returned. 这使我相信,PowerShell对象的返回方式有所不同。 Could this be the case? 可能是这样吗? I just cant figure out why this is different, and how I can handle both local and remotely returned objects. 我只是想不出为什么这是不同的,以及如何处理本地和远程返回的对象。

My PS code: 我的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;

    }

Thanks! 谢谢! Really appreciate some help on this :) 真的感谢一些帮助:)

The are different because they've been serialized at the remote session and then deserialized in your local session. 之所以不同,是因为它们已在远程会话中序列化,然后在本地会话中反序列化。 Serialization can result in loss of fidelity of object properties, and removal of methods from the objects. 序列化可能导致对象属性的保真度降低,并从对象中删除方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM