Help with piping a command output to a variable

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6501

    by gte at 2013-01-11 13:19:13

    Hello,

    I'm new to powershell but am well versed in C sharp so I'm not new to programming. I can't figure out how to pipe the output of this command into a variable instead of a text file, so that I can then join it with another variable and write a single output line to a text file.

    Get-MailboxDatabase -Identity $dbpath | Get-Mailbox | get-random | Get-NamedProperty | FT PropertyID >> "c:\Temp\mbdboutput1.txt"

    Something along the lines of what's below. Thanks for reading.


    $server = [value from foreach loop]
    $propId = Get-MailboxDatabase -Identity $dbpath | Get-Mailbox | get-random | Get-NamedProperty | FT PropertyID

    $server + " " + $propId >> "c:\Temp\mbdboutput1.txt"

    by ArtB0514 at 2013-01-14 06:25:02

    Whenever something in PowerShell doesn't do what you expect, one of the first things to do is to run Get-Help -Full (or -Online) on the command that's causing trouble. In your case, the problem command is Format-Table. If you check the OUTPUTS description for Format-Table, you will see that it produces [quote]OUTPUTS
    Microsoft.PowerShell.Commands.Internal.Format
    Format-Table returns format objects that represent the table.[/quote]
    This means that your $propID object doesn't contain the properties that you are looking for, but instead has PowerShell internal format objects. What you really want to do is this]$propId = Get-MailboxDatabase -Identity $dbpath | Get-Mailbox | get-random | Get-NamedProperty
    Format-Table $propID[/powershell]

    by gte at 2013-01-14 07:19:17

    Hello,

    Thanks for the reply. The code you provided is still giving me an error. I have Powershell 1 if that makes a difference.

    I did do the get-help command as you suggested after Get-NamedProperty and I see the following

    [quote]
    $propId = Get-MailboxDatabase -Identity $dbpath | Get-Mailbox | get-random | Get-NamedProperty < <<< get-help
    + CategoryInfo : InvalidArgument: (Lipscomb, Connie:PSObject) [Get-NamedProperty], ParameterBinding
    Exception
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.Exchange.Management.Powershell.Support.GetNamedPrope
    rty
    [/quote]

    by ArtB0514 at 2013-01-15 06:22:43

    I assume that you are running the Exchange Management Shell. You might want to read the primer: http://technet.microsoft.com/en-us/library/bb245704(EXCHG.80).aspx. You might also want to check out http://social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx.

    Get-Help is not a property that you can feed into the -Identity parameter for Get-NamedProperty. Try it this way:

    Get-Help Get-NamedProperty -Full

    by gte at 2013-01-15 10:08:30

    Ok, thank you

You must be logged in to reply to this topic.