Looking for an example of remote Exchange Shell in c sharp
-
Hi, I have executed Exchange 2007 commands inside of c sharp code before, but have not attempted to do so with 2013 until now. There are no management tools installed on the machines this app will run on, so I believe I have to open up a remote PSsession to an Exchange 2013 server which is something else I have not done. I was wondering if any one had an example of doing this with an Exchange 2013 specific cmdlet? Thank you
-
Hi, I have executed Exchange 2007 commands inside of c sharp code before, but have not attempted to do so with 2013 until now. There are no management tools installed on the machines this app will run on, so I believe I have to open up a remote PSsession to an Exchange 2013 server which is something else I have not done. I was wondering if any one had an example of doing this with an Exchange 2013 specific cmdlet? Thank you
I found this
$session = new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.domain.com/powershell -auth kerberos -credential (get-credential)
import-psession $session
And I can at least see the commands in the powershell session now, so this is a start
-
I found this
$session = new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.domain.com/powershell -auth kerberos -credential (get-credential)
import-psession $session
And I can at least see the commands in the powershell session now, so this is a start
This is telling me the "State of runspace is not valid for this operation" at line 1082, which is the var results2 = powerShell.Invoke(); line. Any ideas?
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
string un = @"domain\username";
System.Security.SecureString pw = new System.Security.SecureString();
string password = "password";
foreach (char ch in password)
{
pw.AppendChar(ch);
}
PSCredential cred = new PSCredential(un, pw);string CONNECTION\_URI = @"http://exchangeserver.com/powershell"; PSCommand psSession = new PSCommand(); psSession.AddCommand("$session = New-PSSession"); psSession.AddParameter("ConfigurationName", "Microsoft.Exchange"); psSession.AddParameter("ConnectionUri", new Uri(CONNECTION\_URI)); psSession.AddParameter("Kerberos"); psSession.AddParameter("Credential", cred); psSession.AddParameter("AllowRedirection"); psSession.AddCommand("Import-PSSession"); psSession.AddParameter("$session"); psSession.AddCommand("Get-MailboxDatabaseCopyStatus"); psSession.AddParameter("databasename"); powerShell.Commands = psSession; var results2 = powerShell.Invoke(); foreach (var item in results2) { MessageBox.Show(item.Members.ToString()); } }
-
This is telling me the "State of runspace is not valid for this operation" at line 1082, which is the var results2 = powerShell.Invoke(); line. Any ideas?
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
string un = @"domain\username";
System.Security.SecureString pw = new System.Security.SecureString();
string password = "password";
foreach (char ch in password)
{
pw.AppendChar(ch);
}
PSCredential cred = new PSCredential(un, pw);string CONNECTION\_URI = @"http://exchangeserver.com/powershell"; PSCommand psSession = new PSCommand(); psSession.AddCommand("$session = New-PSSession"); psSession.AddParameter("ConfigurationName", "Microsoft.Exchange"); psSession.AddParameter("ConnectionUri", new Uri(CONNECTION\_URI)); psSession.AddParameter("Kerberos"); psSession.AddParameter("Credential", cred); psSession.AddParameter("AllowRedirection"); psSession.AddCommand("Import-PSSession"); psSession.AddParameter("$session"); psSession.AddCommand("Get-MailboxDatabaseCopyStatus"); psSession.AddParameter("databasename"); powerShell.Commands = psSession; var results2 = powerShell.Invoke(); foreach (var item in results2) { MessageBox.Show(item.Members.ToString()); } }
In case someone comes across this in a search
string un = @"domain\username";
System.Security.SecureString pw = new System.Security.SecureString();
string password = "password";
string databaseName = "databasename";
string exchangeServerName = "http://exchangeserver.com/powershell";
string microsoftSchemaName = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
foreach (char ch in password)
{
pw.AppendChar(ch);
}
PSCredential cred = new PSCredential(un, pw);WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(exchangeServerName), microsoftSchemaName, cred); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { using (PowerShell powershell = PowerShell.Create()) { powershell.AddCommand("Get-Mailboxdatabasecopystatus"); powershell.AddParameter("identity", databaseName); powershell.AddCommand("select-object"); var props = new string\[\] { "name", "status" }; powershell.AddParameter("property", props); runspace.Open(); powershell.Runspace = runspace; Collection<PSObject> results = powershell.Invoke(); foreach (PSObject result in results) { MessageBox.Show(result.ToString()); } } }