在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