C#中的Powershell-访问Exchange

C#中的Powershell-访问Exchange,c#,powershell,syntax,C#,Powershell,Syntax,我正在使用.NET System.Management.Automation*命名空间中的Powershell类。我需要将设置变量的cmdlet调用转换为C#语法: 以下是我如何构建命令: List<Command> cList = new List<Command>(); Command cEx = new Command("Set-ExecutionPolicy"); cEx.Parameters.Add(new CommandParameter(

我正在使用.NET System.Management.Automation*命名空间中的Powershell类。我需要将设置变量的cmdlet调用转换为C#语法:

以下是我如何构建命令:

 List<Command> cList = new List<Command>();
 Command cEx = new Command("Set-ExecutionPolicy");
        cEx.Parameters.Add(new CommandParameter("Scope", "CurrentUser"));
        cEx.Parameters.Add(new CommandParameter("ExecutionPolicy", "Unrestricted"));
        cList.Add(cEx);

调用c10命令时,出现异常:“找不到与参数名称'ConfigurationName'匹配的参数”

问题是试图连接到Exchange 2010

这篇文章(以及其中链接的文章)很有帮助-

…但我认为这是不对的。为什么?对我来说,它看起来像是有效的代码。它能工作吗?我在调用命令时得到一个EXC选项-“找不到与参数名称'ConfigurationName'匹配的参数,因此您的错误是由以下行引起的:
c10.Parameters.Add(new CommandParameter(“ConfigurationName”,“Microsoft.Exchange”)。您应该编辑您的问题,指出这一点。@chade。谢谢是的。我刚刚意识到一个例外可能是我的开发机器上没有Exchange SDK(或类似的),因为我没有在Exchange服务器上运行它。因此,如果这解决了您的问题,您应该发布一个答案,解释您是如何做到的。是的,它被允许这样做。
 List<Command> cList = new List<Command>();
 Command cEx = new Command("Set-ExecutionPolicy");
        cEx.Parameters.Add(new CommandParameter("Scope", "CurrentUser"));
        cEx.Parameters.Add(new CommandParameter("ExecutionPolicy", "Unrestricted"));
        cList.Add(cEx);
Pipeline pipeline;
        Collection<PSObject> exResults = null;
        foreach (Command cmd in cList)
        {
            pipeline = ps.Runspace.CreatePipeline();
            pipeline.Commands.Add(cmd);
            exResults = pipeline.Invoke();
            foreach (PSObject p in exResults)
            {
                Console.WriteLine(p);
            }
        }
 Command c10 = new Command("Set-Variable");
        c10.Parameters.Add(new CommandParameter("Name", "Session"));
        c10.Parameters.Add(new CommandParameter("Value", "New-PSSession"));
        c10.Parameters.Add(new CommandParameter("ConfigurationName", "Microsoft.Exchange"));
        c10.Parameters.Add(new CommandParameter("ConnectionUri", "https://outlook.office365.com/powershell-liveid"));
        c10.Parameters.Add(new CommandParameter("Credential", pCredUser));
        c10.Parameters.Add(new CommandParameter("Authentication", "Basic"));
        c10.Parameters.Add(new CommandParameter("AllowRedirection"));
        cList.Add(c10);