我有一个C#应用程序,它使用Runspaces查询Lync和Exchange PowerShell。如果我在对PowerShell命令行开关具有本地访问权限的服务器上执行应用程序,即安装了管理工具,那么它可以工作;但是,如果我远程连接到服务器,我的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;
}
谢谢!真的很感激一些帮助:)
答案 0 :(得分:3)
它们是不同的,因为它们已在远程会话中序列化,然后在本地会话中反序列化。序列化可能导致对象属性的保真度丢失,以及从对象中删除方法。