Friday, June 20, 2008

Hyper-V Virtual Machine backup PowerShell Windows 2008 script

Estos días me estoy peleando con las máquinas virtuales que trae Windows 2008, el llamado Hyper-V. Este sistema de virtualización funciona muy bastante bien, yo tengo un Exchange 2003 con unos 60 usuarios y un servidor IIS de producción corriendo en maquinas virtuales y los mueve bastante bien. Vamos que no se nota que son máquinas virtuales.Y ya que son virtuales les he hecho un script con la PowerShell para tener copias por lo que pudiera pasar.
Os lo dejo bajo vuestra responsabilidad para que lo uséis y si os peta me decís donde para corregirlo.

# Para ejecutar el script hay que poner toda la ruta aunque estemos en el directorio del ps1, así "e:\backups\VMbackup"
$SERVER_HOST = "TU_SERVIDOR"

$ListofVMs = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter "ElementName <> Name"

foreach ($VM in $ListofVMs)
{
#Verificar estado actual
If ($VM.EnabledState -ne 2)
{
#write-host "Solo se hacen copias de la maquinas que esten en marcha"
exit
}
If ($VM.EnabledState -eq 2)
{
# ************************
# Parar la maquina virtual
# ************************

#Get the Shutdown Component for the VM
$query = "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" + $VM.name + "'"
$Shutdown = get-wmiobject -query $query -namespace "root\virtualization" -computername "."

#Request a forced shutdown
$Result = $Shutdown.InitiateShutdown($true,"Backup periodico")

$fistro = "ElementName = '" + $VM.ElementName + "'"

while ($VM.EnabledState -ne 3)
{
sleep(1)
$VM = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter $fistro
}


# ***************************
# Exportar la maquina virtual
# ***************************

$VM_Service = get-wmiobject -computer $SERVER_HOST –namespace root\virtualization –class Msvm_VirtualSystemManagementService

$fecha = get-date
$dirMaquina = "E:\Backups\MaquinasVirtuales\" + "Backup_" + $fecha.year + "_" + $fecha.month + "_" + $fecha.day

$status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, $dirMaquina)

If ($status.ReturnValue -eq 4096)
{
# Get the WMI instance of the object
$Job = [WMI]$Status.Job
$JobStatus = $Job.JobState

while ($JobStatus -ne 7)
{
sleep(1)

# Get the WMI instance of the object
$Job = [WMI]$Status.Job
$JobStatus = $Job.JobState
}
}


# ***************************
# Arrancar la maquina virtual
# ***************************

$VM = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter $fistro
If ($VM.EnabledState -eq 3)
{
#Arranca la maquina
$VM.RequestStateChange(2)

while ($VM.EnabledState -ne 2)
{
sleep(1)
$VM = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter $fistro
}
}
}
}


Artículos relacionados:
http://circulotito.blogspot.com/2008/05/event-111-exoledb-microsoft-exchange.html
http://circulotito.blogspot.com/2008/06/bateria-sai-apc-hiberna-windows-2003.html

2 comments:

Cavallin Jorge said...

Hola , de Powershell , no se nada paro en bici algo me animo .. www.pollo-team.com.ar jecavallin@hotmail.com

Circulotito said...

Veo que tenéis los Andes cerquita... menudas ascensiones. Por cierto, ¿como llegaste al powershell si no lo buscabas eso?

Un saludo,