Recently, a client maintenance tool for Exchange has encountered many problems.
Because of the new exposure to C # and exchange, you need to continue learning. Record here, just a beginner's record.
Environment:
Server: Exchange2010,window Server 2008R2
This machine: Win7 32-bit, VS2008
1. The Windows PowerShell snap-in "Microsoft.Exchange.Management.PowerShell.E2010" is not installed on this computer.
Because your computer is Win7 32-bit system, and Exchange service cmdlets module is X64, so error.
In order to solve this compatibility problem, began to want to remotely invoke the server's PowerShell, but because of their own caishuxueqian, did not succeed, hey.
Later I learned that C # and WCF this model (forgive my ignorance), and then began a variety of Google, degrees Niang.
Did a lot of testing, did not succeed. Started with IIS publishing, but after a variety of articles are configured, still cannot be called (for beginners, these configurations are cumbersome, and a small detail may cause the call to fail)
Then directly wrote the console application to invoke:
namespaceexconsole{classProgram {Static voidMain (string[] args) { using(ServiceHost Host =NewServiceHost (typeof(Service1))) { //bindingSystem.ServiceModel.Channels.Binding httpbinding =NewBasicHttpBinding (); //End PointHost.addserviceendpoint (typeof(Wcfservicelibrary1.iservice1), httpbinding,"http://10.100.0.50:8888/"); if(host.description.behaviors.find<system.servicemodel.description.servicemetadatabehavior> () = =NULL) { //BehaviorServiceMetadataBehavior behavior =NewServiceMetadataBehavior (); Behavior. Httpgetenabled=true; //Meta data addressBehavior. Httpgeturl =NewUri ("Http://10.100.0.50:8888/Service1"); HOST.DESCRIPTION.BEHAVIORS.ADD (behavior); //StartHost.open (); Console.WriteLine ("The WCF service has started, do not close this window"); Console.read (); } } } }}
When calling, add Service Reference, address with Http://10.100.0.50:8888/Service1 this will do.
Used in 2.Service, the method of calling PowerShell
Public voidAddexchangeuser (stringLoginNamestringdb) {runspaceconfiguration RsConfig=runspaceconfiguration.create (); Pssnapinexception snapinexception=NULL; Pssnapininfo Info= Rsconfig.addpssnapin ("Microsoft.Exchange.Management.PowerShell.E2010", outsnapinexception); Runspace Myrunspace=Runspacefactory.createrunspace (rsConfig); Myrunspace.open (); Pipeline Pipeline=Myrunspace.createpipeline (); stringcmd ="enable-mailbox-identity yourdomin\\"+ LoginName +"-database"+DB; Command mycommand=NewCommand (CMD,true); PIPELINE.COMMANDS.ADD (mycommand); Collection<PSObject> Commandresults =Pipeline.invoke (); }
3. Other points of attention:
System.Management.Automation.dll must refer to the 64-bit,
Path: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\ System.Management.Automation.dll
Open the Gac_msil folder method: Start-run-input C:\Windows\assembly\GAC_MSIL\, and then I copied the DLL file to another path, right, I do not know, it can actually use
WCF server-side programs: Target platform should be set to: x64
Initial WCF Experience (C # operations Exchange)