C# \ Exchange 2010 PS1 脚本

发布于 2024-11-28 14:54:12 字数 286 浏览 1 评论 0原文

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

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

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

谢谢

I am trying to run a PS1 script using Exchange 2010 remote powershell and c#. I can connect and run the ps1 script but there are a few places in the script that use exchange cmdlets to update necessary user information. One cmdlet the script is using is update-recipient. The script runs fine until it trys to run this cmdlet and errors saying:

The term 'update-recipient' is not recognized as the name of a cmdlet, function, script file, or operable program.

Does anyone know if there are any restrictions on running cmdlets inside of PS1 scripts from c#?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作妖 2024-12-05 14:54:12

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

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

Import-PSSession $Session

In order to run an Exchange 2010 powershell script from the command line, you need to load the Exchange components at the beginning of the powershell script. Add these 2 lines to your .ps1 file. Substitute your Exchange server's name for EXCHANGESERVER in the first line.

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

Import-PSSession $Session
辞慾 2024-12-05 14:54:12

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

Try this sample code ( in know it works for 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();                   
        }
疾风者 2024-12-05 14:54:12

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

Or try this code for 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文