[英]Powershell Command Will not run in C#
我正在編寫ac #Windows Form應用程序,以便以.pst格式將Exchange 2010郵箱遷移到服務器上的文件位置。 我使用Powershell SDK(Runspace05)中的示例來訪問Exchange cmdlet(Get-Mailbox)並使用用戶郵箱填充組合框,沒有任何問題。
我遇到問題的部分是運行New-MailboxExportRequest cmdlet(執行導出的cmdlet)以及返回它的對象並在列表框控件中顯示它們的能力。 我錯過了什么? 在此先感謝您的幫助。
代碼:
InitialSessionState iss = InitialSessionState.CreateDefault();
PSSnapInException warning;
iss.ImportPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out warning);
using (Runspace myrunspace = RunspaceFactory.CreateRunspace(iss))
{
myrunspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
var mailbox = cmbEmailUserName.Text;
var pstFile = txtFileSaveLocation.Text;
const int badLimit = 100; //can be increased in necessary
powershell.AddCommand("Microsoft.Exchange.Management.PowerShell.E2010\\New-MailboxExportRequest");
powershell.AddParameter("Mailbox", mailbox);
powershell.AddParameter("FilePath", pstFile);
powershell.Runspace = myrunspace;
Collection<PSObject> results = powershell.Invoke();
foreach (PSObject thisResult in results)
{
lstBoxStatus.Items.Add(thisResult);
}
myrunspace.Close();
}
}
您想要訪問PSObject
的屬性,而不是對象本身。
嘗試這個:
foreach (PSObject thisResult in results)
{
foreach (PSPropertyInfo thisResultInfo in thisResult.Properties)
{
lstBoxStatus.Items.Add("Name: {0} Value: {1} Type: {2}", thisResultInfo.Name, thisResultInfo.Value, thisResultInfo.MemberType);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.