从Visual Basic / C#/。NET应用程序运行Exchange命令行管理程序的cmdlet
-
27-09-2019 - |
题
目标:提供使用Visual Basic或C#或.NET网络服务与Exchange管理外壳相互作用,发送它的命令来运行小命令,并将结果作为XML返回。 (请注意,我们可以使用任何lanaguage写的服务,但因为它是Windows中,我们有Visual Studio的2008,它似乎是最简单的解决办法是只使用它来创建一个VB / .NET Web服务。事实上,是很容易做到的,只是指向和点击。)
问题:如何运行从Web服务的Exchange命令行管理程序cmdlet的,例如,获得-DistributionGroupMember “实时总统”
看来,我们应该能够创建运行cmdlet的PowerShell脚本,并能够调用命令行,因此,单纯从程序中调用它。这听起来是正确的吗?如果是的话我将如何去这件事吗?谢谢。答案可能是语言无关,但Visual Basic中很可能是最好的,因为这是我在加载测试Web服务了。
解决方案 2
好了,没有得到答案,但有点想通了。我得到一个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;
}