Windows Vista Forums

Odd formatting issues
  1. #1


    Jonathan Kalmes Guest

    Odd formatting issues

    Hey all,

    I'm trying to get a handle on some basic scripts I'm using to monitor
    our systems. I can get them "functioning" correctly, but a number of
    them aren't pretty. The problem seems to be that the first time a pipe
    terminates within a loop, headers are written to the output. This is
    fine as long as I'm adding any other text within the loop before the
    first pipe output is dumped.

    -Do I need to strip off the table headers somehow and manually write
    them out before entering the loop (which seems counter-intuitive, as I'd
    have to manually re-write the headers any time I changed the output
    fields I'm looking for)?



    ....or...

    -Is there something simple I'm missing to get my text in under the
    header but before the data?

    An example script is attached (it checks Exchange queues, so you'll need
    appropriate security access to an Exchange 2003 server if you plan to
    run it). If you run it against more than one server, it puts the first
    server name above the header list. For more complex stuff, that means I
    have to "doctor" the output before sending it to anyone, which is
    starting to get kind of irritating. :S

    --Jonathan "smthng" Kalmes
    http://smthng.info

    #---Begin PS Script------
    #If you want to use a text file for server names,
    #just uncomment the next line and comment the one after that.

    #$computers = Get-Content c:\scripts\servers.txt
    $computers = "Server1","Server2","Server3"
    Foreach ($objItem in $computers){
    Write-host $objItem -foregroundcolor Cyan
    Get-wmiobject -class exchange_SMTPQueue -Namespace `
    ROOT\MicrosoftExchangev2 -ComputerName $objItem | `
    Select-Object LinkName,QueueName,MessageCount,Size | `
    Where-Object -FilterScript {$_.MessageCount -ne 0} | `
    Sort-Object QueueName
    }
    Read-Host -prompt "Press <Enter> to close:"
    #-------End PS Script--------

      My System SpecsSystem Spec

  2. #2


    Rob Campbell Guest

    RE: Odd formatting issues

    If you're just trying to make it look nicer you can add

    | ft

    at the end of the Get-WmiObject statement, after the sort. It won't get the
    server's entry to look like the subsequent entries, but it will get the
    subsequent entries to look like the first one.

    #---Begin PS Script------
    #If you want to use a text file for server names,
    #just uncomment the next line and comment the one after that.

    #$computers = Get-Content c:\scripts\servers.txt
    $computers = "Server1","Server2","Server3"
    Foreach ($objItem in $computers){
    Write-host $objItem -foregroundcolor Cyan
    Get-wmiobject -class exchange_SMTPQueue -Namespace `
    ROOT\MicrosoftExchangev2 -ComputerName $objItem | `
    Select-Object LinkName,QueueName,MessageCount,Size | `
    Where-Object -FilterScript {$_.MessageCount -ne 0} | `
    Sort-Object QueueName | ft
    }
    Read-Host -prompt "Press <Enter> to close:"
    #-------End PS Script--------


    "Jonathan Kalmes" wrote:

    > Hey all,
    >
    > I'm trying to get a handle on some basic scripts I'm using to monitor
    > our systems. I can get them "functioning" correctly, but a number of
    > them aren't pretty. The problem seems to be that the first time a pipe
    > terminates within a loop, headers are written to the output. This is
    > fine as long as I'm adding any other text within the loop before the
    > first pipe output is dumped.
    >
    > -Do I need to strip off the table headers somehow and manually write
    > them out before entering the loop (which seems counter-intuitive, as I'd
    > have to manually re-write the headers any time I changed the output
    > fields I'm looking for)?
    >
    > ....or...
    >
    > -Is there something simple I'm missing to get my text in under the
    > header but before the data?
    >
    > An example script is attached (it checks Exchange queues, so you'll need
    > appropriate security access to an Exchange 2003 server if you plan to
    > run it). If you run it against more than one server, it puts the first
    > server name above the header list. For more complex stuff, that means I
    > have to "doctor" the output before sending it to anyone, which is
    > starting to get kind of irritating. :S
    >
    > --Jonathan "smthng" Kalmes
    > http://smthng.info
    >
    > #---Begin PS Script------
    > #If you want to use a text file for server names,
    > #just uncomment the next line and comment the one after that.
    >
    > #$computers = Get-Content c:\scripts\servers.txt
    > $computers = "Server1","Server2","Server3"
    > Foreach ($objItem in $computers){
    > Write-host $objItem -foregroundcolor Cyan
    > Get-wmiobject -class exchange_SMTPQueue -Namespace `
    > ROOT\MicrosoftExchangev2 -ComputerName $objItem | `
    > Select-Object LinkName,QueueName,MessageCount,Size | `
    > Where-Object -FilterScript {$_.MessageCount -ne 0} | `
    > Sort-Object QueueName
    > }
    > Read-Host -prompt "Press <Enter> to close:"
    > #-------End PS Script--------
    >


      My System SpecsSystem Spec

  3. #3


    Jonathan Kalmes Guest

    Re: Odd formatting issues

    Rob's right, but that's not what I'm looking for either. Slapping a
    Format-Table on the end of the get-wmiobject statement did indeed add
    column headers to each group of server output. I knew that wasn't what
    I was looking for, but it got me thinking so I fiddled a bit and tried
    Format-Table -hideTableHeaders instead. That got rid of the initial
    header in my output, but also added blank lines all over the place.
    What's up with that? Here's my output if I don't pipe into any kind of
    Format-cmdlet ( stripped the last column so it would fit):

    SERVER1

    LinkName QueueName MessageCount
    -------- --------- ------------
    SERVER10.WAS.INT... SERVER10.WAS.INT... 6
    SERVER2
    DeferredDelivery... DeferredDelivery... 7
    SERVER10.WAS.INT... SERVER10.WAS.INT... 1
    SERVER3
    SERVER10.WAS.INT... SERVER10.WAS.INT... 1
    <snip>
    Press <Enter> to close::

    Reasonably decent other than the one odd header. That's specifying no
    formatting other than having the Select-object spec out only 4 fields.
    But...

    If I try to change the formatting at all by using the format-table
    cmdlet, it makes it worse by padding out the entire list with blank
    lines, even if I'm using the -hideTableHeaders. So, I guess that leaves
    me with three questions....

    Where's the header coming from in my original code and why is it only
    happening once?

    Why does Format-table add extra lines (CRs) even with the
    -hideTableHeaders param?

    Is there any way for me to do this kind of process without sending all
    the values to an array first and then parsing the array? I'm trying to
    avoid this, because that kind of defeats the purpose of piping the
    objects in the first place.

    Sorry to beat this to death, but I generate these kinds of quick status
    tables ALL the time... this one is just one example. I really would
    like to get this working... it'll fix 10 other scripts I've got just
    like it and help me understand how PoSH deals with output.

    --Jonathan "smthng" Kalmes
    http://smthng.info

    Rob Campbell wrote:
    > If you're just trying to make it look nicer you can add
    >
    > | ft
    >
    > at the end of the Get-WmiObject statement, after the sort. It won't get the
    > server's entry to look like the subsequent entries, but it will get the
    > subsequent entries to look like the first one.


      My System SpecsSystem Spec

Odd formatting issues problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting kman32 General Discussion 9 28 Mar 2010
formatting cd-rw skyman Vista General 1 23 Feb 2008
RE: Formatting Glenn Vista music pictures video 1 23 Jan 2008
HELP! formatting cds DanniL Vista music pictures video 2 17 Jul 2007
Issues formatting drive for installation SuperPlay Vista installation & setup 7 14 Jun 2006