header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

cmdlet output vs returning objects
Last Post 02 Apr 2009 08:31 PM by marco.shaw. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
randombitsUser is Offline
New Member
New Member
Posts:6
Avatar

--
01 Apr 2009 11: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?
smurawskiUser is Offline
Community Co-Director
Basic Member
Basic Member
Posts:137

--
02 Apr 2009 12:21 AM  
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 )
Director - Research and Development for ProPhoenix ( www.prophoenix.com )
randombitsUser is Offline
New Member
New Member
Posts:6
Avatar

--
02 Apr 2009 01:00 AM  
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.shawUser is Offline
Site Moderator
Advanced Member
Advanced Member
Posts:593
Avatar

--
02 Apr 2009 08: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.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer