PowerCLI: A Simple VM Backup Script

Just lately I've been doing a lot of work in a Lab environment. Some of the work I'm doing is quite important to me so I decided to workout a way I can backup my VM's onto a backup device. To my surprise this was pretty simple to do.

Here is how I decided the script should function:

  1. Send myself an email telling me that the backup process has started
  2. Import the name of the VM to be backed up and the destination datastore from a CSV file
  3. Create a Snapshot of the VM I want to backup
  4. Create a Clone'd VM from the Snapshot
    • Place the backup onto my Backup Datastore
    • Name the backup <master VM name>-<date stamp>
    • Thin provision the backup
  5. Remove the Snapshot from the master VM
  6. Send myself an email telling me that the backup process has completed

So that's what I wanted to do, this is what I ended up with.

Read the rest of this entry »

PowerCLI: Reconfiguring NTP Servers on ESX Hosts

Just lately I’ve been creating a lot of PowerCLI scripts to help configure various aspects of out ESX environment. We’ve just implemented a new NTP Server to our network. So I was given the job to update all of our ESX Hosts. I didn’t fancy spending all morning manually changing them, so I set to work in creating a PowerCLI script to do the business. Here is the result.

$Cluster = "<cluster name>"
$Hosts = Get-Cluster $Cluster | Get-VMHost
ForEach ($Host in $Hosts)
{
Remove-VmHostNtpServer -NtpServer "<old ntp server>" -VMHost $Host | Out-Null
Add-VmHostNtpServer -NtpServer "<new ntp server>" -VMHost $Host | Out-Null
Get-VmHostService -VMHost $Host | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | Out-Null
write "NTP Server was changed on $Host"
}

Adding Virtual PortGroups to all ESX Hosts in a Cluster

Connect-VIServer -Server <vCenter Server>
$AllHosts = Get-Cluster <Cluster Name>
foreach ($ESXHost in ($AllHosts | Get-VMHost))
{
 Get-VMHost $ESXHost | Get-VirtualSwitch -Name <vSwitch Number> | New-VirtualPortGroup -Name
<Virtual PortGroup Name> -VLanId <VLan ID>
}
Disconnect-VIServer -Confirm:$false

Adding a Virtual PortGroup to all Hosts

Connect-VIServer -Server <vCenter Server>
Get-VMHost | Get-VirtualSwitch -Name <vSwitch Number> | New-VirtualPortGroup -Name <Virtual PortGroup
 Name> -VLanId <VLan ID>
Disconnect-VIServer -Confirm:$false

Saving Event Logs Using PoSH – My First Ever Script!

Finally after months and months of thinking about it,  I have finally been able to play with Powershell and make it do something useful.

My task was to save the Event Logs Errors off of all of our production servers on a daily basis. I was handed a piece of paper with a list of servers and told to get on with it. I could have easily done it manually, but I would have had to have changed my job title to “Event Log Saver Guy” as that would be all I would have time to do.

Read the rest of this entry »

PowerShell Scripting, its about time i learnt

If i had a £1 for every time i heard/read about how good PowerShell is i probably wouldn’t have to write this post myself!

So I’ve decided to bite the bullet and I’m going to learn. I’m lucky enough to regularly chat to Alan Renouf from the Virtual-Al Blog, and he’s agreed to help me with my journey from a complete Novice to a “PowerShell Guru” like himself.

Update: Alan has just pointed me to this excerlent VI-Toolkit Labs document which has been release from VMWorld Europe 2009.

Personally I think the best way to learn PowerShell is to start off by being given a set of basic tasks that needs to be done and then working out how to use PowerShell to run this task for me.

This part of my Blog will be mostly for my own benefit, but I’m pretty sure that the scripts i come up with could come in handy to other users. The posts i make on PowerShell will not appear on this blogs front page or in my RSS feeds but they will be Tweeted on Twitter, so if you don’t follow me already and your interested in what I’m writing add me now.

Update: Tom Howarth from PlanetVM has just told me that he too has decided to learn PowerShell, hes also using the VI-Toolkit Labs document from VMWorld Europe 2009.

PowerShell Scripting Resources

Here is a list of PowerShell Scripting Resources that i’m using. I will continue update it:

Get Adobe Flash playerPlugin by wpburn.com wordpress themes