Skip to content

Instantly share code, notes, and snippets.

@MaaxGr
Last active March 31, 2024 11:47
Show Gist options
  • Save MaaxGr/b04e63e4bdd2a021818f4f9f67be26e5 to your computer and use it in GitHub Desktop.
Save MaaxGr/b04e63e4bdd2a021818f4f9f67be26e5 to your computer and use it in GitHub Desktop.
Write-Host ""
Write-Host "Connecting to vsphere..."
Connect-VIServer -Server "$Env:VSPHERE_HOST" -User "$Env:VSPHERE_USER" -Password "$Env:VSPHERE_PW"
Write-Host ""
$run = $true
while ($run) {
Write-Host "Actions:"
Write-Host "(l) List Snapshots"
Write-Host "(n) New Snapshot"
Write-Host "(d) Delete Snapshot"
Write-Host "(e) Exit program"
Write-Host ""
$userInput = Read-Host "Which action should be performed? (l, n, d, e)"
switch ($userInput) {
"l" {
$userInput = "all"
$userInput = if ($_ = Read-Host "Snapshots of which host should be printed? [$userInput]") {$_} else {$userInput}
if ($userInput -eq "all") {
Get-VM | Get-Snapshot | select VM, Name, Created | Format-Table
} else {
$vm = Get-VM -Name "$userInput"
Get-Snapshot -VM $vm | Select-Object VM, Name, Created | Format-Table
}
}
"n" {
$date = Get-Date -Format "dd.MM.yyyy HH:mm"
$vm = Read-Host "On what host should the snapshot be created?"
$name = "Manual snapshot of date $date"
$name = if ($_ = Read-Host "What should be the name of the snapshot? [$name]") {$_} else {$name}
New-Snapshot -VM $vm -Name "$name" -Memory:$true
}
"d" {
$vm = Read-Host "From which host should the snapshot be deleted?"
$data = Get-VM -Name "$vm" | Get-Snapshot | select VM, Name, Created
$data | Format-Table
$lastItem = $data[$data.Count - 1]
$name = $lastItem.Name
$name = if ($_ = Read-Host "Enter the name of the snapshot that should be deleted. [$name]") {$_} else {$name}
$snapshot = Get-VM -Name "$vm" | Get-Snapshot -Name "$name"
Remove-Snapshot -Snapshot $snapshot -confirm:$false
}
"e" {
Write-Host "Program exists now..."
$run = $false
}
Default {
Write-Host "Invalid operation."
}
}
Write-Host ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment