Andrew Morpeth
Andrew Morpeth

Reputation: 167

Are PowerShell objects returned from a remote session different?

I have a C# application that queries Lync and Exchange PowerShell using Runspaces. If I execute the application on a server that has local access to the PowerShell commandlets i.e. the management tools are installed, it works; however, if I connect to the server remotely, my foreach logic fails with the following error:

Foreach loop error

Example loop - the first loop works fine, however it fails at the 2nd one as I drill down in the PS objects:

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. 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:

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 :)

Upvotes: 2

Views: 473

Answers (1)

mjolinor
mjolinor

Reputation: 68273

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.

Upvotes: 3

Related Questions