Question : 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....
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
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 Select All

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


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
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
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 Select All
Random Solutions  
 
programming4us programming4us