Created
September 1, 2017 23:40
-
-
Save mkouhei/308e580731dfa95d1809684263444584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $vmname = $one | |
| function Show-VM-Info { | |
| & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showvminfo $vmname | |
| } | |
| function VM-Powered-off? { | |
| Show-VM-Info | Select-String State: | Select-String -quiet 'powered off' | |
| } | |
| function List-VM { | |
| & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" list vms | |
| } | |
| function VM-Not-Exist? { | |
| return (List-VM | Select-String -quiet $vmname) | |
| } | |
| function Get-Timestamp { | |
| return (Get-Date -UFormat %Y%m%d-%H%M) | |
| } | |
| function Take-Snapshot { | |
| $timestamp = Get-Timestamp | |
| Start-Process "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" -ArgumentList "snapshot $vmname take $timestamp" -NoNewWindow -Wait | |
| } | |
| function List-Snapshot { | |
| & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" snapshot $vmname list | |
| } | |
| function Get-Oldest-Snapshot { | |
| return (-Split (List-Snapshot)[0])[1] | |
| } | |
| function Delete-Oldest-Snapshot { | |
| $oldest_name = Get-Oldest-Snapshot | |
| Start-Process "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" -ArgumentList "snapshot $vmname delete $oldest_name" -NoNewWindow -Wait | |
| } | |
| if (-Not (VM-Not-Exist?)) { | |
| echo "[error] '$vmname' is not exists." | |
| exit 1 | |
| } | |
| if (VM-Powered-off?) { | |
| Take-Snapshot | |
| echo "[done] take snapshot." | |
| if ((List-Snapshot | Measure-Object).Count -lt 4) { | |
| exit 0 | |
| } | |
| Delete-Oldest-Snapshot | |
| echo "[done] delete oldest snapshot." | |
| exit 0 | |
| } else { | |
| echo "VM: '$vmname' has been powered on." | |
| exit 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment