locked
Powershell module in C# RRS feed

  • Question

  • Hi guys,

    I write Powershell module in C#. When I write cmdlets i want export pieces of information to console (write-host) but with Table. 
    I don't know how I can create table in C# for powershell Module. I want table fore xample

    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
    -------  ------    -----      ----- -----   ------     -- -----------
        435      30    28008      24260 ...96   119,23   3472 ApplicationFrameHost
       7326      42    31260      19588 ...10 ...80,94   2964 audiodg
        401      17     5056      16264 ...52     0,30  33980 browser_broker
        224      18    21476      16576   195     0,14  49656 CalculateSubnet.vshost
        356      24    16272         76   268     0,28   8720 Calculator
        220      21    43664      41204   777     5,50    328 chrome
        241      25    70268      62804   869     5,56  16560 chrome
        216      20    45184      34240   773     1,81  16960 chrome
        251      27    79232      70816   901    53,78  23316 chrome
        225      24    75396      88692   818     3,95  27128 chrome
        224      21    42656      32088   766     1,23  30480 chrome
        226      22    56720      35204   781     2,33  32008 chrome
        243      29   117432      95516   864    73,75  32192 chrome
        247      22    40276      35304   841     1,89  37004 chrome
        247      32   146424      53708   890    24,78  38876 chrome
        221      25    76968      40772   806     3,86  40184 chrome
        220      21    52124      33920   779     1,84  44000 chrome
        232      23    46772      38980   788     5,73  44320 chrome
        217      24    75536      36792   803     4,72  45284 chrome
        223      24    69592      39436   802     3,16  45432 chrome
        270      29    90156      67552   994    13,28  47472 chrome
        244      23    51444      36656   853     6,77  48020 chrome
        235      26    93720      57056   842    25,91  48740 chrome
        539      51   259764     219316   569   134,03  49848 chrome
        268      27    77860      62688   941   119,97  50380 chrome
        221      21    67424      79748   783    10,03  50612 chrome
       3719     107   132528     163836   701   278,81  51132 chrome

    Do you have solutions for me?

    Thanks

    Monday, September 28, 2015 1:35 PM

Answers

  • PS isn't really for displaying data but rather using .NET to perform command line actions.  In almost all cases you should be returning back a set of .NET objects so that your command can be chained to others like for each or piping.  One of the side effects of passing back a list of objects is that if the user decides to display the data then they get the table automatically because PS will enumerate your items and dump each public property.  Hence if you want the above table then create an object that has a public property for each of your columns. Returning back an IEnumerable of that type will allow PS to display the above info, if the user opts to do that.  More importantly, the user can change the formatting as documented here - https://technet.microsoft.com/en-us/library/dd347677.aspx?f=255&MSPPError=-2147217396.

    So, in summary, create a .NET type to represent the columns you have in the table, return an IEnumerable list of this type (1 object for each row). PS should display the table auto-magically unless the user captures it.  You shouldn't be calling Write-Host directly unless you need to display some extra info.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Kristin Xie Tuesday, September 29, 2015 9:12 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:38 AM
    Monday, September 28, 2015 1:59 PM