我最近不得不处理一个类似的问题,下面的代码帮助我通过与远程 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();
当然,您将进行一些常用的用户组成员配置、远程安全/不安全远程连接的服务器允许等等。这篇文章可能会有所帮助。