none
The term 'Out-String' is not recognized as the name of a cmdlet, function, script file, or operable program. RRS feed

  • Question

  • I have a program that run different Powershell commands against Exchange.

    It works fine with Exchange 2007, and now I'm converting it so it can also work with Exchange 2010.

    I'm trying to run scripts like the following;
    "Get-ActiveSyncDeviceStatistics -mailbox User1 | fl devicetype, devicemodel, LastSuccessSync, DeviceIMEI, identity, DeviceUserAgent | Out-string -width 300"

    When doing this I'm getting an error that "out-string" and also "fl" are not recognized.

    If I just run the "Get-ActiveSyncDeviceStatistics -mailbox User1 " then this works fine, looks like only Exchange commands works?

    I used the following code to do this;

            Const SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"""
            Dim serverUri As New Uri("http://server1/Powershell")

            Dim securepass As New System.Security.SecureString
            Dim c As Char
            For Each c In sExPassword
                securepass.AppendChar(c)
            Next
            Dim remotecred As New PSCredential(sExUsername, securepass)

            Dim connectionInfo As New WSManConnectionInfo(serverUri, SHELL_URI, remotecred)
            Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)
            Dim psresult As Collection(Of PSObject)
            myRunSpace.Open()

            Dim psh As PowerShell = PowerShell.Create()
            psh.Runspace = myRunSpace
            script = "Get-ActiveSyncDeviceStatistics -mailbox User1 | fl devicetype, devicemodel, LastSuccessSync, DeviceIMEI, identity, DeviceUserAgent | Out-string -width 300"

            Dim pipeLine As Pipeline
            pipeLine = psh.Runspace.CreatePipeline(script)

            psresult = pipeLine.Invoke()

            Dim MyStringBuilder As New StringBuilder()
            Dim sValues() As String
            Dim sTmp As String
            If psresult.Count > 0 Then
                For Each obj As PSObject In psresult
                    MyStringBuilder.AppendLine(obj.ToString())
                Next
                sTmp = MyStringBuilder.ToString
                sValues = sTmp.Split(vbCrLf)
                Return sValues(0)
            Else
                Return "No info"
            End If
            psh.Dispose()
            myRunSpace.Close()

    I hope someone can help me with this.

    Wednesday, April 4, 2012 6:35 AM

All replies

  • There are few different methods you can use to make this work the first is if your running the code directly on the Exchange Server then you can take the code you have for 2007 and just change the line to load the Exchange 2010 Snap in eg

    AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010",

    If you want to use what you have posted the stuff thats being done by " | fl devicetype, devicemodel, LastSuccessSync, DeviceIMEI, identity, DeviceUserAgent | Out-string -width 300" is only formatting which you can do yourself in Vb.Net by just processing the PSObjects in the Results collections.

    Or if your really attached to this method you can do something like this http://blogs.msdn.com/b/akashb/archive/2010/03/26/how-to-call-ps1-script-from-managed-code-using-remote-powershell-exchange-2010.aspx

    Cheers
    Glen

    Thursday, April 5, 2012 5:07 AM