0:01
Today I want to talk about the differences between hash tables and PowerShell custom objects
0:06
While they may look very similar, their functionality and the things you can do with them are quite different
0:12
As always, this code is available out in my GitHub and the Jeff Brown Tech YouTube repository
0:17
and I'll have a link to that down in the video description. Let's go ahead and get started
0:22
First, let's take a look at hash tables and how you can define a hash table
0:27
Start online too, we have a variable, and then you define a hash table. hash table with an at sign and curly brackets, and then you have a series of key value pairs
0:36
key equals, and then the value. Line two, you can do this in a single line
0:41
You just have to make sure you put a semicolon in between each one. Or if you want to do multiple lines, you just have to do a key value pair on each line
0:48
as you can see, starting on line five. You can also create an empty hash table if you want to add values to it later
0:56
So let's go ahead and highlight this line here, 13. You just have to do an at sign and curly brackets
1:02
So I have an empty hash table right now. You can then add values to it using the dot add method
1:09
You just have to send in first the key name, in this case it's server 5
1:14
and then the value of what that is equal to. First, before we add this, let me go back up here
1:19
and let's actually use this version of my hash table. And let's just take a look at what that looks like when you output it to the screen
1:27
First, we have all of our key values here in the name column and then our values with the value that matches up with each of those
1:35
Now let's go back through. Let's do the add method and add that to our hash table
1:40
So let's just display it out again and we can see we now have Server 5
1:44
And then if we want to, we can remove a key value pair using the dot remove method
1:50
And we just specify the key or the name that we want to remove. In this case, it's going to be Server 2
1:55
We'll run that. and let's look at our hash table again, and now we can see Server 2 is no longer in our hash table
2:03
Now with hash tables, we can't have duplicate values. If we try to go back in here and add in Server 1
2:10
which already exists in our hash table, this should display an error saying that item has already been added
2:16
in our dictionary. So can't have duplicate values there. One of the things you might notice about hash tables
2:23
is they are not ordered. In this case, we've been defining our hash table we did server one, two, three, four
2:30
And as we've been displaying it, it has been keeping that order when we've outputted it
2:35
to the screen here, but that's always not the case. Let's take this hash table here and go ahead and put it in
2:43
And let's go ahead and clear the screen, and let's see if this will display in different order
2:49
And sure enough, it does, you can see we have actually second, first and then third, and
2:53
if we do this again, I'll probably keep doing that same order
2:56
This can be annoying if you create a hash table and you want it to be displayed in a certain order
3:01
when you put it out to the screen. You can fix this if you want by turning it into an order dictionary using the ordered keyword
3:12
Again, let's take this. We'll create a new hash table here named ordered hash table
3:16
We've put in our ordered keyword. And now if we try to display that to the screen, it actually keeps it in the order that we defined it
3:25
So far we've been looking at our hash tables. We have the name or the keys and then values associated with them
3:31
You can display one or the other if you want to just using dot notation
3:35
So I can say my hash table. Dot keys. That's going to output our server names that we have
3:41
Or if we want to just look at the values, we're looking at some serial numbers that are
3:45
associated with each server name. Exporting hash tables can be a little tricky It may not be in the format or order that you want it to be Let take a quick look at what that might look like So we take my hash table pipe it over to export CSV and give our file name here
4:02
I always put on no type information. I think that's by default now, but old habits are hard to break
4:08
So let me do this one here, and let's go take a look at the file
4:13
Now, it does have all of it output exactly, but it has each one as a column name or
4:19
of our keys has a column name, so server one, three, four, five, and the values right below them
4:24
Maybe you want them in a more ordered list, you know, from top to bottom
4:30
One way to do that is you can take your hash table and run the Git enumerator method on it
4:37
pipe that over to selecting your objects that you want, so that's key and value, and then exporting
4:42
that out. So let's take a look at this into our Demo 2 file, and this is what I'm talking about
4:49
put them in more of a list or a table format where you have keys on one side and values on the other
4:56
So again, exporting hash tables initially you might just try to export it and not give you the format
5:01
or might output some additional information that you're not expecting. You can just run through the hash table, select your objects or the values that you want, and then export it out
5:11
Next, let's move on to PowerShell custom objects. As we saw in hash tables, there's pretty much two columns
5:18
You have your key and your value. That's the only thing you'll store about it
5:23
You have one value here, one value here. Custom objects have a lot more flexibility in that they represent an entity or an object
5:31
They can have a lot more properties and even potentially more methods than what hash tables can represent
5:37
And you would want to use a custom object if you want to create more structured data about something you're using in your script
5:44
So continuing on with a server example, here on line 63, I have My Custom Object
5:51
We still define the object using an at sign and curly brackets like you would in a hash table
5:56
but we add on the PS custom object keyword here. And we have a lot more things than what hash tables can store
6:04
We have a name, our service tag, a vendor, and a model describing our server object
6:09
So we'll go ahead and highlight this. F8 it. and then let's take a look at my custom object
6:18
And looking at the output here, we have the names, our properties, and our values for each one of them
6:26
Next, let's take a look at the object's members and methods using GitMember
6:30
One of the things we'll note is what we defined inside of our object show up as note properties on the object
6:37
First here we have the type name. We can see it's automation PS custom object
6:44
We have a couple of methods, get type, making it to a string
6:48
and then we have our different properties that we created for the object model name
6:52
service tag vendor, and then what they are. They're all strings and give you an example of what that would look like
6:58
If we want to, we can continue adding onto our custom object by adding new properties
7:03
Starting here on line 75, we're going to take my custom object, pipe it over to add member
7:08
We'll give the member a name and a value. And the member type is going to be no property
7:13
which matches what we just saw when we ran it through Git member
7:17
Let's go ahead and highlight that. Let's clear the screen and let's look at my custom object again
7:24
We now have owner added to it. And if we go back to Git member, we can see it is now an additional
7:32
no property on our object. Just as we can add properties, we can also remove them
7:38
We're going to look at the object variable here, access the PS object
7:44
properties and the dot remove method and just put the name of the note property we want to remove So we just added owner Let go ahead and take it back off And if we look at my custom object again we just back to our four properties here
7:59
The great thing about objects is we can output specific properties of that object
8:04
If we run just my custom object like we have up here right now, it gives us everything
8:09
But we can also pipe this to select object and pull out specific properties that we want to look at
8:14
in this case, just the name and the service tag. And we see that outputs exactly what we're looking for there
8:20
You can also access specific properties of your object using dot notation
8:25
So my custom object, name, or service tag, just to output those pieces there
8:30
So we just have Server 1 or the service tag for our server object
8:35
What we can also do is use custom objects in arrays. Here we have an array called dollar sign servers
8:44
We define our array using at and parentheses. And then we have our elements of our array
8:50
in each element is a custom object. We have different things in here
8:53
such as name, service tag, vendor, model, owner, and size of the server
8:58
And the different properties for each one, server one, two, and three
9:03
So let's go ahead and highlight this. Bring it into our session
9:08
And then from here, we can now perform filtering on the different elements inside our array
9:14
on our custom objects. So in this case, we have servers where object
9:19
the property size equals to you. If we run that, we only get back two responses here out of our array here
9:28
server one and server two. Or we can take a look at where the owner is Alice Jones
9:34
So we run that and we just get back the one server there. Another super nice thing about our custom objects is they have properties that we can perform
9:43
filtering on. Something we saw earlier is custom objects have built-in methods and you can also add in
9:50
your own methods to perform actions on your objects. So again, let's look at servers here, our servers
9:57
array and run get member. This is going to show us, we saw this earlier on our PS custom object
10:03
It has built-in methods for equals, get the hash code, get type, and get string. So you could run this
10:09
on one of your objects in your array, but running servers, and then get type
10:16
And it's just going to come back that it's a PowerShell custom object. But if we wanted to, we can define and create our own methods
10:24
to run and perform against our custom objects. So let's take a look at what that looks like
10:29
What you'll want to do first is create a script block variable
10:34
that has the code for your method. In this case, we're going to take that service tag
10:39
that's associated with our Dell server and convert it to an express service code
10:45
Here in the script block, you'll use the dollar sign this. This is referencing the object that you're running the method against
10:52
And here's some code here. I got this from a GitHub repository
10:56
I'll be sure to link that out in the description in case you're curious about this code a little bit more
11:01
But basically, it takes that service tag, does some different things to it
11:05
runs it through an array in 64 conversion or whatever, and then spits out an express service code
11:13
Let's go ahead and highlight this and create our block variable here
11:17
Now that we have our script block for our method, let's go ahead and we can use the add member command again
11:25
just like we used it to add properties to our object. We can add our methods to it
11:30
Then you want to notice here is on line 154. The name parameter is going to be the name of our method
11:36
that we'll see on there, and we're adding this to my custom object from earlier OK let go ahead and run add AddMember here And then let take a look at our Git member again against our custom object
11:51
You can now see right down here at the bottom, we have a script method called to Express Service Code
11:59
What we can now do with this is we'll run my custom object
12:04
To Express, we can sit here and tab through our methods, and we see to express
12:09
service code that's going to run and output our express service code based on this object's
12:16
service tag that it has associated with it. So if we were to run my custom object
12:20
Service tag, there's the service tag, and then that is the Express Service Code version of it
12:26
I think this is really cool where you can add your own methods to your objects to perform
12:30
conversions like this. Instead of doing it inside of a script or something, you could have that whole script block
12:35
to convert it or just add it as a method, and it's there for you to use
12:39
very simply like this. One thing I learned very recently is you can give your custom objects
12:47
their own PowerShell type name. Now, if we look back at my custom object right now
12:54
if we take a look at the type name here for my custom object, it's PSCustom object
12:58
We saw this earlier. But if you ever run this against other variables
13:02
that you're running from other PowerShell modules, you might see that this type name is different
13:07
It could be like string or an array. whatever else, but you can actually give your own type name here
13:13
So going back to our custom object definition here, we've added a new thing here called PSType Name is equal to Dell Server
13:22
because this is what this object represents. Let's go ahead and run this version of it
13:28
Go ahead and clear out the screen and we'll run GitMember again
13:33
And you can now see our type name is Dell Server. I recently learned this watching Jeff Hicks present at the Research Triangle PowerShell user group
13:42
I'll put a link to that talk down in the description. Jeff Hicks is awesome if you don't know him about PowerShell
13:48
and that talk is really good about how to improve your PowerShell code writing
13:54
He points out that giving your PowerShell type name like this is really good for writing your own custom output files
14:01
You might notice this if you're outputting something to the screen, it only gives you certain properties
14:05
but if you do a format listing against it, it'll give you more properties
14:10
That's where you can customize the output that your object is doing
14:14
and the first step in doing that is giving it its own type name. Finally, we're going to finish up
14:20
It's super easy to export your objects out to a CSV file because it will put them all in order for you automatically
14:26
because the export knows how to handle objects in the pipeline like this
14:31
So if we're going to output this to demo 3 and go and take a look at the file
14:37
we just have the one object in there right now, but we have our properties as each column
14:42
and then if we had more objects, it would have them down in there, as you would be more expected for a table
14:49
That is for this video, just comparing and contrasting hash tables with PowerShell custom objects
14:54
Hash tables are great if you just have a list of things where this equals this
14:59
and there's no more properties that you really, really need to associate it with it. However, custom objects are probably more the way to go
15:06
because you can add more properties to them, do better filtering, and even adding your own
15:11
script methods to them just to enhance their functionality. Anyway, thank you for watching. Hope you
15:16
learn something and we'll see you next time. Thank you