You don't use this at all:
config.AddPSSnapIn("your snapin here", out psEx);
instead.... just use a connection as follows:
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://yourdomainhere/Powershell/Microsoft.Exchange"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", PsCreds);
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
Now run your commands and you're good to go.
Quick notes:
- Make sure you're app is targeting
x64
notAny CPU
orx86
- If you're using .net 4.5 (or 4) make sure you're app pools are set properly (v4.0 not v2.0) and that you have
Enable 32bit apps
set to false