Eu criei um script que atende às minhas necessidades e publicarei aqui.
Este script realiza as seguintes tarefas:
- Itera em todos os hosts físicos listados
- Itera em todas as VMs não excluídas
- Encerra as VMs e & aguarda o desligamento para concluir
- Executa a transferência síncrona do BITS de todos os arquivos VHD (um de cada vez) para as VMs de destino para o local especificado
- Boots segmentam VM após a conclusão da transferência
Esse script pode operar em hosts físicos remotos e não precisa ser executado localmente. Certifique-se de executar o script com credenciais administrativas ou não funcionará corretamente.
Estou usando o Windows Server 2008 R2 e, portanto, não tenho acesso a todos os comandos sofisticados disponíveis no Windows Server 2012.
Além disso, esta é a expressão final de "script kiddie" aqui; Eu misturei cerca de 4 scripts diferentes e, em seguida, adicionei um pouco de lógica sobre ele. Perdoe as inconsistências na sintaxe, etc.
Esse script foi transcrito à mão de um sistema corporativo autônomo que eu uso, portanto, pode haver alguns erros de ortografia. Eu não tentei executar este script diretamente. O do meu sistema parece funcionar bem.
#Run this script in an administrative instance of powershell
#This script executes based on files in the C:\Scripts directory
cd "C:\Scripts"
#Import the BITS Library so that synchronous transfers can occur easily
Import-Module BitsTransfer
#Note that all these files are delimited by carriage returns - 1 host/vm name per line
#Place these files in the "C:\Scripts" directory on the system running this script
#HyperVParents is the list of remote systems that are hosting VMs that need backup.
$HyperVParents = Get-Content HyperV_Hosts.txt
#ExcludedVMs is the list of VMs that will not be turned off or booted during this process
#NOTE: All VM VHD files in Hyper-V are backed up with this script.
$ExcludedVMs = Get-Content ExcludedVMs.txt
#Build Date String
$filedate = get-date -format "M-d-yyyy"
#Create target directory on remote server for backup if it is not already present
#Replace REMOTESRV with your servername
$TargetPath = "\REMOTESRV\VHDs\" + $filedate
If(!(Test-Path -Path $TargetPath))
{
New-Item -ItemType directory -Path $TargetPath
}
Foreach ($HyperVParent in $HyperVParents)
{
$VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -ComputerName $HyperVParent
$VMs = Get-WmiObject -Namespace "root\virtualization" -ComputerName $HyperVParent -Query "Select * From MSVM_ComputerSystem where Caption='Virtual Machine'"
Foreach ($VM in $VMs)
{
#Set $VMExcluded to null in order to test the next VM for exclusion
#This routine could probably be more efficient.
$VMExcluded = $null
#Loop through list of excluded VMs to see if current VM is within this list
Foreach ($ExcludedVM in $ExcludedVMs)
{
If ($VM.ElementName -eq $ExcludedVM)
{
$VMExcluded = $true
Write-Host $VM.ElementName, ": Excluded from startup/shutdown process"
}
}
$VMSettingData = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$VM} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState" -ComputerName $HyperVParent
$VirtualDiskResource = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$VMSettingData} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" -ComputerName $HyperVParent | Where-Object {$_.ResourceSubType -match "Microsoft Virtual Hard Disk"}
$VHD_Path = $null
$vmGUID = $VM.name
#Start logical check that skips unused systems. They will not be powered off or on.
If ($VMExcluded -eq $null)
{
$Query = "select * from Msvm_computersystem where Name='" + $vmGUID + "'"
$VMTemp = gwmi -namespace root\virtualization -query $Query -ComputerName $HyperVParent
#Only attempt a shutdown if the system is powered on
If ($VMTemp.EnableState -ne "3")
{
Write-Host "System Requires Shutdown:", $VM.ElementName
#Build SQL Query - Use the target GUID here since the Msvm_ShutdownComponent seems to be only targeted with a GUID
$ShutdownQuery = "SELECT * from Msvm_ShutdownComponent WHERE SystemName ='" + $vmGUID + "'"
#Execute the query to select the shutdown component of the target VM
$vmshut = gwmi -namespace root\virtualization -query $ShutdownQuery -ComputerName $HyperVParent
$result = $vmshut.InitiateShutdown("$true","VHD Backup Process");
Write-Host "Shutting Down:", $VM.ElementName
#Wait for system to shutdown
Do {
#Increment 1 sec pauses for each loop
Start-Sleep -s 1
#Use a different variable here so that the original target VM is not modified
#Requery the VM each second to get an updated EnabledState
$VMTemp = gwmi -namespace root\virtualization -query $Query -ComputerName $HyperVParent
}
While ($VMTemp.EnabledState -ne "3");
Write-Host $VM.ElementName, "successfully shutdown"
}
Else
{
Write-Host $VM.ElementName, ": Already shutdown"
}
}
#End Logical check for systems that are unused
#Perform the file transfer of the VHD file afer the VM has shut down.
Foreach ($VHD in $VirtualDiskResource)
{
$VHD_PATH = $VHD.Connection[0] + ","
#Replace colon in path with dollar sign for UNC purposes
$VHD_PATH_TEMP = $VHD.Connection[0] -replace ':','$'
#Build Source String
$BackupSource = "\" + $HyperVParent + "\" + "VHD_PATH_TEMP"
#Extract VHD Name from the path for use in the target file
$VHDName = Split-Path -Leaf $VHD.Connection[0]
#Build Target String; $TargetPath was built at the initiation of the script
$BackupTarget = $TargetPath + "\" + $VHDName
Write-Host "Beginning Backup:", $VM.ElementName, $BackupSource
#Transfer VHD file to the target server using BITS
StartBitsTransfer -Source $BackupSource -Destination $BackupTarget
Write-Host "Backup Complete:", $VM.ElementName, $BackupTarget
}
#Here backed up systems are turned back on after the file transfer of their VHD is complete.
#Filter out certain unused VMs - unused systems do not need to be booted.
If ($VMExcluded -eq $null)
{
Write-Host "Starting VM:", $VM.ElementName
#Boot the VM before the script loops and moves to the next system.
$result = $VM.requeststatechange(2)
}
}
}