PowerCLI

 View Only
  • 1.  Change VM ip address

    Posted Jul 04, 2009 06:13 PM

    I am trying to get the change vm ip address (from the PowerCLI faq http://communities.vmware.com/docs/DOC-4210) to work but get an error. The error can be found in the attachment.

    This is the code:

    $vmclonespec = New-Object VMware.Vim.VirtualMachineCloneSpec

    $vmclonespec.Customization = New-Object VMware.Vim.CustomizationSpec

    $vmclonespec.Customization.NicSettingMap = @(New-Object VMware.Vim.CustomizationAdapterMapping)

    $vmclonespec.Customization.NicSettingMap[0].Adapter = New-Object VMware.Vim.CustomizationIPSettings

    $vmclonespec.Customization.NicSettingMap[0].Adapter.ip = New-Object VMware.Vim.CustomizationFixedIp

    $vmclonespec.Customization.NicSettingMap[0].Adapter.Ip.IpAddress = "192.168.123.123"

    $vmclonespec.Customization.Identity = New-Object vmware.Vim.CustomizationIdentitySettings

    $vmclonespec.Customization.GlobalIPSettings = New-Object VMware.Vim.CustomizationGlobalIPSettings

    $vmclonespec.config = New-Object VMware.Vim.VirtualMachineConfigSpec

    $vmclonespec.location = New-Object VMware.Vim.VirtualMachineRelocateSpec

    $vmclonespec.powerOn = $false

    $vmclonespec.template = $false

    $name = "Test"

    $target = Get-Folder -Name "Lab" | % {Get-View $_.ID}

    $vmmor = Get-VM -Name "WXPLAB" | Get-View

    $vmmor.CloneVM_Task($target.MoRef ,$name, $vmclonespec )



  • 2.  RE: Change VM ip address

    Broadcom Employee
    Posted Jul 04, 2009 06:42 PM

    I believe your customization spec is incomplete. I think you need to populate the object a little more fully, but I am not 100% certain what you need to provide to ONLY change the IP address. Your error is being caused by not having the following. From there, I am still following the trail of breadcrumbs left by subsequent error messages :smileyhappy:

    $vmcloneSpec.Customization.Identity = New-Object VMware.Vim.CustomizationSysprep
    



  • 3.  RE: Change VM ip address

    Broadcom Employee
    Posted Jul 04, 2009 08:08 PM

    It may be helpful to look here

    http://communities.vmware.com/message/1301800#1301800

    for an example of a script that populates the customization spec.



  • 4.  RE: Change VM ip address

    Broadcom Employee
    Posted Jul 04, 2009 09:38 PM

    After much trial and error, I think I have found out how to do it without the need to clone the VM. I don't know if this is useful, but you should be able to construct the same CustomizationSpec and apply it during a CloneVM operation instead of CustomizeVM.

    #connect to VirtualCenter with credentials
    Connect-VIServer -Server VC_SERVER -User USERNAME -Password PASSWORD
    
    #VM Variables
    $vmpool = "Testing Pool"
    $vmname = "TestVM01
    $vmtemplate = "winxpsp3"
    $vmfolder = "DB-Provision"
    $datastore = "LAB_ESX_Celerra"
    $ip = "192.168.0.101"
    $subnetmask = "255.255.255.0"
    $gateway = "192.168.0.1"
    $dns = "192.168.0.20"
    $domain = "lab.local"
    
    ##
    $vm = Get-VM $name
    $cg = New-Object VMware.Vim.CustomizationGlobalIPSettings
    $cg.DnsServerList = $dns
    $cg.DnsSuffixList = $domain
    
    $cfi = New-Object VMware.Vim.CustomizationFixedIp
    $cfi.IpAddress = $ip
    
    $cis = New-Object VMware.Vim.CustomizationIPSettings
    $cis.DnsDomain = $domain
    $cis.DnsServerList = $dns
    $cis.Ip = $cfi
    $cis.Gateway = $gateway
    $cis.SubnetMask = $subnetmask
    
    $cm = New-Object VMware.Vim.CustomizationAdapterMapping
    $cm.Adapter = $cis
    
    $customspec = New-Object Vmware.Vim.CustomizationSpec
    $customspec.GlobalIPSettings = $cg
    
    $cust_name = New-Object Vmware.Vim.CustomizationFixedName
    $cust_name.Name = $vmname
    
    #Windows
    $cust_sysprep = New-Object Vmware.Vim.CustomizationSysPrep
    $cust_sysprep.GuiUnattended = New-Object VMware.Vim.CustomizationGuiUnattended
    $cust_sysprep.GuiUnattended.autoLogon = $true
    $cust_sysprep.GuiUnattended.autoLogonCount = "1"
    
    $cust_sysprep.GuiUnattended.Password = New-Object VMware.Vim.CustomizationPassword
    $cust_sysprep.GuiUnattended.Password.plainText = "True"
    $cust_sysprep.GuiUnattended.Password.value = "password"
    
    $cust_sysprep.UserData = New-Object VMware.Vim.CustomizationUserData
    $cust_sysprep.UserData.orgName = "Testing"
    $cust_sysprep.UserData.fullName = "Testing"
    $cust_sysprep.UserData.ProdID = "YOUR PRODUCT KEY HERE"
    
    
    $cust_sysprep.UserData.ComputerName = New-Object VMware.Vim.CustomizationFixedName
    $cust_sysprep.UserData.ComputerName.name = $vmname
    
    $cust_sysprep.Identification = New-Object VMware.Vim.CustomizationIdentification
    $cust_sysprep.Identification.JoinWorkgroup = "WORKGROUP"
    
    #$cust_sysprep.Options = New-Object VMware.Vim.CustomizationWinOptions
    #$cust_sysprep.Options.changeSID = 0
    
    $customspec.Identity = $cust_sysprep
    $customspec.NicSettingMap = $cm
    
    $vmv = Get-View $vm
    $vmv.CustomizeVM($customspec)
    
    

    Message was edited by: DougBaer -- I failed to mention that CustomizeVM only works when the VM is PoweredOff!



  • 5.  RE: Change VM ip address

    Posted Jul 04, 2009 10:21 PM

    Thanks for you reply. Going to test it next Monday.

    Have a great weekend :smileywink:



  • 6.  RE: Change VM ip address

    Posted Sep 08, 2009 09:33 AM

    Hi Dougbaer,

    Did you test your code on VI3 or on vSphere environment?

    Don't manage to get it working, do I miss something?

    I'm dealing with a SRM project and I have to set my IP address with a script (althoug SRM has an other solution, I know)

    My VM is turned of in this phase.

    Code starts but get an error back :Exception calling "CustomizeVM" with "1" argument(s): "Customization failed."

    All idea are welcome !!!

    Thanks

    Christof

    Script i'm testing:

    Modified script (two lines (bold))

    #VM Variables

    #$vmpool = "Testing Pool"

    $vmname = "w2k3-tst-srv07"

    #$vmtemplate = "winxpsp3"

    #$vmfolder = "DB-Provision"

    #$datastore = "LAB_ESX_Celerra"

    $ip = "192.168.0.101"

    $subnetmask = "255.255.255.0"

    $gateway = "192.168.0.1"

    $dns = "192.168.0.20"

    $domain = "lab.local"

    ##

    $vm = Get-VM $name

    $cg = New-Object VMware.Vim.CustomizationGlobalIPSettings

    $cg.DnsServerList = $dns

    $cg.DnsSuffixList = $domain

    $cfi = New-Object VMware.Vim.CustomizationFixedIp

    $cfi.IpAddress = $ip

    $cis = New-Object VMware.Vim.CustomizationIPSettings

    $cis.DnsDomain = $domain

    $cis.DnsServerList = $dns

    $cis.Ip = $cfi

    $cis.Gateway = $gateway

    $cis.SubnetMask = $subnetmask

    $cm = New-Object VMware.Vim.CustomizationAdapterMapping

    $cm.Adapter = $cis

    $customspec = New-Object Vmware.Vim.CustomizationSpec

    $customspec.GlobalIPSettings = $cg

    $cust_name = New-Object Vmware.Vim.CustomizationFixedName

    $cust_name.Name = $vmname

    #Windows

    $cust_sysprep = New-Object Vmware.Vim.CustomizationSysPrep

    $cust_sysprep.GuiUnattended = New-Object VMware.Vim.CustomizationGuiUnattended

    $cust_sysprep.GuiUnattended.autoLogon = $true

    $cust_sysprep.GuiUnattended.autoLogonCount = "1"

    $cust_sysprep.GuiUnattended.Password = New-Object VMware.Vim.CustomizationPassword

    $cust_sysprep.GuiUnattended.Password.plainText = "True"

    $cust_sysprep.GuiUnattended.Password.value = "password"

    $cust_sysprep.UserData = New-Object VMware.Vim.CustomizationUserData

    $cust_sysprep.UserData.orgName = "Testing"

    $cust_sysprep.UserData.fullName = "Testing"

    <code>#$cust_sysprep.UserData.ProdID = "YOUR PRODUCT KEY HERE"</code>*$cust_sysprep.UserData.ProductId= "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"*
    

    $cust_sysprep.UserData.ComputerName = New-Object VMware.Vim.CustomizationFixedName

    $cust_sysprep.UserData.ComputerName.name = $vmname

    $cust_sysprep.Identification = New-Object VMware.Vim.CustomizationIdentification

    $cust_sysprep.Identification.JoinWorkgroup = "WORKGROUP"

    #$cust_sysprep.Options = New-Object VMware.Vim.CustomizationWinOptions

    #$cust_sysprep.Options.changeSID = 0

    $customspec.Identity = $cust_sysprep

    $customspec.NicSettingMap = $cm

    $vmv = Get-View $vm.Id

    #$vmv = Get-View $vm

    $vmv.CustomizeVM($customspec)



  • 7.  RE: Change VM ip address

    Broadcom Employee
    Posted Sep 08, 2009 02:24 PM

    I was using vSphere all the way. Did you enter your product key in the customization spec? Note that this example only works with Windows VMs, and I was using a Windows 2003 OS.

    To me, this is a rather ugly to do it. I would look at using something like Invoke-VMscript from PowerCLI to change the IP address while the VM is online:

    $vm = Get-VM myVM
    $gc = Get-Credential 'administrator'
    $hc = Get-Credential 'root'
    $script = "ipconfig /all"
    
    #Change to static IP address
    $script = @"
    `$config = gwmi Win32_NetworkAdapterConfiguration
    `$adapter = `$config | where { `$_.IPEnabled -eq 'true' }
    `$adapter.EnableStatic('172.16.254.51','255.255.255.0')
    `$adapter.SetGateways('172.16.254.1')
    "@
    Invoke-VMScript -vm $vm -ScriptText $script -hostcredential $hc -guestcredential $gc
    
    #Revert all adapters to DHCP
    #$script = @"
    #`$config = gwmi Win32_NetworkAdapterConfiguration
    #`$adapter = `$config | where { `$_.IPEnabled -eq 'true' }
    #`$adapter.EnableDHCP()
    #"@
    #Invoke-VMScript -vm $vm -ScriptText $script -hostcredential $hc -guestcredential $gc
    

    You can get some additional information here



  • 8.  RE: Change VM ip address

    Posted Sep 09, 2009 11:06 AM

    Hi DougBaer,

    The problem had nothing to do with vSphere or VI3.5, the posted code apperantly missed a mandatory sysprep field (in my situation, anyway).

    I had to add these lines:

    $cust_sysprep.LicenseFilePrintData = New-Object VMware.Vim.CustomizationLicenseFilePrintData

    $cust_sysprep.LicenseFilePrintData.AutoMode = "perseat"

    Thanks any way for the great code !!

    Tested it out and works fine except for the part that it plays with the domain/workgroup, where I 'm only interessed in changing IP.

    So I follow you advise and go for launching a Powershell script in the VM that does the job

    Tx

    Christof

    The slightly modified code:

    $vm = Get-VM $name

    $cg = New-Object VMware.Vim.CustomizationGlobalIPSettings

    $cg.DnsServerList = $dns

    $cg.DnsSuffixList = $domain

    $cfi = New-Object VMware.Vim.CustomizationFixedIp

    $cfi.IpAddress = $ip

    $cis = New-Object VMware.Vim.CustomizationIPSettings

    $cis.DnsDomain = $domain

    $cis.DnsServerList = $dns

    $cis.Ip = $cfi

    $cis.Gateway = $gateway

    $cis.SubnetMask = $subnetmask

    $cm = New-Object VMware.Vim.CustomizationAdapterMapping

    $cm.Adapter = $cis

    $customspec = New-Object Vmware.Vim.CustomizationSpec

    $customspec.GlobalIPSettings = $cg

    $cust_name = New-Object Vmware.Vim.CustomizationFixedName

    $cust_name.Name = $vmname

    #Windows

    $cust_sysprep = New-Object Vmware.Vim.CustomizationSysPrep

    $cust_sysprep.GuiUnattended = New-Object VMware.Vim.CustomizationGuiUnattended

    #$cust_sysprep.GuiUnattended.autoLogon = $true

    #$cust_sysprep.GuiUnattended.autoLogonCount = "1"

    #$cust_sysprep.GuiUnattended.Password = New-Object VMware.Vim.CustomizationPassword

    #$cust_sysprep.GuiUnattended.Password.plainText = "True"

    #$cust_sysprep.GuiUnattended.Password.value = "password"

    #

    $cust_sysprep.UserData = New-Object VMware.Vim.CustomizationUserData

    $cust_sysprep.UserData.orgName = "Testing"

    $cust_sysprep.UserData.fullName = "Testing"

    #

    $cust_sysprep.UserData.ProductId= "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"

    $cust_sysprep.LicenseFilePrintData = New-Object VMware.Vim.CustomizationLicenseFilePrintData

    $cust_sysprep.LicenseFilePrintData.AutoMode = "perseat"

    $cust_sysprep.UserData.ComputerName = New-Object VMware.Vim.CustomizationFixedName

    $cust_sysprep.UserData.ComputerName.name = $vmname

    #

    $cust_sysprep.Identification = New-Object VMware.Vim.CustomizationIdentification

    $cust_sysprep.Identification.JoinWorkgroup = "WORKGROUP"

    #$cust_sysprep.Options = New-Object VMware.Vim.CustomizationWinOptions

    #$cust_sysprep.Options.changeSID = 0

    $customspec.Identity = $cust_sysprep

    $customspec.NicSettingMap = $cm

    $vmv = Get-View $vm.Id

    #$vmv = Get-View $vm

    $vmv.CustomizeVM($customspec)



  • 9.  RE: Change VM ip address

    Broadcom Employee
    Posted Sep 09, 2009 02:01 PM

    Thanks for the update. I feel your pain -- I know how much 'fun' it was to strip away the pieces of the Customization Spec to get to just the IP address part.

    I wonder if the requirements have somethign to do with the version of sysprep that gets kicked off, since I'm pretty sure I didn't have to set the AutoMode parameter.