Hi
I've been writing a module that queries a table in SQL, it's not large, only a few hundred rows - but I don't want this to be a live view; a daily snapshot is fine in that the first time I run it per day, it refreshes the in-memory copy and then that's a persistent store. All my further commands, Get-ntserver etc run on the memory copy, the $script:ntservers variable.
What I found however, was that building that object takes up to 20 seconds for about 700 rows. Not a huge problem as I'm running it once per day. I had a look around for the "right" way to do this - the reason I want the data as proper objects is so I can pipeline them to other functions in the module
eg get-ntserver | where {$_.Role -eq 'CITRIX'} | get-serveruptime
get-ntserver just runs on the $script:ntservers variable, there's no further SQL lookup at this time beyond the daily refresh.
Currently, the object is being built with something like:
}
So basically, it's the old "loop round all the columns and rows" routine. That doesn't seem ideal...
For speed, I was quicker building an initial row object template, and then copying that. That works great - but I can't help but think there's a better way to this. I don't want to build the "where" clause into SQL - plan on still letting Powershell do the Where clause per my above example. I'm kinda intrigued about a best-practice for this - any suggestions? Also, it's dynamic, as the number of columns in the database will change from time to time, hence "select *".
I guess what I'm really looking for amounts to
$PSObject = SQLResultsToObject("select * from server")
etc etc
Cheers