header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
cmdlet output vs returning objects
Last Post 02 Apr 2009 12:31 PM by Marco Shaw (MVP). 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Random BitsUser is Offline
New Member
New Member
Posts:6
Avatar

--
01 Apr 2009 03:20 PM

    Let's say we have a cmdlet get-foo

    get-foo -show config

    should show some formatted text as below

    Data Store Path: c:\foo\datastore
    Total Items: 100

    Command Successful.

    $a = (get-foo -show config)

    it should return a object that has two members
    strPath and intTotalItems

    The text in the labels should not be part of the return object.

    How do we handle this in cmdlet code?
    Steven MurawskiUser is Offline
    Community Co-Director
    Basic Member
    Basic Member
    Posts:137

    --
    01 Apr 2009 04:21 PM
    You can write logging messages to the Verbose stream or the Debug stream (either with the WriteVerbose method or WriteDebug method).

    If you need to write out warning messages, you would use WriteWarning.

    The result of your cmdlet would be written using the WriteObject method.

    Your resulting command would look more like:

    Get-Foo -Verbose

    or

    $a = (Get-Foo -verbose)
    Steven Murawski
    Blog ( blog.usepowershell.com )
    Co-Host - Mind of Root ( www.mindofroot.com )
    Random BitsUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    01 Apr 2009 05:00 PM
    Verbose or Debug output works with -verbose or -debug parameters only. I needed them in the standard output.

    I am glad I have figured it out myself. The cmdlet need to use a custom format file (*.format.ps1xml file). Defining the file is not very simple though. Needs some practice.

    But you can output anything you want without disturbing the returning objects structure.
    :)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    02 Apr 2009 12:31 PM
    Yes, I think you should be going the ETS route by creating a .ps1xml file specific to the type of objects you are dealing with.

    Need help with that?

    BTW... You shouldn't try editing the format.ps1xml files. You should create your own XML formatting file, and load that with the Update-TypeData cmdlet.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer