PowersHell: Factory Reset of ESXi
OK, well this example is a little obscure admittedly. During the course my travels in powersHell with VMware – I’ve begun to put together my own JACS (Just-Another-Configuration-Script). The idea is to take a clean-factory set ESXi build, and use purely powersHell to configure it. Of course you know when your writing scripts and TESTING them. You always need to reset your tests system against a new revision of the script.
Fortunately, ESXi makes this very easy to do by using the “Reset System Configuration” option
Prior to doing this – I have to drop the ESXi host into maintance mode, and remove it from the vCenter. Being the lazy git I am I thought I would investigate how to do this with powersHell. You see I only have 4-ESX hosts – and use them for all manner of VMware related projects. So in my lab environment I need to be able to wipe configurations to set them up for the rtfm-ed domain, the vi3book or vi4book.com domains – and then there’s the domain name of new SRM book which I start working after VMworld (corp.com). I should have been more smart about this configuration stuff – and just used something vanilla. Starting from the new SRM book everything is going to be corp.com from now on. In means I can work on different projects at the same time without worrying about the screen grabs having the wrong IDs. The other issue have with these ESX hosts – is often go from very simple vSwitch configurations, to very complicated ones. So the work I’ve been doing with powersHell and vSwitches – is going to come in very hand.
Anyway, below is the PS script I use to do a factory reset of my ESXi hosts. Once reset they get a client reservation from my DHCP server – and use this bit of PS at the beginning of my JACS before re-configuring them…
Connect-VIServer esx4.vi4book.com -username root Set-VMHostAccount -UserAccount root -password P@ssw0rd!
I’m quite proud of the script below in a very geeky sad way. Not because it is clever. But because it is the FIRST piece of powersHell I have written on my own, that address the SDK directly that works! I got close with the DPM powersHell script – and it would have worked if I hadn’t been such a dumb ass about password. The same goes with the bulk reset of root passwords. The reason I’m pleased about it – is after 2-3 weeks of slogging away at the SDK, I think I have done enough examples that I now UNDERSTAND the damn SDK. And if I’m looking to PS the script I won’t have to resort to googlewacking and cutting & pasting other peoples work together – for a such crap scripting guy like me – that’s a real achievement!!!
$vmhost = "esx4.vi4book.com" $vcname = "virtualcenter4.vi4book.com" #Connect to vCenter & Enter Maintenance Mode Connect-VIServer $vcname -username administrator -password vmware $esxhost = Get-VMHost $vmhost $hostview = $esxhost | Get-View Set-VMHost $esxhost -State maintenance # Remove ESX host from vCenter... Remove-VMHost $vmhost -Confirm:$false # Carry out factory reset... Connect-VIServer $vmhost -username root -password password $esxhost = Get-VMHost $vmhost $hostview = $esxhost | Get-View $ns = Get-View -Id $hostview.ConfigManager.firmwareSystem $ns.ResetFirmwareToFactoryDefaults()






RSS
iTunes
September 21st, 2009 at 2:22 pm
Nice work!
November 11th, 2009 at 12:35 pm
Hi Mike, I just wondered why you werent using the Set-VMHostFirmware -ResetToDefaults, doesnt this do the same ?
November 11th, 2009 at 1:21 pm
To tell you the truth – at the time I was experimenting with learning the SDK, and took this as an example. I was so into the SDK at the time, I didn’t even look for cmdlet. I’m not sure if it does the same thing. Ironically, Carter was joking how dangerous this could be, and I was surprise he didn’t tell me at the time there was a cmd-let. Next time I have chance I will give it a try. Thanks for drawing my attention to this…
October 19th, 2011 at 2:41 pm
Hi
if i Set-VMHostFirmware -ResetToDefaults,it says operation is not allowed in current state
what to do?