[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2610
  • Last Modified:

Get-MailboxStatistics is not recognized as a cmdlet, function, operable program, or script file

Hi,
I have the code below in a custom vb.net application

if I run the vb.net app i get this error..

The term 'Get-MailboxStatistics -server exch.xyz.com | ft Identity, TotalItemSize' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

However if I go onto the server and open up PowerShell and run the command it works fine.

Please Help

Thanks....
Dim results As ICollection(Of PSObject)
                    Dim rc As RunspaceConfiguration = RunspaceConfiguration.Create()
                    Dim snapEx As PSSnapInException = Nothing
                    Dim info As PSSnapInInfo = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapEx)
                    Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(rc)
                    myRunSpace.Open()
                    Dim pipeLine As Pipeline = myRunSpace.CreatePipeline()
                    Using pipeLine
                        Dim getSize As New Command("Get-MailboxStatistics -server exch.xyz.com | fl Identity, TotalItemSize")
                        pipeLine.Commands.Add(getSize)
                        results = pipeLine.Invoke()
                        If results IsNot Nothing AndAlso results.Count > 0 Then
                            For Each ps As PSObject In results
                                If ps.Members("TotalItemSize").Value IsNot Nothing Then
                                    Dim tempsize As String
                                    tempsize = ps.Members("TotalItemSize").Value
                                    tempsize.Replace("B", "")
                                    tempsize = tempsize / 1024
                                    htExMBSizes.Add(ps.Members("Identity").Value, tempsize)
                                End If
                            Next
                        End If
                    End Using

Open in new window

0
manfredfield
Asked:
manfredfield
1 Solution
 
Chris DentPowerShell DeveloperCommented:

It's because "Command" either expects a simple, single CmdLet (i.e. no parameters) or for you to tell it that it is a script, allowing it to execute with parameters and further pipelines.

You'd use this Constructor:

http://msdn.microsoft.com/en-us/library/ms567348(VS.85).aspx

And it should make yours work with the modification below.

HTH

Chris
Dim results As ICollection(Of PSObject)
Dim rc As RunspaceConfiguration = RunspaceConfiguration.Create()
Dim snapEx As PSSnapInException = Nothing
Dim info As PSSnapInInfo = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapEx)
Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(rc)
myRunSpace.Open()
Dim pipeLine As Pipeline = myRunSpace.CreatePipeline()
Using pipeLine
  ' Modified constructor for Command to include boolean flag 
  ' indicating command is a script
 
  Dim getSize As New Command("Get-MailboxStatistics -server exch.xyz.com | fl Identity, TotalItemSize", True)
  pipeLine.Commands.Add(getSize)
  results = pipeLine.Invoke()
  If results IsNot Nothing AndAlso results.Count > 0 Then
    For Each ps As PSObject In results
      If ps.Members("TotalItemSize").Value IsNot Nothing Then
        Dim tempsize As String
        tempsize = ps.Members("TotalItemSize").Value
        tempsize.Replace("B", "")
        tempsize = tempsize / 1024
        htExMBSizes.Add(ps.Members("Identity").Value, tempsize)
      End If
    Next
  End If
End Using

Open in new window

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now