February 25, 2015

I Ho, I Ho, Powershell workflows....

Does it make you grumpy when you don’t have a network monitor system in place on your network and your not allowed one, yet you are expected to be able answer the following types of  questions instantly?

Are all the servers up and working or is XYZ  up and running? Can you see XYZ as I can’t ping it?

The first port of call most of us will do is to quickly ping the effected box(s) yourself and see – What if you wanted to get a very quick overall
picture of all machine ups on the domain and responding to pings? You guessed it – we can use Powershell to great effect!

why not get Powershell to look up all your computer in your Active Directory, they even provide an cmdlet to help you do that Get-ADComputer and ping each computer object that it finds and report back what is up and
responding to a ping.

Well we know we can enumerate AD – we can use handy Test-Connection, the Powershell ping cmdlet – so we are on to a winner right…. ummm hang on while I ping my whole AD of 100’s of servers and computers one at a time…. SLOW…. plenty of time a cup of coffee, maybe nip down the shop to get lunch and with any luck your end user who is waiting for your response will have died of old age or some other BOFH related accident before you have completed your ping of the whole domain!Oh there server was up…sigh!

Here we can use a great feature of Powershell which came in with version 3 – workflow and -parallel flag to speed up the execution of our Test-Connection cmdlet by running lots of them in parallel 🙂 – as this is a basic script we are not going to do error handling – so I am using the -ErrorAction SilentlyContinue flag. So this workflow will only return machines that are up! Hence I named it, are-alive.

So here is the code to get all your computers in AD and then ping each of them and if it responds print the output!

workflow are-alive {
$computers = Get-ADComputer -Filter * | Select -ExpandProperty DNSHostName
foreach -parallel ($computer in $computers) {
Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue
}
}

So just load a Powershell window, load in the work flow using what ever method you like! And say are-alive and you screen with luck will spit out a list of machine that are up and responding to ping!

This initial script isn’t very tidy but it shows you the concept – Grumpy and Lazy Admin here ain’t going to do all the work for you! Write your own… or just steal it. The quality of this script makes me grumpy but MEH!

I really really just wanted to point out the workflow concept and the -parallel flag that comes with it. There are others, so it is worth a Google! – which is great when you start thinking what could I do with all this??? So when you want to do many things at a time to speed things up like ( pinging a whole network!) think of workflows because waiting for stuff to happen just makes me grumpy!

So to take away, I only wrote this badly written code to show you the concept of workflow – you can tell I wrote it as it is bonk! But it does work and could be made better with great features like colour and layouts etc and don’t forget you can use | Sort or FT, Out-CSV etc – this is just a basic “hey you can do this” and “look at workflows which I think are neat”. If you come up with a way of improving or making this better – share it in the comments.

Hazzy