'Can not execute Powershell Script in .Net MVC Application
I have a .Net MVC application which need to check and get some information from another server via powershell command with code shown below. But app can not get information
try
{
string un = @"domainA\domainUserName";
System.Security.SecureString pw = new System.Security.SecureString();
string password = "";
string exchangeServerName = "ServerAdress/powershell";
foreach (char ch in password)
{
pw.AppendChar(ch);
}
using (PowerShell _powerShell = PowerShell.Create())
{
var credential = new PSCredential(un, pw);
_powerShell.Runspace.SessionStateProxy.SetVariable("Credential", credential);
_powerShell.AddScript($"$Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri {exchangeServerName} -Credential $Credential -Authentication Kerberos -AllowRedirection");
_powerShell.AddScript("Import-PSSession $Session");
_powerShell.Invoke();
_powerShell.AddScript(@"get-mailbox -identity " + mail + " | Select-Object ProhibitSendReceiveQuota");
var results = _powerShell.Invoke();
string size = "";
string used = "";
string size2 = "";
foreach (PSObject obj in results)
{
size = (obj.Properties["ProhibitSendReceiveQuota"].Value.ToString());
}
_powerShell.AddScript(@"Get-MailboxStatistics -Identity " + mail + " | Select-Object Totalitemsize,DatabaseProhibitSendQuota");
var results2 = _powerShell.Invoke();
_powerShell.AddScript("Remove-PSSession $Session");
_powerShell.Invoke();
//
}
}
Target server and source server are in different domains. In local running app wroks perfectly. What can be cause this issue and how can I resolve issue.
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
