none
Import hyper-v machine to cluster powershell

    Question

  • Hey, anyone can help me?

    i have a script that export a virtual machine from hyper-v and import on hyper-v now i have to import that machine to cluster, that can be using VMM or FailOverCluster Modules, anyone have any idea how do that?

    can be with WMI or other thing...

    cls
    
    $VMName = read-host "Nome da vm"
    $ExportPath = read-host "Local for export"
    $serverExport = read-host "Server where export"
    $ServerImport = read-host "Server where import"
    
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $ServerExport
    $VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerExport
    $VM.RequestStateChange(3)
    while($VM.EnabledState -notlike 3)
    {
    	$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerExport	
    }
    $result = $VMMS.ExportVirtualSystem($VM, $true, $ExportPath)
    $job=[WMI]$result.job
    
    while($job.JobState -eq 3 -or $job.JobState -eq 4)
    {
    	write-progress -activity "Search in Progress" -status "% Exporting:" -percentcomplete $job.PercentComplete
    	start-sleep 1
    	$job=[WMI]$result.job
    }
    $job=[WMI]$result.job
    if ($job.JobState -eq 7)
    {
    	write-host "Export with success"
    }
    Else
    {
    	write-host "Exporting fail"
            write-host "ErrorCode:" $job.ErrorCode
            write-host "ErrorDescription" $job.ErrorDescription
    }
           
    
    
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $ServerImport
    $FullPath = $ExportPath+"\"+$VMName
    $result = $VMMS.ImportVirtualSystem($FullPath,$true)
    $job=[WMI]$result.job
    while($job.JobState -eq 3 -or $job.JobState -eq 4)
    {
    	write-progress -activity "Search in Progress" -status "% Importing:" -percentcomplete $job.PercentComplete
    	start-sleep 1
    	$job=[WMI]$result.job
    }
    $job=[WMI]$result.job
    if ($job.JobState -eq 7)
    {
    	write-host "Import with success"
    	$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerImport
    	$VM.RequestStateChange(2)#2 start 3 shutdown
    	import-modules FailoverClusters
    }
    Else
    {
    	write-host "Import fail"
            write-host "ErrorCode:" $job.ErrorCode
            write-host "ErrorDescription" $job.ErrorDescription
    }


    Meu Blog: http://howtoserver.com Minhas Ceritificações: 73-642,73-640

    Wednesday, May 09, 2012 2:53 AM

Answers

  • Hi,

    You can use the following PowerShell CMDlet to add an existing virtual machine to cluster. Make sure you have put the virtual machine in the shard volumes, such as CSV.

    Add-ClusterVirtualMachineRole –VirtualMachine Test01

    Note: Test01 is the virtual machine name in my test, adjust it accordingly.

    For more information, you can refer to:

    http://technet.microsoft.com/en-us/library/ee460967.aspx

    Wednesday, May 09, 2012 1:01 PM
    Moderator
  • Here a script if anyone need... it export VM form one hyper-v server to other hyper-v server and add the machine to cluster.

    cls
    
    $VMName = read-host "Nome da vm"
    $ExportPath = read-host "Local para exportar"
    $serverExport = read-host "Servidor de onde exportar"
    $ServerImport = read-host "Servidor onde importar"
    
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $ServerExport
    $VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerExport
    $VM.RequestStateChange(3)
    while($VM.EnabledState -notlike 3)
    {
    	$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerExport	
    }
    $result = $VMMS.ExportVirtualSystem($VM, $true, $ExportPath)
    $job=[WMI]$result.job
    
    while($job.JobState -eq 3 -or $job.JobState -eq 4)
    {
    	write-progress -activity "Search in Progress" -status "% Exportando:" -percentcomplete $job.PercentComplete
    	start-sleep 1
    	$job=[WMI]$result.job
    }
    $job=[WMI]$result.job
    if ($job.JobState -eq 7)
    {
    	write-host "Exportou com sucesso"
    }
    Else
    {
    	write-host "Exportação falhou"
            write-host "ErrorCode:" $job.ErrorCode
            write-host "ErrorDescription" $job.ErrorDescription
    }
           
    
    
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $ServerImport
    $FullPath = $ExportPath+"\"+$VMName
    $result = $VMMS.ImportVirtualSystem($FullPath,$true)
    $job=[WMI]$result.job
    while($job.JobState -eq 3 -or $job.JobState -eq 4)
    {
    	write-progress -activity "Search in Progress" -status "% Importanto:" -percentcomplete $job.PercentComplete
    	start-sleep 1
    	$job=[WMI]$result.job
    }
    $job=[WMI]$result.job
    if ($job.JobState -eq 7)
    {
    	write-host "Importou com sucesso"
    	$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerImport
    	import-modules FailoverClusters
    	Add-ClusterVirtualMachineRole -VirtualMachine $VMNAME 
    	$VM.RequestStateChange(2)#2 liga 3 desliga
    }
    Else
    {
    	write-host "Importação falhou"
            write-host "ErrorCode:" $job.ErrorCode
            write-host "ErrorDescription" $job.ErrorDescription
    }
    System center is wonderful, with SCVMM 2012 i do all that script in 
    $VMNAMES = read-host "Name(s) of(s) vm(s) to migrate ('Name0','name1')"
    $VMM = read-host "vmm server (FQDN)"
    $VMHost = read-host "Hyper-v server destination (FQDN)"
    $Path = read-host "CSV path to move (RHCLOUDXX)"
    $CloudName = read-host "Name of the cloud of destination"
    $Path = "C:\Clusterstorage\"+$Path
    foreach($VMNAME in $VMNAMES)
    {
    	$VM = Get-SCVirtualMachine -VMMServer $VMM | ?{$_.name -like "$VMNAME"}
    	Move-SCVirtualMachine -VM $VM -VMHost $VMHost -HighlyAvailable $true -Path $Path -UseLAN 
    	$cloud = Get-SCCloud -VMMServer $VMM | where {$_.Name -eq $CloudName}
    	$VM | Set-SCVirtualMachine -cloud $cloud
    	$UserRole = Get-SCUserRole -VMMServer $VMM | where {$_.Name -eq "Suporte-users"} 
    	$VM = Get-SCVirtualMachine -VMMServer $VMM | ?{$_.name -like "$VMNAME"}
    	$VM | Set-SCVirtualMachine -Owner "REDEHOST\SUPORTE-USERS" -UserRole $UserRole
    }



    Meu Blog: http://howtoserver.com Minhas Ceritificações: 73-642,73-640


    Wednesday, May 09, 2012 1:35 PM

