Gosh that is a horrible looking thing. This is one of the things we fixed with PowerShell V2 so you take a few minutes to
learn this so you never have to do that mess again. Let’s get some help:
PS> Get-Help New-Object -Parameter Property
-Property <hashtable>
Sets property values and invokes methods of the new object. Enter a hash table in which the keys are the names of properties or methods and the values are property value s or method arguments. New-Object creates the object and sets each property value and invokes each method in the order that they appear in the hash table. If the new object is derived from the PSObject class, and you specify a property that does not exist on the o bject, New-Object adds the specified property to the object as a NoteProperty. If the object is not a PSObjec t, the command generates a non-terminating error. Required? false Position? named Default value Accept pipeline input? false Accept wildcard characters? false
Thats right – you can now specify a hashtable and we’ll set these as NoteProperties on an object. What that means is that you can now write code like this:
or if you prefer:
This is SO much better than V1 – be sure to use it and to tell all your friends about it as well!
Experiment! Enjoy! Engage!
Jeffrey Snover [MSFT]
Distinguished Engineer Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
This a data type conversion issue.
I am trying to get the names of computers out of SCCM and feed it into SCCM Report. The report commandlet receives hashtable where the variable must be named 'Computer Name'. The value is the computer name.
Convert Hashtable To Array Powershell Word
Outputing Commandlet example (get computer names from SCCM) This works.
Recieving Commandlet example (process the query) This works:
I need the 'Get-CMDevice' line to output as the type below.
My failed attempt
Error output:
Aaron
AaronAaron
1 AnswerSelect-Object will always output a PSCustomObject , not a hashtable.
Just construct the hashtable inside the Mathias R. JessenMathias R. Jessen
ForEach-Object body before calling Invoke-CMReport :
63k55 gold badges7272 silver badges118118 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged powershellsccm or ask your own question.
Summary: Take your Windows PowerShell scripting to the next level by combining hash tables and arrays.
Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are anxiously counting down the days until the first ever Pittsburgh PowerShell Users Group meeting. The event is quickly selling out; therefore, if you want to attend, you will need to sign up for the meeting. The reason for the reservation is that space at the Pittsburgh Microsoft Office is limited. The meeting will be a lot of fun.
This past week, I talked about working with arrays. Although Windows PowerShell makes working arrays very easy, there are still lots of little things that need special attention. The five articles in the series devoted to working with Arrays in Windows PowerShell are listed here:
Today, I want to look at creating an array that contains hash tables. Suppose I create a couple of hash tables, such as the ones that are shown here.
$a = @{“one”=1;”two”=2}
$b = @{“three”=3;”four”=4}
If I want to create an array with these two hash tables, it is as simple as using the equality operator, and assigning them to a variable by using standard array techniques. This command is shown here.
$c = $a,$b
The view of this array of hash tables appears to be a bit strange—it looks like I am looking at a hash table. This is shown here.
PS C:> $c
Name Value
—- —–
two 2
one 1
three 3
four 4
The cool thing is that I can index into the elements of my array by using square brackets as shown here.
PS C:> $c[0]
Name Value
—- —–
two 2
one 1
Because each element of the array contains a hash table, I can index into the array, and then use “hash table kinds of commands” such as querying the Keys or Values properties or using the Itemmethod. These commands are shown here.
PS C:> $c[0].values
2
1
PS C:> $c[0].keys
two
one
PS C:> $c[0].item(“one”)
1
The commands that create two hash tables, add the hash tables to an array, and then index into the array to work with the hash tables are shown in the following image.
If I am only interested in the keys that the hash tables contain, I can pipe the array to the Select-Object cmdlet. This technique is shown here.
PS C:> $c | select keys
Keys
—-
{two, one}
{three, four}
Although the output does contain the keys, it is not the best output. I can clean things up a bit by using the ExpandProperty parameter. This command is shown here.
PS C:> $c | select -ExpandProperty keys
two
one
three
four
That is about all there is to working with an array of hash tables. Join me tomorrow as I begin a new week with a special guest blog post by Karl Mitschke.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at [email protected], or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
How do you format an array of hash tables with the Format-Table cmdlet?
Example:
Desired Output:
Actual Output:
Joseph SturtevantJoseph Sturtevant
9,96599 gold badges6161 silver badges8585 bronze badges
1 AnswerVimes
3,84188 gold badges4242 silver badges7474 bronze badges
Powershell Hashtable To Jsonmjolinormjolinor
51.8k33 gold badges7474 silver badges103103 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Powershell Convert Table To ArrayNot the answer you're looking for? Browse other questions tagged powershellformatting or ask your own question.
Archived
So I am doing this thing where I need to generate an HTML report in Powershell. I know how to take a basic object and do converto-html on it, but I have no idea how to do this when the object contains a property which is a hash table.
I would like the format of the report to be a table, but how would I enumerate the keys and values of the property into a cell in this table? Can you embed a table within the cell which would have the key value pairs? Is there a better way to do this?
I really have no clue.
Thanks!
4 comments
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |