【问题标题】:C# \ Exchange 2010 PS1 ScriptC#\Exchange 2010 PS1 脚本
【发布时间】:2011-08-09 08:35:30
【问题描述】:
我正在尝试使用 Exchange 2010 远程 powershell 和 c# 运行 PS1 脚本。我可以连接并运行 ps1 脚本,但脚本中有几个地方使用 Exchange cmdlet 来更新必要的用户信息。脚本使用的一个 cmdlet 是 update-recipient。该脚本运行良好,直到它尝试运行此 cmdlet 并出现错误提示:
“update-recipient”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。
有谁知道从 c# 在 PS1 脚本中运行 cmdlet 是否有任何限制?
谢谢
【问题讨论】:
标签:
c#
.net
powershell-remoting
exchange-server-2010
【解决方案1】:
为了从命令行运行 Exchange 2010 powershell 脚本,您需要在 powershell 脚本的开头加载 Exchange 组件。将这两行添加到您的 .ps1 文件中。用您的 Exchange 服务器名称替换第一行中的 EXCHANGESERVER。
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHANGESERVER/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
【解决方案2】:
试试这个示例代码(知道它适用于 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();
}
【解决方案3】:
或者试试这个代码Exchange 2007 from MSFT
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);