OK, so every environment has â€œThat Guyâ€â€¦ if you donâ€™t know what Iâ€™m on about, take a look at what floated past on Twitter this evening from @vSeanClark:
RT @techmute: Some people really make me wish vSphere had the option "take a snapshot when this guy logs in as an admin" <- startup idea?
Wellâ€¦ if anyone has some venture capital or Angel fundsâ€¦ Iâ€™m your guy. Why? Well with vSphere Alarms and PowerCLI, one can indeed snapshot a VM when a specific user pulls a remote console, hereâ€™s how:
Setup the Alarm:
With the following actions:
- Send a notification email
- Run a command: C:\WINDOWS\system32\cmd.exeâ€œ â€œ/c echo .|powershell.exe -nologo -noprofile -noninteractive C:\Scripts\OMG-ThatGuy.ps1â€
* A few notes here: The â€œRun a commandâ€ is all one really long lineâ€¦
Next the script:
If you copy/paste it from here youâ€™ll want to make sure that everything after Get-VM is on a single line.
# Add the snapin:
#Take the snapshot!
Connect-VIServer -Server localhost -User something -Password something
$vmId = "VirtualMachine-" + $env:VMWARE_ALARM_TARGET_ID
Get-VM -Id $vmId | New-Snapshot -Name ((Get-Date).ToShortDateString())
The first bit adds the PowerCLI snapin, the second gets the VM that triggered the alarm and creates a snapshot with todayâ€™s date. Save this in c:\scripts, or another location of your choosing, and weâ€™re ready to go!
So Iâ€™m not really sure where this script would be all that practical in real use. It is however an example of the power of both PowerCLI and vSphere. Hereâ€™s a screenshot of this working, btw:
What good would this post be if I didnâ€™t link back to all the random places I found the info on how to do this on:
- The PS Snapin & Command Line way to launch a script:
- VMwareâ€™s PowerCLI and Alarms (Pay attention to the environment variables):
- Accessing said environment variables:
- Manipulating the Get-Date cmdlet:
- My old post on alarms:
- The actual bit that got this working: