Cannot disable or enable an Exchange mailbox running a Powershell command inside C#
I have a couple of powershell command that are very simple.
disable-mailbox dadelgad -confirm:$false
enable-mailbox -identity 'dadelgad' -database 'NET5014DB10' -Alias 'dadelgad'
The first command is to disable an exchange mailbox and the second enables the mailbox. I am logged in as a user who is in the Organization Management group which has full admin priviledges to Exchange but is not a domain admin. If I run these commands directly in Powershell, they work fine but they do not work when called from C#.
I created a very simple windows forms app that has a couple of buttons that invokes these commands from C# code. Running the app as the user with full Exchange right, most commands work with no problem such as get-mailbox -identity 'dadelgad'. I can set flags in Exchange, add alias emails and do most functions but I cannot disable or enable an account.
Do I need to be a domain admin to do thes开发者_JS百科e functions. It almost seems like a permission issue but the user has full rights to Exchange and can perform both of these commands directly in Powershell.
Any help would be appreciated?
I finally figured out what was causing this problem so I am passing the info on to you in case you run into the same problem. I found the solution on serverfault.com at this url. What was happening is the user that I was logged in as and running the program was being blocked by the UAC (User Access Control). Turning it off solved the problem. Well, not really solved as I shouldn't run wide open like this but told me what the problem was. Now I need to go back and see if I can tweak the permissions to allow the program to run but also provide protection.
I used the code below to create a mailbox and I have altered it for enabling a mailbox and it should work for disabling as well. Make sure to find a better way to secure the password to the account than just hardcoding it.
SecureString password = new SecureString();
string username = "youruseraccount";
string str_password = "thepassword";
string exchangeserver = "yourexchangeserver";
string liveIdconnectionUri = "http://" + exchangeserver +"/Powershell?serializationLevel=Full";
foreach (char x in str_password) {
password.AppendChar(x);
}
PSCredential credential = new PSCredential(username, password);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
runspace.Open();
powershell.Runspace = runspace;
PSCommand command1 = new PSCommand();
command1.AddCommand("Enable-Mailbox");
command1.AddParameter("Identity", "dadelgad");
command1.AddParameter("Database", "NET5014DB10");
//Add as many parameters as you need
powershell.Commands = command1;
powershell.Invoke();
To run additional commands create a new PSCommand, add it the powershell instance the same way but after the previous invoke and invoke the powershell again.
Greg
精彩评论