我找到了一些指南,这些指南解释了如何在Exchange Server上运行PowerShell脚本,但所有指南都要求计算机已加入域,而且大多数指南似乎都使用了变通方法,而不是最佳实践。
有没有办法使用C#.NET或VB.NET远程连接到Exchange Server?
实际上,我希望使用管理员凭据连接到我的Exchange Server (我将在程序中提供加密的凭据),并使用PowerShell脚本创建邮箱。这就是全部。
我希望将应用程序作为控制台应用程序启动,一旦确认了功能,就可以将其实现到web窗体应用程序或MVC中。
任何建议都是非常感谢的!
发布于 2013-04-03 23:22:14
我最近不得不处理一个类似的问题,下面的代码帮助我在安全连接到远程Exchange服务器的情况下执行功能。
Runspace remoteRunspace = null;
PSCredential psc = null;
// If user name is not passed used the credentials of the current farm account
if (!string.IsNullOrWhiteSpace(username))
{
if (!string.IsNullOrWhiteSpace(domain))
username = domain + "\\" + username;
SecureString secpassword = new SecureString();
if (!string.IsNullOrEmpty(password))
{
foreach (char c in password)
{
secpassword.AppendChar(c);
}
}
psc = new PSCredential(username, secpassword);
}
WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(RemotePowerShellUrl), PowerShellSchema, psc);
if (psc != null)
rri.AuthenticationMechanism = AuthenticationMechanism.Credssp;
remoteRunspace = RunspaceFactory.CreateRunspace(rri);
remoteRunspace.Open();
Pipeline pipeline = remoteRunspace.CreatePipeline();
Command cmdSharePointPowershell = new Command(Commands.AddPSSnapin.CommandName);
cmdSharePointPowershell.Parameters.Add(Commands.AddPSSnapin.Name, MicrosoftSharePointPowerShellSnapIn);
pipeline.Commands.Add(cmdSharePointPowershell);
pipeline.Invoke();当然,您会对用户组成员身份、服务器允许的远程安全/不安全远程连接等进行一些常见的配置。this的文章可能会有所帮助。
https://stackoverflow.com/questions/15791143
复制相似问题