我终于找到了这个页面: http://www.vmdev.info/?p=202 我从他们展示的示例开始,它工作,所以我一直在添加CloneSpec字段,直到我得到我想要的:
$nicMaparray = @() $FirstNic = New-Object VMware.Vim.CustomizationAdapterMapping $FirstNic.adapter = New-Object VMware.Vim.CustomizationIPSettings $FirstNic.adapter.dnsDomain = $domain $FirstNic.adapter.dnsServerList = "10.26.40.115","10.26.40.116" $FirstNic.adapter.gateway = $DefGW $FirstNic.adapter.ip = New-Object Vmware.Vim.CustomizationFixedIp $FirstNic.adapter.ip.IpAddress = $NewIP $FirstNic.adapter.subnetMask = "255.255.248.0" $nicMaparray += $FirstNic $folderobj = $origvm.parent $vm = Get-VM $sourceName | Get-View $cloneName = $targetName $cloneFolder = Convert-PathToFolderObject -FolderPath $folderpath $cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec $cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec $cloneSpec.Location.Host = (Random(Get-VMHost | Where {$_.Name -like "*esxiqa*"}) | get-view).MoRef $targetDatastore = ($MapInfo | Where-Object {$_.Name -eq "Hard disk 1"}).NewDataStore $cloneSpec.Location.Datastore = (Get-Datastore $targetDatastore | get-view).MoRef $cloneSpec.customization = New-Object VMware.Vim.CustomizationSpec $cloneSpec.customization.globalIPSettings = New-Object VMware.Vim.CustomizationGlobalIPSettings $cloneSpec.customization.globalIPSettings.dnsServerList = "10.26.40.115","10.26.40.116" $cloneSpec.customization.identity = New-Object VMware.Vim.CustomizationSysprep # $spec.customization.identity.guiRunOnce = New-Object VMware.Vim.CustomizationGuiRunOnce $cloneSpec.customization.identity.guiUnattended = New-Object VMware.Vim.CustomizationGuiUnattended $cloneSpec.customization.identity.guiUnattended.autoLogonCount = 0 $cloneSpec.customization.identity.guiUnattended.password = New-Object VMware.Vim.CustomizationPassword $cloneSpec.customization.identity.guiUnattended.password.plainText = $true $cloneSpec.customization.identity.guiUnattended.password.value = Get-Password -Username "Administrator" $cloneSpec.customization.identity.identification = New-Object VMware.Vim.CustomizationIdentification $cloneSpec.customization.identity.identification.joinWorkgroup = "WORKGROUP" # $spec.customization.identity.licenseFilePrintData = $null $cloneSpec.customization.identity.userData = New-Object VMware.Vim.CustomizationUserData $cloneSpec.customization.identity.userData.computerName = New-Object VMware.Vim.CustomizationFixedName $cloneSpec.customization.identity.userData.computerName.name = $cloneName $cloneSpec.customization.identity.userData.productID = $ProductKey $cloneSpec.customization.nicSettingMap = $nicMaparray #nicMaparray build above # $cloneSpec.customization.options = $null $cloneSpec.powerOn = $true Get-HardDisk -VM $sourceName | %{ $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator $disk.diskId = $_.ExtensionData.Key #2001,2002,2003,... $DiskLabel = $_.ExtensionData.DeviceInfo.Label $dsname = ($MapInfo | Where-Object {$_.Name -eq $DiskLabel}).NewDataStore $ds = Get-Datastore -Name $dsname $disk.datastore = $ds.id $cloneSpec.Location.Disk += $disk } Write-Verbose "Cloning $sourceName" try { $vm.CloneVM( $cloneFolder, $cloneName, $cloneSpec) return $true } catch { $_.Exception return $false }
你不能复制到孤立的环境?只需构建一组额外(隔离)VLAN并复制所有虚拟机,保留IP /名称,仅在虚拟机级别切换VLAN。