Question Export de VM avec Hyper-V V3

Plus d'informations
il y a 12 ans 4 mois #16721 par Willy
Export de VM avec Hyper-V V3 a été créé par Willy
Bonjour,

Je cherche à exporter des VMs Hyper-V en Powershell.
J'utilisais avec succès un script en Powershell pour sauvegarder mes machines sur Windows serveur 2008,2008 R2, et 2012 néanmoins de puis Windows serveur 2012 R2 ce même script plante lamentablement.

Ayant fait quelque recherche il est dit que \"ExportVirtualSystem\" est obsolète et d'utilisé la methode \"ExportVirtualSystemEX\"

Le script suivant est un exemple simplifier de sauvegarde :

[code:1]
# Machine hôte
$HyperVServer = \"VS01\"

# VM
$VMName = \"VM01\"

# Dossier des sauvegardes
$ExportPath = \"E:\Backup\HYPER-V\"

# Get the management service
$VMMS = gwmi -namespace root\virtualization\v2 Msvm_VirtualSystemManagementService -computername $HyperVServer

# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter \"ElementName='$VMName'\" -namespace \"root\virtualization\v2\" -computername $HyperVServer

# Export the virtual machine
$result = $VMMS.ExportVirtualSystem($VM, $true, $ExportPath)
[/code:1]

Rapport d'erreur :

Echec lors de l'appel de la méthode, car [System.Management.ManagementObject] ne contient pas la méthode nommée \"ExportVirtualSystem\"

Et avec le code suivante:

[code:1]
$ns = \"root\virtualization\v2\"
$expDir =\"E:\Backup\HYPER-V\"

#Get VM Object
$vm = gwmi -n $ns Msvm_ComputerSystem | ?{$_.ElementName -eq 'VM01'}

#Get export setting object
$exp = @($vm.GetRelated('Msvm_VirtualSystemExportSettingData'))[0]

#If you dont want to copy the VHDs and AVHDs
$exp.CopyVmStorage = $false

#If you dont want to copy the Saved state
$exp.CopyVmRuntimeInformation = $false

#Get VMMS object
$vmms = gwmi -n $ns Msvm_VirtualSystemManagementService

#Perform Export
$out = $vmms.ExportVirtualSystemEx($vm.Path.Path, $expDir, $exp.GetText(1))

#Perform Job handling if necessary
if ($out.ReturnValue -eq 4096)
{
$task = [Wmi]$out.Job;
while($task.JobState -eq 3 -or $task.JobState -eq 4)
{
$task.Get();
sleep 1;
}
if ($task.JobState -ne 7)
{
\"Error exporting VM \" + $task.ErrorDescription;
}
else
{
\"Export completed successfully...\"
}

}
elseif ($out.ReturnValue -ne 0)
{
\"Export failed with error : \" + $out.ReturnValue;
}
else
{
\"Export completed successfully...\"
}
[/code:1]

Rapport d'erreur :

Echec lors de l'appel de la méthode, car [System.Management.ManagementObject] ne contient pas la méthode nommée \"ExportVirtualSystemEX\"

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 12 ans 4 mois #16722 par ANTRI Mohamed
Bonsoir,

Vous n'avez pas besoin d'utiliser les classes WMI pour exporter une VM. Avec windows serveur 2012 r2 et powershell 4.0 vous avez la cmdlets \"Export-VM\" à essayer.

Merci de nous tenir informé. :P

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 12 ans 4 mois #16724 par Willy
Réponse de Willy sur le sujet Re:Export de VM avec Hyper-V V3
Merci j'ai refait mon script et je partage avec vous la solution.

[code:1]
function ExportVM(){
param(
[Parameter(Mandatory=$true)][string]$VM,
[Parameter(Mandatory=$true)][string]$State,
[Parameter(Mandatory=$true)][string]$Path,
[Parameter(Mandatory=$true)][string]$LogFile
)

try {
if ($State -eq \"Running\"«») {

Write-Host \"$($VM) is currently running, will suspend it.\" -foregroundcolor green
Write-Output \"$(Get-Date -Format G) ## $($VM) is currently running, will suspend it.\" -ErrorAction Stop | Out-File $LogFile -Append
Stop-VM –Name $VM -Save -ErrorAction Stop

sleep 4

Write-Host \"Exporting $($VM) to $($Path)\" -foregroundcolor green
Write-Output \"$(Get-Date -Format G) ## Exporting $($VM) to $($Path).\" -ErrorAction Stop | Out-File $LogFile -Append
Export-VM -Name $VM -Path $($Path) -ErrorAction Stop

Write-Host \"Starting $($VM)\" -foregroundcolor green
Start-VM -Name $VM -ErrorAction Stop

} elseif ($State -eq \"Off\"«») {
Write-Host \"$($VM) is already turned off\" -foregroundcolor green
Write-Host \"Exporting $($VM) to $($Path).\" -foregroundcolor green
Write-Output \"$(Get-Date -Format G) ## $($VM) is already turned off.\" -ErrorAction Stop | Out-File $LogFile -Append
Write-Output \"$(Get-Date -Format G) ## Exporting $($VM) to $($Path).\" -ErrorAction Stop | Out-File $LogFile -Append
Export-VM -Name $VM -Path $($Path) -ErrorAction Stop

}

return $true
} catch {
Write-Output \"$(Get-Date -Format G) ## ERROR: $($_.Exception.Message)\" | Out-File $LogFile -Append
return $false
}
}
[/code:1]

Utilisation :

[code:1]
## VM
$VMsBck = VM01,VM02,VM03

$Date = get-date -f \"dd-MM-yy\"

## LOG
$path = \"C:\Hyper-V\log-$Date.txt\"
$log = New-Item $path -Type file -Force

## VMs list
$VMs=($VMsBck).split(\",\"«»)

Get-VM | ForEach-Object {
foreach ($Guest in [array] $VMs){
if($_.Name -eq $Guest){

ExportVM -VM $Guest -State $_.State -Path $Path -LogFile $log

}
}
}
[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.034 secondes
Propulsé par Kunena