Show me what you got – Summarise directory size

Linux-esque ‘du –max-depth=1 -h’ in PowerShell

In a previous IT life I was performing junior linux admin duties where I found du -h --max-depth=1 to be very handy when tracking down whatever happened to be causing those pesky disk space alerts we all hate. In my more recent IT life, the tool of choice is TreeSize however not every client has a license or has the app installed on their various servers. I wanted something as fast as possible while still being light weight. This function took around 4 hours to ‘scan’ a network share which was over 4TB and around 20 million files, TreeSize beat this function by about 10 minutes.

A quick google shows a heap of people have come up with basically the same thing. How cool is that. Continue reading “Show me what you got – Summarise directory size”

Placeholder – Temporary file

Using [System.IO.Path]::GetTempFileName() to make a temporary file

Should you find yourself wanting to store a file quickly for processing later and you’re not sure on the users permissions or the environments storage status and options, why not whack the file in the user’s temporary folder ?

Instead of needing to build a path to use ($env:TEMP+"\blah.tmp"), we might as well use a built in method that just does it for us.

[System.IO.Path]::GetTempFileName() Continue reading “Placeholder – Temporary file”

I sentence you to change case! – Changing to sentence case

Using a Word Com Object to change text to sentence or other case.

PowerShell can use several methods to change the case of a string such as .ToLower() and .ToUpper(). You can also use installed culture to capitalise the first letter of every word, though this isn’t a great option as can be hard to read. In my travels on the internet, I’ve seen some people use .ToCharArray(), then changing [0] but this method still has problems with mixed case and multiple sentences (yeah, you can get around it, but this is about learning other methods today)

I’ve chosen word as I could not find an example of how to do this and in the past I’ve used it to spellcheck, and it was very easy to do so. In hindsight, maybe I should have stuck to spellcheck … I’ve been reading MSDN and TechNet and manually looking over the word com object for about 6 hours. Annoyingly the end result looks really simple.

So you don’t need to do the same… click more for the function. Continue reading “I sentence you to change case! – Changing to sentence case”

Error, does not compute – Input validation

Input validation via a function and regex

In past posts, some of the functions have included param / parameter to test or set various variables. This is a very good (and likely just a very correct) way of scripting, you should learn to do it. I should learn to do it more too but I’m too concerned about making user input correct AND easy for the user to deal with AND as flexible as possible. I do not want to be making multiple functions to make sure input data is correct.

So I made this simple function

function FNvalid_entry($msg,$regX){
	while (!$val_ent){
		$val_ent = read-host $msg
		if ([regex]::ismatch($val_ent,$regX)){
			break
		}else{
			write-host -fore yellow "Bad format"
			remove-variable val_ent
		}
	}
return $val_ent
}

Continue reading “Error, does not compute – Input validation”

Locked up tighter than Fort Knox – Test for file locks

A function to test for file locks quickly and easily.

On occasion you may have a need to update a central file from multiple locations or scripts. Most of the time it presents little issue however constant small writes may lead to multiple systems, scripts, workflows etc attempting to update a file at the same time, which could result in lost data. Some may shout out saying this is a design issue (I guess it is), though sometimes the scope of a script changes suddenly and no time or budget is given to adapt to the new situation.

This is where a small function to test if a file is in use can be very handy… Continue reading “Locked up tighter than Fort Knox – Test for file locks”

Handy links are handy

Just a post with some links which helped in my PowerShell journey.

Over the last few years, I’ve regularly been asked “How did you get into using PowerShell?” or “I want to learn PowerShell, how can I do it ?”. The simple answer is .. I don’t know.

You see, I never wanted to start using it.

Continue reading “Handy links are handy”

r4nD0M – Password generator

A password generator based on true random “seed” from quantum fluctuations of vacuum.

Password security is a big thing these days, especially with all the data breaches happening of late. Most posts on the subject just tell you to change your password regularly, don’t do this, don’t do that but I wanted to do something different. Something to help people make a password which is as close to true random as possible.

Continue reading “r4nD0M – Password generator”

Better, stronger, faster … arrays

Using System.Collections.ArrayList to make faster arrays

PowerShell variables are extremely versatile, especially when using them as an array even if it is just using $var=@() and += to add new elements. One thing you will soon notice however is additions become very slow as the array grows in size, this is due to the array being fixed size on creation and to overcome the size limit, += creates a new array as a copy of the old data plus the new data. This results in extra memory reads and slows the process as the array grows.

So, how do we get around this ? Well there are a couple of options

Continue reading “Better, stronger, faster … arrays”

You got to Zip it up – Compress-Archive and .Net

Compression of files or folders and quickly detecting the environments capabilities

If you happen to work for an MSP, you will quickly become use to needing to take into consideration a lot of variables such as

  • Who will be using the script
    • Non-technical Service Desk
    • Technical Service Desk
    • Desktop Engineers
    • or highly skilled users such s SOE/MOE or Server Engineers
  • What Operating System
  • What PowerShell version

Just because you have PowerShell, you might not be using the right version on all computers in the environment. I still have a 2003 server with PowerShell version 2 and a lots of my past customers were running 2008r2.

You may find yourself wanting to reduce the size of a file or folder, especially if you plan on pulling rather large log files from a Domain Controller in place of using a normal backup solution.

PowerShell 5.0 includes compress-archive, which is very easy to use.

Compress-Archive -Path .\source -DestinationPath .\destination.zip 

Wonderful, but remember I mentioned older operating systems and PowerShell versions. Yep, well, PowerShell v4 does not have this command however…

Continue reading “You got to Zip it up – Compress-Archive and .Net”

A hidden gem – SNMP

SNMP tools hidden in printers! using olePrn.OleSNMP to confirm provided SNMP information is correct

Something I had found odd for sometime was a lack of SNMP polling within PowerShell. The tool is so powerful so why can’t we query pesky switches, printer and other IT equipment for small, fast and useful data. Sure we could use one of many available CLI or GUI tools, but plain text/string manipulation is a pain! Well it turns out its hidden in an obscure place, within olePrn.OleSNMP ComObject. I don’t recall where I came across this information originally, but thank you GoogleFu for finding it.

The only information we need to use to make this work: A hostname or IP, read community string and an OID we wish to query.

$SNMP = New-Object -ComObject olePrn.OleSNMP
$snmp.open(‘host’,’community string’,attempts#,timeout(ms))
$snmp.get(‘an oid’)

$SNMP = New-Object -ComObject olePrn.OleSNMP
$snmp.open('192.168.23.201','PowerShellIsCool',2,1000)
$snmp.get('.1.3.6.1.2.1.1.1.0')

Continue reading “A hidden gem – SNMP”