在Visual Studio中以调试模式运行时,为本地交换服务器启用邮箱的代码工作,但在同一实例上的IIS上部署时失败。该命令也可从Powershell Console工作 It抛出异常无法连接到服务器访问被拒绝。从C#powershell启用邮箱
有人能帮我解决吗?
PFB从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();
我们可以在调试模式下从powershell控制台以及从Visual Studio运行命令......当相同的代码部署在IIS上的相同服务器上时,它会引发访问被拒绝。 –
任何想法可能会出错? –
您是否检查过上述选项?你没有说明你的代码中是否使用了https或http,所以我在黑暗中完全绊倒了,而你给我的信息太少了......你只重复你的代码而不检查我的答案...... – BastianW