C# Exchange 2010 PS1 Script

本文关键字:Script PS1 2010 Exchange | 更新日期: 2023-09-27 18:04:48

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

术语'update- receiver '不被识别为cmdlet、函数、脚本文件或可操作程序的名称。

有没有人知道在PS1脚本中运行cmdlet是否有任何限制?

谢谢

C#  Exchange 2010 PS1 Script

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

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

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

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