Tuesday, March 31, 2009

Update Notes field for all VM in your Organisation

The Systems Administration team had been assigned the task of updating all VM guest machines with a description. This would allow us to have an idea of who owned and what a server did.

I thought this should easy enough to achieve a powershell script but I then released I could do this in 2 lines.

$vminfo = import=csv c:\powershell\vminfo.csv
$vminfo |ForEach-Object {set-vm $_.vmserver -Description $_.vmdescription -confirm:$false}

First you'll need CSV of the VM servernames and descriptions
example of vminfo.CSV

Vmserver,VMDescription
server1,Server owned by:Mr T VMdescription goes here
server2,Server owned by:Mr T VMdescription goes here

$vminfo = import=csv c:\powershell\vminfo.csv

In the first line of the command, the Import-CSV cmdlet is used to retrieve the saved object
representation from thevminfo.csv file, create corresponding objects and store them in the $vminfo variable.

$vminfo |ForEach-Object {set-vm $_.vmserver -Description $_.vmdscription -confirm:$false}

The secondline use a Foreach loop to set the description of the server. $vminfo is now object you can play with.

$_.Vmserver and $_.VMDescription values come from the header line of the CSV file.

Foreach-object ( i.e each line in vminfo.csv) do the set-vm cmdlet. -confirm:$file supresses confirmation.

/ Steve D

Tuesday, March 24, 2009

Blog dedication "But ya f#$% one goat..."

Hi All,

Here's my first blog ever!

This afternoon I was writing some powershell code and I was one keystroke away from destroying most of my virtual enivornment and I thought I'd start technology blog that was dedicated to techos who have been dogged by there mistakes.

If you don't know what "But ya f#$% one goat..." reference means here's the Joke below.

A Scottish old timer in Scotland, in a bar, talking to a young man. The Old Man says, "Lad, look out there to the field. Do ya see that fence? Look how well it's built. I built that fence stone by stone with me own two hands. I piled it for months.""But do they call me McGreggor-the-Fence-Builder? Nooo..."Then the old man gestured at the bar.

"Look here at the bar. Do ya see how smooth and just it is? I planed that surface down by me own achin' back. I carved that wood with me own hard labour, for eight days.""But do they call me McGreggor-the-Bar-builder? Nooo...

"Then the old man points out the window. "Eh, Laddy, look out to sea...Do ya see that pier that stretches out as far as the eye can see? I built that pier with the sweat off me back. I nailed it board by board.""But do they call me McGreggor-the-Pier-Builder? Nooo...

"Then the old man looks around nervously, trying to make sure no one is paying attention."But ya fuck one goat..."

For those who'd like to delete all of there virtual machines here's a great one liner for you. you'll need to install VI tookit before you can do any of this. To get it go here http://vmware.com/go/powershell.

No warranty blah blah blah

get-vm *remove-vm -deletefromdisk -confirm:$false

Essentialy you use the get-vm command to list all your virtual machines you then pipe it to remove-vm to the remove-vm. Then -confirm:$false supresses confirmation. $false is pre defined variable in powershell user get-var to check out the rest.

Remember everyone makes mistakes especially people who are lazy and copy paste code with out releasing there impact.

Cheers,

SteveD

Hi Steve