Recently I worked on a PowerShell script to extend hardware inventory. During this process, I needed a named array with three elements. I will admit that I have never taken a programming class. However, I know the fundamentals of object-orientation programming and have become very good at PowerShell. However, hunting on the Internet is my go-to reference when it comes to some fundamentals. As we all know, the key to finding the correct answer on the Internet is finding the right keywords. First, I tried “3-dimensional array, hash tables, PowerShell arrays,” but that did not work. Finally, I found an excellent blog by Tommy Maynard, “There is a Difference: Arrays Versus Hash Tables.” The blog explains the differences between arrays and hash tables. I have used hash tables in the past, and I admit the “GetEnumerator()” function has confused me.
While this information was helpful, I wanted an object with three properties: UserID, LogonDate, and LogoffDate. Redefining the problem from an array with three elements to an object with three properties led me to the solution. What I needed to create was a new custom object. Yes, I know this is obvious now, but last night it was not, hence the need for this blog to remind me of the answer so I can refer to it in the future.
The command is “New-Object System.Collections.ArrayList”. Below is the code snippet.
#Create an Array with properties (Column Headers)
#Define an array list object.
$LogonList = New-Object System.Collections.ArrayList
#Create a function to add the members to the array
function Add-ArrayListMember ($UserName, $LastLogon, $LastLogoff, $ListName)
{
$item=New-Object System.Object
$item | Add-Member -MemberType NoteProperty -Name "UserID" -Value $UserName
$item | Add-Member -MemberType NoteProperty -Name "LogOnDate" -Value $LastLogon
$item | Add-Member -MemberType NoteProperty -Name "LogOffDate" -Value $LastLogoff
$ListName.Add($item) | Out-Null
}
#call the function to add members to the array
Add-ArrayListMember -UserName User1 -LastLogon "10/11/2021" -LastLogoff "12/10/2021" -ListName $LogonList
Add-ArrayListMember -UserName User2 -LastLogon "1/12/2022" -LastLogoff "1/30/2021" -ListName $LogonList
Add-ArrayListMember -UserName User3 -LastLogon "4/14/2022" -LastLogoff "5/1/2022" -ListName $LogonList
#Perform a Get-Member command to view the properties of the new object.
PS C:\Users\dawn> $LogonList |gm
TypeName: System.Object
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
LogOffDate NoteProperty string LogOffDate=12/10/2021
LogOnDate NoteProperty string LogOnDate=10/11/2021
UserID NoteProperty string UserID=User1
# the last three members of the object are the properties added in the function
PS C:\Users\dawn> $LogonList
UserID LogOnDate LogOffDate
------ --------- ----------
User1 10/11/2021 12/10/2021
User2 1/12/2022 1/30/2021
User3 4/14/2022 5/1/2022
As denoted in the Get-Member command, LogonList is a System Object with LogOffDate, LogOndate, and userID as members. Now that I had an object with three members, I created a WMI class and populated it with each instance of user logon.
Leave a Reply