Monday 15 June 2015

Manipulating object tables in Powershell.

Last week I posted about creating object tables in Powershell. This was mainly for the purpose of being able to use Export-CSV to send the data out to a CSV file. This time I am going to have a brief look at some of the other things you can do with an object table, which is a convenient way of having a simple database that you can store data in and process it through your script.

Since the object table is basically an array of objects, it can be manipulated using the basic array operators. Such as:
  • Append an instance: += which most people will recognise as the standard increment operator borrowed from C/C++.
  • Retrieve an instance by index: e.g. $S[0] returns the first instance
  • Retrieve a range of instances by index e.g. $S[4..9], $S[1,2+4..9]
  • Retrieve the last instance: $S[-1] which is great as you don't need to know how big the array is to use this.
Obviously we don't need to use typical database methods like First, Last, Next and Previous which are often provided to step through a table's records. Instead just index the array.

Friday 12 June 2015

Three Jaycar Torches


I have found torches purchased from Jaycar to be the best quality and useful LED small torches that I have owned lately. On the right you have a small pocket torch that has a slide-zoom head and runs on 3xAAA batteries (similar to ST3483). In the middle a more powerful equivalent with a twist zoom and the same level of battery power (ST3475).

The head torch (ST3279) is my most recent purchase and runs on the same amount of battery power. It has a slide zoom head and is waterproof. This replaces an older Jaycar head torch which had a separate battery pack mounted on the back of the head on the strap, the problem with this one was that the connecting wire broke off after several years of faithful service. There is a problem with the new headtorch and that is the red translucent band that is incorporated into the head. The LED shines through that which creates a prominent glare issue when wearing the torch on the head. So I will have to get some black paint to put over the band to block the light from going through it.

Thursday 11 June 2015

Creating object tables with Powershell

One of the useful things you get with Powershell is lots of functions that create data tables in memory that you can cycle through using Foreach to get an instance (equivalent to a record or row in a regular database) and then interrogate its properties (equivalent to columns). The Foreach loop automatically steps through all the object instances in the table.

Having done a number of function calls (for example Import-CSV to get a list of users from a CSV file, or Get-ADUser with a search spec to retrieve a bunch of user accounts) I thought it was about time I worked out how to create an object table myself, so that I could populate it with some user account data and then write it all out to a CSV file, using Export-CSV in this case. 

Well it turned out to be exceptionally simple to do this. Basically, object tables are just arrays of objects. Here is some code to illustrate this.

$UserList = @()
foreach ($Class in $Classes)
{   
$ADStudents = Get-ADUser -Filter  {Division -eq $Class} -SearchBase  "ou=Students,ou=School,dc=our,dc=school" -SearchScope Subtree -Properties ipPhone   
if ($ADStudents -ne $null)   
         foreach ($S in $ADStudents)
        {           
$username = $S.samAccountName           
$pwd = $S.ipPhone           
if ($pwd -eq $null)           
{               
    $pwd = ""           
}           
$UserList += New-Object psobject -Property @{Class = $($Class);Username=$($Username);Password=$($pwd)
}        
}   
}
}
So in this script, which loops through a set of classes ($Classes is an array of names of classes) the first line is to create the table itself, with no rows and no columns just yet. This is the simple $UserList = @() call which is actually creating an array.

Then the first Foreach loop is to go through a predefined array of class names for the school, and it gets the name of the current class into $Class. Then it gets all of the student accounts which have that class specified in their Division property. Note that it also retrieves the IpPhone property, which is used to store the passwords that we want to have a list of.

So the next step is to find out if we retrieved any results, and if so, another foreach loop has the task of getting each instance in turn (because the results of the Active Directory call are returned in an object table themselves) and then we add a row or record to $UserList. Firstly we create an object instance in which to store the data, and then we specify that the properties of that instance are Class, Username and Password, and the values they contain. 

It's useful to note here there is no fixed structure for each row, because each row only needs to be an object, and each row could easily refer to a different object structure, with different property names. This is a key difference from a regular database which predefines column names and types so they are consistent throughout the dataset. Obviously I am keeping things simple here and making every object instance identical.

So that is how we create our table, and the final line of the script (not shown) is a simple call to Export-CSV to write out the table to disk.