All replies

  • Hi,

    If you want to make a virtual machine High Availability in Failover Cluster or SCVMM, you need to store the virtual machine in a shared storage, that’s the key.

    How to Export Non Clustered VM’S and Import Them to CSV Shares on 2008 R2 Cluster

    http://blogs.technet.com/b/askcore/archive/2011/05/26/how-to-export-non-clustered-vm-s-and-import-them-to-csv-shares-on-2008-r2-cluster.aspx

    Wednesday, May 09, 2012 5:20 AM
    Moderator
  • Ok, i am doing that, i export machine to a CSV and import on hyper-v but need to import it on the cluster.

    my script do exactly the same of that you send me, i need to do the last part import vm from hyper-v to cluster...


    Meu Blog: http://howtoserver.com Minhas Ceritificações: 73-642,73-640


    Wednesday, May 09, 2012 11:19 AM
  • Hi,

    You can use the following PowerShell CMDlet to add an existing virtual machine to cluster. Make sure you have put the virtual machine in the shard volumes, such as CSV.

    Add-ClusterVirtualMachineRole –VirtualMachine Test01

    Note: Test01 is the virtual machine name in my test, adjust it accordingly.

    For more information, you can refer to:

    http://technet.microsoft.com/en-us/library/ee460967.aspx

    Wednesday, May 09, 2012 1:01 PM
    Moderator
  • I found a function on VMM 2012 for migrate machines between clusters, now i using it directly instead to make on hyper-v

    Meu Blog: http://howtoserver.com Minhas Ceritificações: 73-642,73-640

    Wednesday, May 09, 2012 1:18 PM
  • Here a script if anyone need... it export VM form one hyper-v server to other hyper-v server and add the machine to cluster.

    cls
    
    $VMName = read-host "Nome da vm"
    $ExportPath = read-host "Local para exportar"
    $serverExport = read-host "Servidor de onde exportar"
    $ServerImport = read-host "Servidor onde importar"
    
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $ServerExport
    $VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerExport
    $VM.RequestStateChange(3)
    while($VM.EnabledState -notlike 3)
    {
    	$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerExport	
    }
    $result = $VMMS.ExportVirtualSystem($VM, $true, $ExportPath)
    $job=[WMI]$result.job
    
    while($job.JobState -eq 3 -or $job.JobState -eq 4)
    {
    	write-progress -activity "Search in Progress" -status "% Exportando:" -percentcomplete $job.PercentComplete
    	start-sleep 1
    	$job=[WMI]$result.job
    }
    $job=[WMI]$result.job
    if ($job.JobState -eq 7)
    {
    	write-host "Exportou com sucesso"
    }
    Else
    {
    	write-host "Exportação falhou"
            write-host "ErrorCode:" $job.ErrorCode
            write-host "ErrorDescription" $job.ErrorDescription
    }
           
    
    
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $ServerImport
    $FullPath = $ExportPath+"\"+$VMName
    $result = $VMMS.ImportVirtualSystem($FullPath,$true)
    $job=[WMI]$result.job
    while($job.JobState -eq 3 -or $job.JobState -eq 4)
    {
    	write-progress -activity "Search in Progress" -status "% Importanto:" -percentcomplete $job.PercentComplete
    	start-sleep 1
    	$job=[WMI]$result.job
    }
    $job=[WMI]$result.job
    if ($job.JobState -eq 7)
    {
    	write-host "Importou com sucesso"
    	$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $ServerImport
    	import-modules FailoverClusters
    	Add-ClusterVirtualMachineRole -VirtualMachine $VMNAME 
    	$VM.RequestStateChange(2)#2 liga 3 desliga
    }
    Else
    {
    	write-host "Importação falhou"
            write-host "ErrorCode:" $job.ErrorCode
            write-host "ErrorDescription" $job.ErrorDescription
    }
    System center is wonderful, with SCVMM 2012 i do all that script in 
    $VMNAMES = read-host "Name(s) of(s) vm(s) to migrate ('Name0','name1')"
    $VMM = read-host "vmm server (FQDN)"
    $VMHost = read-host "Hyper-v server destination (FQDN)"
    $Path = read-host "CSV path to move (RHCLOUDXX)"
    $CloudName = read-host "Name of the cloud of destination"
    $Path = "C:\Clusterstorage\"+$Path
    foreach($VMNAME in $VMNAMES)
    {
    	$VM = Get-SCVirtualMachine -VMMServer $VMM | ?{$_.name -like "$VMNAME"}
    	Move-SCVirtualMachine -VM $VM -VMHost $VMHost -HighlyAvailable $true -Path $Path -UseLAN 
    	$cloud = Get-SCCloud -VMMServer $VMM | where {$_.Name -eq $CloudName}
    	$VM | Set-SCVirtualMachine -cloud $cloud
    	$UserRole = Get-SCUserRole -VMMServer $VMM | where {$_.Name -eq "Suporte-users"} 
    	$VM = Get-SCVirtualMachine -VMMServer $VMM | ?{$_.name -like "$VMNAME"}
    	$VM | Set-SCVirtualMachine -Owner "REDEHOST\SUPORTE-USERS" -UserRole $UserRole
    }



    Meu Blog: http://howtoserver.com Minhas Ceritificações: 73-642,73-640


    Wednesday, May 09, 2012 1:35 PM