C#\ Exchange 2010 PS1脚本

时间:2011-08-09 08:35:31

标签: c# .net powershell-remoting exchange-server-2010

我正在尝试使用Exchange 2010远程PowerShell和c#运行PS1脚本。我可以连接并运行ps1脚本,但脚本中有一些地方使用exchange cmdlet来更新必要的用户信息。脚本使用的一个cmdlet是update-recipient。该脚本运行正常,直到它尝试运行此cmdlet和错误说:

术语“update-recipient”未被识别为cmdlet,函数,脚本文件或可操作程序的名称。

有没有人知道在c#?

的PS1脚本中运行cmdlet是否有任何限制

由于

3 个答案:

答案 0 :(得分:1)

要从命令行运行Exchange 2010 powershell脚本,您需要在powershell脚本的开头加载Exchange组件。将这两行添加到.ps1文件中。在第一行中替换Exchange服务器的EXCHANGESERVER名称。

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHANGESERVER/PowerShell/ -Authentication Kerberos

Import-PSSession $Session

答案 1 :(得分:0)

尝试此示例代码(知道它适用于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();                   
        }

答案 2 :(得分:0)

或者试用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);