【发布时间】:2018-01-28 09:47:56
【问题描述】:
为本地交换服务器启用邮箱的代码在 Visual Studio 以调试模式运行时有效,但在同一实例上的 IIS 上部署时失败。命令也适用于 Powershell 控制台 它抛出异常无法连接到服务器访问被拒绝。
有人可以帮我解决这个问题吗?
PFB sn-p 在 Visual Studio 调试模式下运行时运行良好,但在 IIS 上部署的代码运行时失败
string connectionUri = strConURI;
string loginPassword = Pwd;
SecureString secpassword = new SecureString();
foreach (char c in loginPassword)
{
secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(Usercred, secpassword);
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(connectionUri));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
// command.AddCommand("Set-ExecutionPolicy RemoteSigned");
powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
Collection<System.Management.Automation.PSObject>
result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
throw new Exception("failed");
}
powershell = PowerShell.Create();
command = new PSCommand();
command.AddCommand("Invoke-Command");
const String ScriptBlock = "Get-User {0} | Enable-RemoteMailbox -RemoteRoutingAddress {1};";
String ScriptBlockstr = string.Format(ScriptBlock, GetUser, MailboxUser);
command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create(ScriptBlockstr));
command.AddParameter("Session", result[0]);
powershell.Commands = command;
powershell.Runspace = runspace;
var mailBoxes = powershell.Invoke();
【问题讨论】:
标签: c# visual-studio powershell iis exchange-server