다음 샘플 스크립트를 사용자 지정한 후 사용하여 VM(가상 시스템)을 개수에 제한 없이 복제할 수 있습니다.

페이지 구분 없이 스크립트 내용을 복사한 후 붙여넣으려면 https://www.vmware.com/support/pubs/view_pubs.htmlHorizon 7 설명서 페이지에서 이 항목의 HTML 버전을 사용하십시오.

스크립트 입력

이 스크립트는 Linux 데스크톱 배포를 위한 샘플 PowerCLI 스크립트의 입력 파일에 설명된 단일 입력 파일을 읽습니다. 이 스크립트는 또한 다음 정보를 대화형으로 요청합니다.

  • vCenter Server의 IP 주소

  • vCenter Server의 관리자 로그인 이름

  • vCenter Server의 관리자 암호

  • 복제 유형(링크 가능 또는 전체)

  • vSphere VM 콘솔을 비활성화할지 여부

스크립트 내용

<#
Create Clones from a Master VM

The Tool supports creation of Full clone and linked clone from Master VM.
The parent VM is required for the linked-clone to work and the parent VMs file cannot be renamed or moved.
#>
#------------------------- Functions -------------------------
function GetInput
{
    Param($prompt, $IsPassword = $false)
    $prompt = $prompt + ": "
    Write-Host $prompt -NoNewLine
    [Console]::ForegroundColor = "Blue"
    if ($IsPassword)
    {
        $input = Read-Host -AsSecureString
        $input = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($input))
    }
    else
    {
        $input = Read-Host
    }
    
    [Console]::ResetColor()
    return $input
}

function IsVMExists ()
{
    Param($VMExists)
	Write-Host "Checking if the VM $VMExists already Exists"
	[bool]$Exists = $false

	#Get all VMS and check if the VMs is already present in VC
	$listvm = Get-vm
	foreach ($lvm in $listvm)
	{
		if($VMExists -eq $lvm.Name )
		{
			$Exists = $true
		}
	}
	return $Exists
}
function Disable_VM_Console()
{
    Param($VMToDisableConsole)
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="RemoteDisplay.maxConnections"
    $extra.Value="0"
    $vmConfigSpec.extraconfig += $extra
    $vm = Get-VM $VMToDisableConsole | Get-View
    $vm.ReconfigVM($vmConfigSpec)
}


function Delete_VM()
{
    Param($VMToDelete)
	Write-Host "Deleting VM $VMToDelete"
	Get-VM $VMToDelete | where { $_.PowerState –eq "PoweredOn" } | Stop-VM –confirm:$false
	Get-VM $VMToDelete | Remove-VM –DeleteFromDisk –confirm:$false
}

#------------------------- Main Script -------------------------

$vcAddress = GetInput -prompt "Your vCenter address" -IsPassword $false
$vcAdmin = GetInput -prompt "Your vCenter admin user name" -IsPassword $false
$vcPassword = GetInput -prompt "Your vCenter admin user password" -IsPassword $true
$cloneType = GetInput -prompt 'Clone Type ("linked" or "full")' -IsPassword $false
$disableVMConsole = GetInput -prompt 'Disable vSphere VM Console ("yes" or "no", recommend "yes")' -IsPassword $false
"-----------------------------------------------------"
$csvFile = '.\CloneVMs.csv'

# Check that user passed only linked or full clone
if (($CloneType.length >0) -and ($CloneType -ne "linked" -or $CloneType -ne "full"))
{
	write-host  -ForeGroundColor Red "Clone type supports only 'linked' or 'full' (case sensitive)"
	exit
}
if (($disableVMConsole.length >0) -and ($disableVMConsole -ne "yes" -or $disableVMConsole -ne "no"))
{
	write-host  -ForeGroundColor Red "Disable vSphere VM Console supports only 'yes' or 'no' (case sensitive)"
	exit
}

#check if file exists
if (!(Test-Path $csvFile))
{
	write-host  -ForeGroundColor Red "CSV File $CSVFile not found"
	exit
}

