【发布时间】:2014-08-25 20:57:08
【问题描述】:
情况:
我正在尝试制作一个应用程序 (c#-asp.net) 来操作交换服务器上的用户。该应用程序将位于与交易所的服务器不同的服务器上。因此,为了操作数据,我使用了用 c# 创建的“Exchange 远程管理会话”。 Exchange 远程管理会话允许访问简单的 powershell 命令,例如“New-Mailbox”和“Set-User”——这对于简单的任务很有用,但在我的情况下,我必须执行更复杂的操作,这些操作需要一些特定的命令不包含在默认命令中。要访问这个命令,我必须使用一些特定的模块,比如“ActiveDirectory”。很简单 ?仅使用“导入模块”!不是真的,就像我说的那样,“Exchange 远程管理会话”的命令非常有限,并且“Import-Module”是不允许的......
那么我们能做些什么呢?
我阅读了很多关于我的问题的信息,最“简单”(我理解理论)的解决方案类似于:
从通用 PS 会话开始,导入 AD 模块,然后连接到 Exchange 管理会话并执行 Import-PSSession 并为 Exchange 管理内容使用隐式远程处理。
鉴于我是使用 c# 操作 Powershell 的新手,我不知道如何在我的代码中使用这个很棒的解决方案。所以我请求你的帮助。
这是我当前的代码:
// 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 远程管理会话”中使用该会话?
【问题讨论】:
标签: c# asp.net session powershell exchange-server