克隆虚拟机更改网络身份 [英] clone vm change network identity

查看:238
本文介绍了克隆虚拟机更改网络身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写脚本(powershell / powercli)QA环境(261台服务器)的克隆。
基本上,我只想更改VM名称,主机名和IP地址就可以为每个服务器制作一个新副本。理想情况下,我希望在域上创建新的克隆(其中90%是Windows操作系统)。而且我的许多服务器都有许多无法完全容纳在一个数据存储中的大型硬盘,因此我必须能够克隆到多个数据存储。

I'm trying to script (powershell/powercli) the cloning of our QA environment (261 servers). Basically I want to make a new copy of each server just changing the VM name, the hostname, and the IP address. Ideally I'd want the new clone on the domain (90% of them are Windows OS). And many of my servers have many large hard drives that won't all fit on one datastore so I have to be able to clone to multiple datastores.

服务器的New-VM足够小,可以容纳一个数据存储,但是OSCustomization只能在大约30%的时间内工作。其余时间,我必须登录Windows并手动从域中删除以重命名主机名。在大多数情况下,Set-OSCustomizationNicMapping都有效,但是指定-Domain -DomainUsername / Password永远无效(充其量它会重命名服务器并放入域名工作组,但永远不会将其加入域)。

I started off with New-VM for the servers that were small enough to fit on one datastore, but OSCustomization only works about 30% of the time. The rest of the time I have to login to Windows and manually remove from the domain to rename the hostname. Most of the time Set-OSCustomizationNicMapping works But specifying -Domain -DomainUsername/Password never works (at best it renames the server and puts in the "domain-name" workgroup but it never joins it to the domain).

要解决多数据存储问题,我使用VMWare.Vim.VirtualMachineRelocateSpec发现了$ vm.ExtensionData.CloneVM,以指定将哪些硬盘驱动器转到哪些数据存储,并且效果很好,但是我不知道如何

To workaround the multidatastore issue I discovered $vm.ExtensionData.CloneVM using VMWare.Vim.VirtualMachineRelocateSpec to specify which hard drives go to which datastores and that works great, but I don't know how to customize the hostname or NIC settings.

因此,基本要求是:克隆到多个更改主机名和IP设置的数据存储。有人有任何建议或解决方案吗?

So the basic requirements are: clone to multiple datastores changing hostname and IP settings. Does anyone have any recommendations or solutions?

以下是我的一些代码段:

Here's some of my code snippets:

为以下内容设置OSCustomizationSpec New-VM

Setting up the OSCustomizationSpec for New-VM

$spec = New-OSCustomizationSpec -Name "PowerCLI Scripting for $NewHostName" -Spec "PowerCLI Scripting" -WhatIf:$False
Set-OSCustomizationSpec $spec -Workgroup "WORKGROUP" -DomainUsername "qajoin@qa.company.com" -DomainPassword (Get-Password -Username "qa\qajoin") -ProductKey $ProductKey -AdminPassword (Get-Password $Script:LuserName) | Out-Null
Get-OSCustomizationSpec "PowerCLI Scripting for $NewHostName" `
| Get-OSCustomizationNicMapping `
| Set-OSCustomizationNicMapping `
-IPMode:UseStaticIP `
-IPAddress $NewIPAddress `
-SubnetMask "255.255.248.0" `
-DNS "10.26.40.115","10.26.40.116" `
-DefaultGateway $NewDFGW | Out-Null

New-VM命令:

$VM = New-VM -VMHost $VMHost -VM $Hostname -Name $NewHostName -Description "$Description" -OSCustomizationSpec "PowerCLI Scripting for $NewHostName" -Location (Get-Folder -Id $Location) -Datastore $MostFreeSpace -ErrorAction Stop

这里是多数据存储克隆:

Here's the multi-datastore clone:

$VMXtargetDatastore = Get-Datastore ($MapInfo | Where-Object {$_.Name -eq "Hard disk 1"}).NewDataStore

#Create an empty CloneSpec
$spec = New-Object VMware.Vim.VirtualMachineCloneSpec 
$spec.Template = $false
 $spec.PowerOn = $false

 #Create a RelocateSpec (datastore is target for .vmx)
$spec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.Location.Datastore = $targetDatastore.ExtensionData.MoRef

#For each disk in the current vm
#  create a new DiskLocator spec
#  populate the datastore and diskid from the current harddisk
#  add the spec to RelocateSpec from above
Get-HardDisk -VM $Origvm | %{
    $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
    $disk.diskId = $_.ExtensionData.Key #2001,2002,2003,...
    $DiskLabel = $_.ExtensionData.DeviceInfo.Label
    #$disk.datastore = $_.ExtensionData.Backing.Datastore #type=datastore value=datastore-2790
    $dsname = ($MapInfo | Where-Object {$_.Name -eq $DiskLabel}).NewDataStore
    $ds = Get-Datastore -Name $dsname
    $disk.datastore = $ds.id
    $spec.Location.Disk += $disk
}

$CustSpec = New-Object VMware.Vim.CustomizationSpec
$origvm.ExtensionData.CloneVM((Get-Folder -Id $folder).ExtensionData.MoRef, $targetName, $spec)

我想使用ExtensionData.CloneVM方法的下一步是声明CustomizationIdentitySettings的新对象,但声明文档( http: //pubs.vmware.com/vi3/sdk/Re我可以找到的ferenceGuide / vim.vm.customization.IdentitySettings.html )并没有太大帮助(可能是因为设置因操作系统而异),然后将该对象添加到$ spec对象中。
OSCustomizationNicMapping也将采用同样的方法( http:// www .vmware.com / support / developer / PowerCLI / PowerCLI41U1 / html / OSCustomizationNicMapping.html ),但我不能在酒吧外浪费足够的信息来弄清楚如何构建$ spec对象。

I'm guessing the next step using the ExtensionData.CloneVM method is to declare a new object of CustomizationIdentitySettings but the documentation (http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.customization.IdentitySettings.html) that I could find isn't really helpful (probably because the settings vary by OS?) and then add that object to my $spec object. Same would go for OSCustomizationNicMapping (http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/OSCustomizationNicMapping.html) but I can't suss enough information out of the pubs to figure out how to build my $spec object.

推荐答案

我终于找到了此页面: http://www.vmdev.info/?p=202
我从他们显示的示例开始并且它起作用了,所以我一直在添加CloneSpec字段,直到获得所需的内容:

I finally found this page: http://www.vmdev.info/?p=202 I started with the example they show and it worked, so I kept adding in CloneSpec fields until I got what I wanted:

    $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
    }

这篇关于克隆虚拟机更改网络身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