Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Captcha Click on image to update the captcha .

Add post

You must login to add post .

Login

Register Now

Lorem ipsum dolor sit amet, consectetur adipiscing elit.Morbi adipiscing gravdio, sit amet suscipit risus ultrices eu.Fusce viverra neque at purus laoreet consequa.Vivamus vulputate posuere nisl quis consequat.

VMware: Automatizar snapshots (Parte 2)

VMware: Automatizar snapshots (Parte 2)

Difunde No Solo Hacking!

En este artículo incluímos el script para que uséis en vuestros entornos, así como el vídeo explicándolo por si alguno necesita acalaración de cada una de las partes.

Código

# Carlos Melantuche
# https://www.NoSoloHacking,info
# Youtube https://www.youtube.com/c/nosolohacking?sub_confirmation=1


#Variables de tu entorno, especifica la IP/FQDN de tu servidor ESXi/vCenter y las credenciales
$vCenterIP = "IP o nombre Servidor"
$vCenterUser = "Usuario"
$vCenterPassword = "Pon tu password"
$VMList = @("VM1", "VM2", "VM3")
$SnapshotName = "Mispe"

#Función para crear Snapshots 
Function CrearVMSnapshot {
Connect-VIServer $vCenterIP -username $vCenterUser -password $vCenterPassword
    Foreach ($VM in $VMList) {
    Write-Host "Creando Snapshot para la máquina $VM"
    New-Snapshot -VM $VM -Memory -quiesce -Name $SnapshotName -RunAsync
    pause}
                             }
                                                        
#Función para revertir Snapshots             
Function RevertirUltimoVMSnapshot {
Connect-VIServer $vCenterIP -username $vCenterUser -password $vCenterPassword
    Foreach ($VM in $VMList) {
    Write-Host "Revertiendo Snapshot para la máquina $VM"
    $snap = Get-Snapshot -VM $VM | Sort-Object -Property Created -Descending | Select -First 1
    Set-VM -VM $vm -SnapShot $snap -Confirm:$false  -RunAsync | Out-Null
    pause
                             }                           
                                    } 

#Function RevertirVMSnapshotEspecifica {
# Connect to vCenter
#Connect-VIServer $vCenterFQDN -username $vCenterUser -password $vCenterPassword
#    Foreach ($VM in $VMList) {
#    Write-Host "Revertiendo Snapshot para la máquina $VM"
#    $snap = Get-Snapshot -VM $VM | Sort-Object -Property Created -Descending | Select -First 1
#    Set-VM -VM $vm -SnapShot $SnapshotName -Confirm:$false  -RunAsync | Out-Null
#    pause
#                             }                           
#                                    } 

#Consolidando todas las snapshots                                   
Function ConsolidarTodasVMSnapshot {

Connect-VIServer $vCenterIP -username $vCenterUser -password $vCenterPassword
    Foreach ($VM in $VMList) {
    Write-Host "Removing ALL Snapshot for $VM"
    $snap = Get-Snapshot -VM $VM | Sort-Object -Property Created -Descending | Select -First 1
    get-vm -Name $VM | get-snapshot | remove-snapshot -Confirm:$false  | Out-Null
    pause                                }                           
                                    }   
    
    
    Function UnaTecla 
{
    Write-Host -NoNewline -Object 'Press any key to return to the main menu...' -ForegroundColor Yellow
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
    Menu
}
                                     
Function Menu 
{
    Clear-Host        
    Do
    {
        Clear-Host                                                                       
        Write-Host -Object 'Elige una opción'
        Write-Host     -Object '**********************'
        Write-Host -Object 'Opcones a elegir' -ForegroundColor Red
        Write-Host     -Object '**********************'
        Write-Host -Object '1.  Tomar Snapshot de VMs '
        Write-Host -Object ''
        Write-Host -Object '2.  Revertir al último Snapshot '
        Write-Host -Object ''
 #       Write-Host -Object '3.  Revert a snapshot especifico'
  #      Write-Host -Object ''
        Write-Host -Object '3.  Borrar todos los Snapshots '
        Write-Host -Object ''
        Write-Host -Object '4.  Exit'
        Write-Host -Object $errout
        $Menu = Read-Host -Prompt '(1-4)'
 
        switch ($Menu) 
        {
           1 
            {
                CrearVMSnapshot            
                UnaTecla
            }
            2 
            {
                RevertirUltimoVMSnapshot
                UnaTecla
            }
            #3 
            #{
            #    RevertirVMSnapshotEspecifica
            #    UnaTecla
            #}
            3 
            {
                BorrarTodasVMSnapshot
                UnaTecla
            }
            4 
            {
                Exit
            }   
            default
            {
                $errout = 'Opción inválida, por facro selecciona de nuevo........Sólo del 0-4 Muñón'
            }
 
        }
    }
    until ($Menu -ne '')
}   
 
# Arrancar el menu
Menu                        
                                   

Vídeo


Difunde No Solo Hacking!

About Carlos Melantuche


Follow Me

error: ooops!