我正嘗試在C#中編寫代碼,該代碼通過Power Shell命令從用戶處獲取郵箱詳細信息。如何從.Net程序運行Import-PSSession
電源外殼命令腳本是:
Import-PSSession -session (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://myServer.myDomain.com/Powershell)
Get-Mailbox -Identity helpdesk
運行該腳本確定從PowerShell中。
現在我的目標是使用C#運行它。
這是我的函數:
private void button1_Click(object sender, EventArgs e)
{
m_RunSpace = RunspaceFactory.CreateRunspace();
m_RunSpace.Open();
Pipeline pipeLine = m_RunSpace.CreatePipeline();
Command newSession = new Command("New-PSSession");
newSession.Parameters.Add("-ConfigurationName", "Microsoft.Exchange");
newSession.Parameters.Add("-ConnectionUri", "http://myServer.myDomain.com/Powershell");
Command createSessionForExch = new Command("Import-PSSession");
createSessionForExch.Parameters.Add("-Session", newSession);
Command getMailbox = new Command("Get-Mailbox");
getMailbox.Parameters.Add("-Identity", "helpdesk");
pipeLine.Commands.Add(createSessionForExch);
pipeLine.Commands.Add(getMailbox);
Collection<PSObject> commandResults = pipeLine.Invoke();
foreach (PSObject cmdlet in commandResults)
{
}
}
但是我收到一個錯誤「獲取郵箱」命令不被識別:
大概是因爲進口PSSession中沒有被正確invok 。
我需要幫助如何正確地從C#
是用戶帳戶運行腳本的一個memeber Exchange中可以管理郵箱的RBAC角色?否則,導入會話時將無法獲取它的cmdlet。 – mjolinor
是的。 用戶運行腳本有權限。 當我在PowerShell上運行它時,它工作正常。 當我試圖在C#上實現它時,我收到一個錯誤。 我想我沒有正確實施它。 – E235
你有沒有找到辦法做到這一點? –