I recently came over an interesting thread on LinkedIn where they talked about how to count lines in a CSV. A lot of good code examples where posted, where they tried to optimize for speed and memory consumption. I decided to try out some ideas of my own, but as I often do, I got sidetracked into building a function to easily perform measurements of code blocks instead.
(more…)
Category: PowerShell
Compare-DataTable
Have you ever been in a situation where you needed to compare two DataTables? No? Well, if you ever do, I have your back! This function will take two DataTables and compare them, either only schema or a full compare.
(more…)
Get-FolderSize
This is a little function I wrote one day when I needed to get the total size of a set of folders (including sub folders). It performs a recursive search and computes the total count and also shows the size in a more human-readable format. It also have a “hidden” property called SizeInBytes that can be helpful if you want to do size calculations and need the size in a common format.
(more…)
New-DataTable
I have written previously about how to convert an object to a DataTable and how to create one based on an existing MSSQL table, but now I will share a function to create a new empty DataTable. Sure, this isn’t very complicated to do manually, but using this function you can define the table structure using an ordered dictionary, which makes it much easier to update later if needed.
(more…)
Get-VMWareSnapshot
Ok, I understand this one is done “to death” already, but anyway, here is my take on it. I had a need for something a bit more optimized than the Get-Snapshot command, so I had to use Get-View. I also needed the possibility of either getting all snapshots, or filter on VM name.
(more…)
Reordering columns in a DataTable
If you need the columns in a DataTable in a specific order, you just create them in the order you need, right? But what if you are dealing with a DataTable full of data?
(more…)
Optimizing WMI queries using forward only enumerators
I was writing a script that runs queries to about 2000 servers to check the patch compliance of each server, using WMI to query the SCCM agent on each machine. The servers are located all around the world, with varying degree of latency. Even though I utilize runspaces to speed things up, I still noticed that some of the threads timed out on me. Investigating I found out that it was the WMI query itself that took a long time executing, particularly on high-latency sites.
(more…)
Get VMWare resource status
The following function will query one or more vSphere servers for overall status, configuration status as well as any configuration issues. It can also filter on Name, if you just want to get a quick status of one or more named resources.
(more…)
ConvertTo-ScriptBlock
Ok, just a small one today. You won’t save any typing with this, as the actual code is just one line, and not much longer than the function name. But I think it looks nicer, and it’s easier to remember. Besides, it looks more like “proper” PowerShell.
(more…)
Extending Test-Connection
Using the Test-Connection in an enterprise environment, I often miss the -a parameter of the old ping command, that tells ping to try to resolve IP address to HostName.
So I sat down and created a proxy function for Test-Connection, adding a Resolve parameter. It’s quite simple actually, it’s checks the ComputerName parameter, and if the data is a valid IP address tries to resolve using the Net.Dns class. It then takes the resolved name and injects it back into the ComputerName parameter before calling the original Test-Connection command.
(more…)