# Connect to the VC (Parameterize VC)
#Connect to vCenter
$VC_Conn_State = Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword
if([string]::IsNullOrEmpty($VC_Conn_State))
{
   Write-Host 'Exit since failed to login vCenter'
   exit
}
else
{
  Write-Host 'vCenter is connected'
}

#Read input CSV file
$csvData = Import-CSV $csvFile 
#$csvData = Import-CSV $csvFile -header("VMName","Parentvm","CustomSpec","Datastore","Host","FromSnapshot","DeleteIfPresent")
foreach ($line in $csvData)
{
    "`n-----------------------------------------------------"
    $VMName = $line.VMName
    write-host -ForeGroundColor Yellow "VM: $VMName`n"

    $destVMName=$line.VMName
    $srcVM = $line.Parentvm
	$cSpec = $line.CustomSpec
	$targetDSName = $line.Datastore
    $destHost = $line.Host
	$srcSnapshot = $line.FromSnapshot
	$deleteExisting = $line.DeleteIfPresent
	if (IsVMExists ($destVMName))
	{
		Write-Host "VM $destVMName Already Exists in VC $vcAddress"
		if($deleteExisting -eq "TRUE")
		{
			Delete_VM ($destVMName)
		}
		else
		{
			Write-Host "Skip clone for $destVMName"
            continue
		}
	}    
    $vm = get-vm $srcvm -ErrorAction Stop | get-view -ErrorAction Stop
	$cloneSpec = new-object VMware.VIM.VirtualMachineCloneSpec
	$cloneSpec.Location = new-object VMware.VIM.VirtualMachineRelocateSpec
	if ($CloneType -eq "linked")
	{
		$cloneSpec.Location.DiskMoveType = [VMware.VIM.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking
	}    
	Write-Host "Using Datastore $targetDSName"
	$newDS = Get-Datastore $targetDSName | Get-View
	$CloneSpec.Location.Datastore =  $newDS.summary.Datastore
    Set-VM -vm $srcVM -snapshot (Get-Snapshot -vm $srcVM -Name $srcSnapshot) -confirm:$false
    $cloneSpec.Snapshot = $vm.Snapshot.CurrentSnapshot
	$cloneSpec.Location.Host = (get-vmhost -Name $destHost).Extensiondata.MoRef
	$CloneSpec.Location.Pool = (Get-ResourcePool -Name Resources -Location (Get-VMHost -Name $destHost)).Extensiondata.MoRef
    # Start the Clone task using the above parameters
	$task = $vm.CloneVM_Task($vm.parent, $destVMName, $cloneSpec)
    # Get the task object
	$task = Get-Task | where { $_.id -eq $task }
    #Wait for the taks to Complete
    Wait-Task -Task $task
    
    $newvm = Get-vm $destVMName
    $customSpec = Get-OSCustomizationSpec $cSpec
    Set-vm -OSCustomizationSpec $cSpec -vm $newvm -confirm:$false 
	if ($disableVMConsole -eq "yes")
	{
		Disable_VM_Console($destVMName)
	} 
    # Start the VM
	Start-VM $newvm
}
Disconnect-VIServer $vcAddress -Confirm:$false
exit

스크립트 실행

스크립트를 실행하면 다음 메시지가 표시됩니다.

PowerCLI C:\scripts> .\CloneVMs.ps1
Your vCenter address: 10.117.44.17
Your vCenter admin user name: administrator
Your vCenter admin user password: *******
Clone Type<"linked" or "Full"> : linked
Disable vSphere VM Console ("yes" or "no", recommend "yes") : yes

복제 프로세스에 소요되는 시간은 데스크톱 시스템 수에 따라 좌우되며 몇 분에서 몇 시간까지 걸릴 수 있습니다. 프로세스가 완료되었는지 확인하려면 vSphere Client에서 마지막 가상 시스템의 전원이 켜져 있고, 고유한 호스트 이름이 있으며, VMware Tools가 실행되고 있는지 확인하십시오.