![](/img/trans.png)
[英]Powershell generic session and import this session in Exchange remote management session
[英]Are PowerShell objects returned from a remote session different?
我有一個使用Runspaces查詢Lync和Exchange PowerShell的C#應用程序。 如果我在對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;
}
謝謝! 真的感謝一些幫助:)
之所以不同,是因為它們已在遠程會話中序列化,然后在本地會話中反序列化。 序列化可能導致對象屬性的保真度降低,並從對象中刪除方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.