Register
: :
Login
Home
Forums
Blogs
Podcast
Directories
Scripts
Downloads
Community
User Group Support
Learning Resources
We have a new sponsor! Introducting
Pragma Systems
. See the home page for details.
Unanswered
Active Topics
Forums
Search
Members
Forums
>
PowerShell Development
>
Cmdlet Development
cmdlet output vs returning objects
Last Post 02 Apr 2009 08:31 PM by
marco.shaw
. 3 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
randombits
New Member
Posts:6
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?
smurawski
Community Co-Director
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 )
randombits
New Member
Posts:6
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.shaw
Site Moderator
Advanced Member
Posts:593
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.
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
Forums
>
PowerShell Development
>
Cmdlet Development
Active Forums 4.1
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008