I have a Powershell Script which is stored in a string called "script" with the content:
get-user | out-file C:\Users\user\Desktop\user.txt -append
My C# Code:
RunspaceConfiguration runConfig = RunspaceConfiguration.Create();
PSSnapInException psEx = null;
runConfig.AddPSSnapIn("VMWare.View.Broker", out psEx);
Runspace runspace = RunspaceFactory.CreateRunspace(runConfig);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
Collection<PSObject> results = new Collection<PSObject>();
results = pipeline.Invoke();
runspace.Close();
If I debug the code I get the following exception:
No snap-ins have been registered for Windows Powershell Version 2
If I run the script manually and add the snap-in it works fine
Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.E2010 because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
has anyone encountered that? – Limann