I'm using the following code (It takes scripttext and passes it to powershell ) on a 64 Bit system using Visual Studio 2010 Express Basic. The problem I'm encountering is it won't load the Exchange snap-in. I believe it's debugging in 32 Bit, but I don't know how to force it to 64. Any help would be greatly appreciated.
Private Function RunScript(ByVal scriptText As String) As String
' create Powershell runspace
Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
Dim snapInException As PSSnapInException = Nothing
Dim info As PSSnapInInfo
info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapInException)
Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(rsConfig)
myRunSpace.Open()
' create a pipeline and feed it the script text
Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript(scriptText)
' add an extra command to transform the script output objects into nicely formatted strings
' remove this line to get the actual objects that the script returns. For example, the script
' "Get-Process" returns a collection of System.Diagnostics.Process instances.
MyPipeline.Commands.Add("Out-String")
' execute the script
Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
' close the runspace
MyRunSpace.Close()
' convert the script result into a single string
Dim MyStringBuilder As New StringBuilder()
For Each obj As PSObject In results
MyStringBuilder.AppendLine(obj.ToString())
Next
' return the results of the script that has
' now been converted to text
Return MyStringBuilder.ToString()
End Function