Stuart Barnett

Stuart Barnett

  • NA
  • 1
  • 1.8k

Calling Powershell Exchange cmdlet in VS2010

Sep 23 2011 8:32 AM
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.

The code works everywhere except where it's needed
I've tried a few permutations and getting nowhere, can anyone help?

Thanks

Stuart