OK, well this example is a little obscure admittedly. During the course of 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 you're 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 maintenance mode, and remove it from the vCenter. Being the lazy guy 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 manners of VMware related projects. So in my lab environment I need to be able to wipe configurations to set them up. The other issue I have with these ESX hosts – is they 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 handy.
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 addresses the SDK directly AND works! I got close with the DPM powersHell script – and it would have worked if I hadn’t been so dumb 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 such a 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()