Question Calling Powershell Exchange cmdlet in VS2010

stubar

Member
Joined
Nov 9, 2010
Messages
5
Programming Experience
1-3
Hello

I am trying to run a Powershell script in VS2010 on a 64bit 2008 R2 server with Exchange 2007 Tools installed; and failing miserably :(

The code is:

Dim res As String
Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
runspace.Open()
Using pipeline As Pipeline = runspace.CreatePipeline()
Dim myScript As String
myScript = vbCr & vbLf & "set-executionpolicy -scope CurrentUser -executionPolicy RemoteSigned -force" & vbCr & vbLf & _
"add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin" & vbCr & vbLf & _
"Get-Mailbox -DomainController Myserver.mydomain.com stest1"
Dim command As New Command(myScript, True)

pipeline.Commands.Add(command)

Dim results As Collection(Of PSObject) = pipeline.Invoke()

Dim i
For Each i In results
res = res & vbCrLf & i
Next

End Using
cmdOutputTextBox.Text = res


It fails at "PipeLine.Invoke" with:
The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program.

I've tried a few permutations and getting nowhere, can anyone help?

Thanks

Stuart
 

Latest posts

Back
Top