C# Powershell通用会话,并在Exchange远程管理会话中导入此会话
形势: 我正在尝试制作一个应用程序(c#-asp.net)来操作exchange服务器上用户的。应用程序将位于与exchange不同的服务器上。因此,为了操作数据,我使用了用c#创建的“Exchange远程管理会话”。Exchange远程管理会话允许访问简单的powershell命令,如“New Mailbox”和“Set User”-这对简单任务很好,但在我的情况下,我必须执行更复杂的操作,这些操作需要一些默认命令中未包含的特定命令。要访问这个命令,我必须使用一些特定的模块,比如“ActiveDirectory”。简单吗?仅使用“导入模块”!不是真的,正如我所说,“Exchange远程管理会话”受命令限制很大,不允许使用“导入模块” 那么我们能做什么呢? 我读了很多关于我的问题的书,最“简单”(我理解理论)的解决方案是: 从通用PS会话开始,导入AD模块,然后连接到Exchange管理会话,执行导入PSSession,并对Exchange管理内容使用隐式远程处理 考虑到我对使用c#操作Powershell非常陌生,我不知道如何在代码中使用这个很棒的解决方案。所以我请求你的帮助 这是我当前的代码:C# Powershell通用会话,并在Exchange远程管理会话中导入此会话,c#,asp.net,session,powershell,exchange-server,C#,Asp.net,Session,Powershell,Exchange Server,形势: 我正在尝试制作一个应用程序(c#-asp.net)来操作exchange服务器上用户的。应用程序将位于与exchange不同的服务器上。因此,为了操作数据,我使用了用c#创建的“Exchange远程管理会话”。Exchange远程管理会话允许访问简单的powershell命令,如“New Mailbox”和“Set User”-这对简单任务很好,但在我的情况下,我必须执行更复杂的操作,这些操作需要一些默认命令中未包含的特定命令。要访问这个命令,我必须使用一些特定的模块,比如“Active
// Prepare the credentials.
string runasUsername = @"MarioKart 8";
string runasPassword = "MarioKart";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword)
ssRunasPassword.AppendChar(x);
PSCredential credentials =
new PSCredential(runasUsername, ssRunasPassword);
// Prepare the connection
var connInfo = new WSManConnectionInfo(
new Uri("MarioKart8Server"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credentials);
connInfo.AuthenticationMechanism =
AuthenticationMechanism.Basic;
connInfo.SkipCACheck = true;
connInfo.SkipCNCheck = true;
// Create the runspace where the command will be executed
var runspace = RunspaceFactory.CreateRunspace(connInfo);
// create the PowerShell command
var command = new Command("New-Mailbox");
....
// Add the command to the runspace's pipeline
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(command);
// Execute the command
var results = pipeline.Invoke();
if (results.Count > 0)
System.Diagnostics.Debug.WriteLine("SUCCESS");
else
System.Diagnostics.Debug.WriteLine("FAIL");
这段代码非常适合于简单的任务(比如“新建邮箱”)!但是,如何创建“通用PS会话”,然后在“Exchange远程管理会话”中使用此会话 据我所知,您的问题是: 您必须执行更复杂的操作,这些操作需要一些默认命令中未包含的特定命令。要访问此命令,必须使用某些特定模块,如“Active Directory” 这里有三种使用C#查询Active Directory的方法(不带PowerShell模块) 首先,使用ADSI(旧式) 第二个,从.NET 3.5开始,微软推出了
Principal
和AccountManagement
第三个,您可以使用低级(本机LDAP)协议
你不是完全正确的。由于您正在连接到远程powershell会话,因此不需要执行导入模块 创建会话时,IIS和Powershell将根据与凭据关联的访问权限自动为您加载所需的模块。这与加载Exchange Powershell并在加载模块页面顶部看到绿色栏时类似 希望能有帮助