Those great guys down in the South West of England, @mpoore, @jeremybowman, @virtualisedreal and @simoneady have kindly invited me down to their next VMUG to present about automation. So I will be talking about some of my experiences in automation projects from the last few years and particularly how to write your own code in a generic way so that it is portable across different projects and systems.
It looks like there is plenty of other good content lined up that day so I’d suggest you get down there too.
Sometimes I’m asked how I manage my own (PowerShell) code, in terms of version control, backups, portability etc. In this presentation I demonstrated how my PowerShell code is typically broken down into functions and then placed into modules. This allows me to make very generic code for granular tasks, typically either to plug a gap missing from the out-of-the-box cmdlets or maybe stringing a few of them together. As a consultant this enables me to build up a toolkit of functions for particular scenarios gained over various different experiences and use them in a modular fashion where needed for each particular project.
Creating your own aliases in PowerShell is pretty straightforward. The New-Alias cmdlet allows you to create handy shortcuts for existing cmdlets or functions you have created yourself.
I write a lot of functions for my own modules, so having shortcuts for some of these functions would be pretty useful when using them via a module. However, when I first added some to one of my modules they weren’t available for use.
Saw this tweet yesterday about an equivalent of tail -f in PowerShell
(I think Simon meant Get-Content)
In PowerShell v3 the Get-Content cmdlet received an additional parameter -Tail. This means rather than pulling back the whole of a text or log file, you can just get the last x lines, e.g.
Get-Content .\\vmkernel.log -Tail 10 Nice, but how can we make this more friendly to those used to typing tail -f?
There are a number of different ways to test for the presence of a registry key and value in PowerShell. Here’s how I like to go about it. We’ll use an example key HKLM:\SOFTWARE\TestSoftware with a single value Version:
Check for the key
You can use the Test-Path cmdlet to check for the key, but not for specific values within a key. For example
Test-Path 'HKLM:\\SOFTWARE\\TestSoftware' but not
Test-Path 'HKLM:\\SOFTWARE\\TestSoftware\\Version' So for the value we need to work a bit harder.
Quite a common request for working with Windows machines is to report the software installed on them. If you don’t have a centralised system for reporting on client software (many places don’t) then you may turn to some form of scripted method to obtain this information.
Most people tend to head to Add / Remove Programs when thinking about what software is installed in Windows. However, not all applications will always populate information in there, depending on how they have been installed.
Adding and removing Items from a PowerShell array is a topic which can lead to some confusion, so here are a few tips for you.
Create an array and we will note the type System.Array:
$Fruits = "Apple","Pear","Banana","Orange" $Fruits.GetType() However, if we try to Add or Remove items to the array we get errors that the “Collection was of a fixed size”
$Fruits.Add("Kiwi") $Fruits.Remove("Apple") $Fruits.IsFixedSize We can see that the array we originally created is of a fixed size.
Sometimes when running a PowerShell script you may need to test at the beginning whether the process it was called from had Windows admin privileges in order to be able to achieve what it needs to do. Prior to PowerShell v4 I had used something along the lines of the following to test for this condition - not the most obvious piece of code ever to be fair:
function Test-IsAdmin { (\[Security.
If you’re looking to learn or improve on existing skills as part of a new year goal and one of those in PowerShell, then you may find it useful to check out the Winter Scripting Games 2014. When you are looking to improve your scripting skills it can sometimes be tricky if you don’t have a practical problem to solve. By taking part in these games you will have a number of opportunities to apply your skills to real problems.
At today’s #UKVMUG I presented on the topic: PowerCLI In The Enterprise: Breaking The Magician’s Code. Below are the slides from the session:
During the session I discussed breaking your PowerShell code down into functions and modules. To aid with this I am posting the 6 function templates I use which cover many of the typical scenarios I write a function for and enable the rapid creation of these functions since much of the code is typically repeated.