从Visual Basic/C#/ .NET运行Exchange命令行管理程序cmdlet应用程序
目标:提供使用Visual Basic或C#或.NET与Exchange命令行管理程序交互的Web服务,向其发送命令以运行cmdlet并返回结果作为XML。 (请注意,我们可以使用任何语言来编写服务,但由于它是一个Windows Box,并且我们有Visual Studio 2008,所以似乎最简单的解决方案就是使用它来创建VB/.NET Web服务。的确,它是很容易做到的,只是指向和点击)从Visual Basic/C#/ .NET运行Exchange命令行管理程序cmdlet应用程序
问题:如何运行从Web服务,比如在Exchange命令行管理程序cmdlet的,则Get-DistributionGroupMember“实时总统”
看来,我们应该能够创建一个运行该cmdlet的PowerShell脚本,并且能够从命令行调用该脚本,因此只需从程序中调用该脚本即可。这听起来正确吗?如果是的话,我会怎么做呢?谢谢。答案可以是语言不可知的,但Visual Basic可能是最好的,因为这是我加载测试Web服务了。
嗯,没有得到答案,但有点解决它。我在运行64位PowerShell时遇到了问题,但最终升级到Exchange 2010并使用C#,因此不再有问题。
简单的答案是,您在Visual Studio中创建一个新的PowerShell应用程序,然后添加对System.Management.Automation dll的引用。这使您可以为Powershell设置一个名称空间并调用它。 http://msdn.microsoft.com/en-us/library/system.management.automation(VS.85).aspx您可以使用可用的管道类http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.pipeline(VS.85).aspx创建管道,以便将管道命令返回。然后你把你的命令,如果需要添加参数。运行该应用程序,它会返回您在PowerShell中调用的cmdlet的结果,然后您可以从该处启动。
适应从MSDN http://msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx实际的代码可能会非常棘手,因为你必须得到许可权和一个MACINE与所有Exchange插件的运行:
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation.Runspaces;
using System.Reflection;
public static Runspace GetExchangeRunspace()
{
return GetExchangeRunspace("");
}
public static Runspace GetExchangeRunspace(string snapIn)
{
string consoleFilePath = (ScriptEngine.GetExchangeAssemblyPath()
+ "bin\\exshell.psc1").Replace("Exchange Server", "EXCHAN~1");
Response.Write("<br/>" + consoleFilePath);
PSConsoleLoadException warnings = null;
RunspaceConfiguration runspaceConfiguration
= RunspaceConfiguration.Create(consoleFilePath, out warnings);
if ((snapIn + "").Trim().Length > 0)
{
PSSnapInException warning = null;
Response.Write("<br/>Start AddPSSnapIn..." + snapIn);
Response.Write("<br/>"
+ runspaceConfiguration.AddPSSnapIn(snapIn, out warning));
Response.Write("<br/>" + warning);
}
return RunspaceFactory.CreateRunspace(runspaceConfiguration);
}
private static string GetExchangeAssemblyPath()
{
string path = "";
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Microsoft\\ExchangeServer\\v14\\Setup"); // or your version
if (key != null)
{
path = Path.GetFullPath(string.Concat(key.GetValue("MsiInstallPath")));
Response.Write(path);
}
}
catch (Exception ex) { }
return path;
}
你如何获得Exchange管理外壳cmdlet显示?看来你需要以某种方式添加对它们的引用,否则我会得到一个他们不存在的错误。 – Andrey 2012-02-23 22:48:45
@Andrey你知道如何让EMS cmdlet出现吗? – 2012-07-19 09:20:02