Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format-table and properties that are arrays

Tags:

powershell

i use this powershell command:

get-vm | ft name, *start*, *stop*, customproperties

that returns objects with a string array as a property (customproperties):

Name                StartAction DelayStart      StopAction CustomProperties
----                ----------- ----------      ---------- ----------------
TKAD4        AlwaysAutoTurnOnVM          0 ShutdownGuestOS {NoStartupDelay, ...
TKAD3        AlwaysAutoTurnOnVM          0 ShutdownGuestOS {NoStartupDelay, ...

how can i return just one element from an array the is a property as an object to display it as part of a table?

my desired output would look like this:

Name                StartAction DelayStart      StopAction        Custom1
----                ----------- ----------      ----------        -------
TKAD4        AlwaysAutoTurnOnVM          0 ShutdownGuestOS NoStartupDelay
TKAD3        AlwaysAutoTurnOnVM          0 ShutdownGuestOS NoStartupDelay
like image 207
longneck Avatar asked Jul 27 '12 20:07

longneck


People also ask

What is the use of Format-Table?

The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.

How do I declare an array in PowerShell?

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator ( = ). The comma can also be used to initialize a single item array by placing the comma before the single item.

How do I use AutoSize in PowerShell?

If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.

How do I add an item to an array in PowerShell?

To add value to the array, you need to create a new copy of the array and add value to it. To do so, you simply need to use += operator. For example, you have an existing array as given below. To add value “Hello” to the array, we will use += sign.


1 Answers

In your Format-Table, change customproperties to either:

@{label='Custom1';e={$_.CustomProperties[0]}}

If it is an array. If it is a collection use:

@{label='Custom1';e={$_.CustomProperties | Select -First 1}}
like image 145
Keith Hill Avatar answered Oct 03 '22 07:10

Keith Hill