VMware Communities > VMTN > VMware vSphere™ > Automation Tools > vSphere™ PowerCLI > Discussions

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
8 Replies Last post: Sep 9, 2009 7:00 AM by DougBaer
Reply

Change VM ip address

Jul 4, 2009 11:13 AM

Click to view ICT-Freak's profile Enthusiast ICT-Freak 61 posts since
Dec 28, 2006

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 )

Attachments:
Reply Re: Change VM ip address Jul 4, 2009 11:41 AM
Click to view DougBaer's profile Expert DougBaer 600 posts since
Oct 20, 2004
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 :)

$vmcloneSpec.Customization.Identity = New-Object VMware.Vim.CustomizationSysprep
Reply Re: Change VM ip address Jul 4, 2009 1:07 PM
in response to: DougBaer
Click to view DougBaer's profile Expert DougBaer 600 posts since
Oct 20, 2004
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.
Reply Re: Change VM ip address Jul 4, 2009 2:41 PM
in response to: DougBaer
Click to view DougBaer's profile Expert DougBaer 600 posts since
Oct 20, 2004
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!
Reply Re: Change VM ip address Jul 4, 2009 3:20 PM
in response to: DougBaer
Click to view ICT-Freak's profile Enthusiast ICT-Freak 61 posts since
Dec 28, 2006

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

Have a great weekend ;-)

Reply Re: Change VM ip address Sep 8, 2009 2:32 AM
in response to: DougBaer
Click to view bartvp's profile Lurker bartvp 1 posts since
Aug 21, 2007

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)

Attachments:
Reply Re: Change VM ip address Sep 8, 2009 7:26 AM
in response to: bartvp
Click to view DougBaer's profile Expert DougBaer 600 posts since
Oct 20, 2004
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
Reply Re: Change VM ip address Sep 9, 2009 4:06 AM
in response to: DougBaer
Click to view ugauchr's profile Novice ugauchr 6 posts since
Jan 10, 2007

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)


Reply Re: Change VM ip address Sep 9, 2009 7:00 AM
in response to: ugauchr
Click to view DougBaer's profile Expert DougBaer 600 posts since
Oct 20, 2004
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.
Actions