我正在嘗試使用Exchange 2010遠程PowerShell和c#運行PS1腳本。我可以連接並運行ps1腳本,但腳本中有一些地方使用交換cmdlet來更新必要的用戶信息。腳本正在使用的一個cmdlet是更新收件人。該腳本運行良好,直到它試圖運行此cmdlet並且錯誤地說:C# Exchange 2010 PS1腳本
術語「更新收件人」不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。
有沒有人知道是否有任何限制從c#PS1腳本內運行cmdlet?
感謝
我正在嘗試使用Exchange 2010遠程PowerShell和c#運行PS1腳本。我可以連接並運行ps1腳本,但腳本中有一些地方使用交換cmdlet來更新必要的用戶信息。腳本正在使用的一個cmdlet是更新收件人。該腳本運行良好,直到它試圖運行此cmdlet並且錯誤地說:C# Exchange 2010 PS1腳本
術語「更新收件人」不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。
有沒有人知道是否有任何限制從c#PS1腳本內運行cmdlet?
感謝
這段代碼爲了運行的Exchange 2010的PowerShell腳本從命令行運行,則需要在PowerShell腳本的開頭加載Exchange組件。將這兩行添加到.ps1文件中。在第一行中將Exchange服務器的名稱替換爲EXCHANGESERVER。
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHANGESERVER/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
試試這個示例代碼(在知道它適用於Exchange 2010)
PSCredential credential = new PSCredential(@"domain\user", createPassword("Pass"));
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "exchange.ibm.com", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
try
{
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command objCommand = new Command("");
objCommand.Parameters.Add("Identity", @"dom\user");
pipeline.Commands.Add(objCommand);
Collection<PSObject> results = pipeline.Invoke();
}
catch
{
}
finally
{
runspace.Close();
}
Runspace myRunspace = RunspaceFactory.CreateRunspace();
myRunspace.Open();
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open(rsConfig);