0

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

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

有谁知道在 c# 的 PS1 脚本中运行 cmdlet 是否有任何限制?

谢谢

4

3 回答 3

1

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

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

Import-PSSession $Session
于 2012-06-15T16:12:32.973 回答
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();                   
        }
于 2012-02-11T02:42:03.993 回答
0

或从 MSFT尝试此代码用于Exchange 2007

       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);
于 2012-02-11T02:43:04.557 回答