Start powershell ExchangeManagementShell from code

jwradhe

Member
Joined
Sep 23, 2021
Messages
7
Programming Experience
Beginner
Hi!
Im building a little app, and im want it to open our ExchangeManagementShell from inside the code if a checkbox i selected.
I have a scortcut on desktop to open this normaly, and im trying to copy what its opening:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell "


im trying to to this:
C#:
var ps1File = @"'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'";

var startInfo = new ProcessStartInfo()
{
       FileName = "powershell.exe",
       Arguments = $"-noexit -command . \"{ps1File}\"; Connect-ExchangeServer -auto -ClientApplication:ManagementShell ",
       //UseShellExecute = false
};
Process.Start(startInfo);

And when at first get this working, i want to add a line and run there.

What am i doing wrong here?
 
Notice where the double quotes are in your inline code that you got from the shortcut as opposed to your line 6. There is a double quote before the '.' and one after the space at the end.

Once you get that working, you can add another line by putting semicolon where that trailing space is at, and then your new command that you want to execute.

Alternatively you can try hosting the PowerShell engine like what @demetsen has been doing in his threads recently.
 
Or maybe do it another way, is this correct?


C#:
                                    using (PowerShell PowerShellInstance = PowerShell.Create())
                                    {
                                        PowerShellInstance.AddCommand("Set-ExecutionPolicy")
                                            .AddParameter("Scope", "CurrentUser")
                                            .AddParameter("ExecutionPolicy", "Bypass");
                                        PowerShellInstance.AddCommand("$SESSION=New-PSSession")
                                            .AddParameter("ConfigurationName", "Microsoft.Exchange")
                                            .AddParameter("ConnectionUri", "http://serveraddress/PowerShell")
                                            .AddParameter("WarningAction", "SilentlyContinue");
                                        PowerShellInstance.AddCommand("Import-PSSession")
                                            .AddArgument("$SESSION")
                                            .AddParameter("WarningAction", "SilentlyContinue");
                                        PowerShellInstance.AddCommand("Enable-RemoteMailbox")
                                            .AddArgument(fullmailaddress)
                                            .AddParameter("PrimarySmtpAddress", fullmailaddress)
                                            .AddParameter("RemoteRoutingAddress", mailnickname + "@domain.mail.onmicrosoft.com")
                                            .AddParameter("DomainController", domaincontroller);
                                        PowerShellInstance.Invoke();
                                    }
 
Nice. I usually just zone out during the TUB meetings that Microsoft does while they cover Exchange, but I vaguely recall them saying something about not requiring to do remoting sessions anymore for Exchange management. Like I said, I zone during that time so I don't know what the timeline for that was going to be.
 
I Get this now on the row in bold.

---------------------------
Could not create O365 account: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index
---------------------------
OK
---------------------------

C#:
var session = newSession[0];
PowerShellInstance.Commands.Clear();
PowerShellInstance.AddCommand("Import-PSSession")
.AddParameter("Session", session)                                         
 .AddParameter("WarningAction", "SilentlyContinue")
 .Invoke();



C#:
using (PowerShell PowerShellInstance = PowerShell.Create())
                                    {
                                        PowerShellInstance.AddCommand("Set-ExecutionPolicy")
                                            .AddParameter("Scope", "CurrentUser")
                                            .AddParameter("ExecutionPolicy", "Bypass")
                                            .Invoke();
                                        MessageBox.Show("Set-ExecutionPolicy");
                                        var newSession = PowerShellInstance.AddCommand("New-PSSession")
                                            .AddParameter("ConfigurationName", "Microsoft.Exchange")
                                            .AddParameter("ConnectionUri", "http://okdexchgp02.ad.oriola/PowerShell")
                                            .AddParameter("WarningAction", "SilentlyContinue")
                                            .Invoke();
                                        MessageBox.Show("New-PSSession");
                                        var session = newSession[0];
                                        PowerShellInstance.Commands.Clear();
                                        PowerShellInstance.AddCommand("Import-PSSession")
                                            .AddParameter("Session", session)                                         
                                            .AddParameter("WarningAction", "SilentlyContinue")
                                            .Invoke();
                                        MessageBox.Show("Import-PSSession");
                                        PowerShellInstance.AddCommand("Enable-RemoteMailbox")
                                            .AddParameter("Identity", fullmailaddress)
                                            .AddParameter("PrimarySmtpAddress", fullmailaddress)
                                            .AddParameter("RemoteRoutingAddress", mailnickname + "@oriola365.mail.onmicrosoft.com")
                                            .AddParameter("DomainController", domaincontroller)
                                            .Invoke();
                                        MessageBox.Show("Enable-RemoteMailbox");
                                    }
 
That is an Exchange question, not a C# question.

The best you can do is try the same set if commands manually instead of coded. If you get the same error, then there is something wrong with your commands. If you don't get an error, double check that what you did manually is exactly the same as what you have coded. If they are exactly the same, time to open a support case.
 

Latest posts

Back
Top Bottom