VMware Cloud Community
layer100
Contributor
Contributor

PowerCLI New-VM: specify IP/Hostname (OSCustomizationSpec?)

Hello all,

I want to change the IP/Hostname while cloning a VM using PowerCLI. I tried to use the OSCustomizationSpec parameter of the New-VM cmdlet:

New-VM -Name $cloneName -VMHost $targetVmHost -VM $sourceVM -ResourcePool $rp -Location $targetLocation -Datastore $targetDatastore -OSCustomizationSpec (get-oscustomizationspec $customizationTemplate)

This command works well without the OSCustomizationSpec parameter, but this way it results in an error:

New-VM : Unable to cast object of type 'VMware.Vim.CustomizationSpecItem' to type 'VMware.Vim.CustomizationSpec'.+ At line:1 char:7 ++ New-VM <<<< -Name $cloneName -VMHost $targetVmHost -VM $sourceVM -ResourcePool $rp -Location $targetLocation -Datastore $targetDatastore -OSCustomizationSpec (+ get-oscustomizationspec $customizationTemplate) ++ CategoryInfo : NotSpecified: (Smiley Happy , InvalidCastException+ ++ FullyQualifiedErrorId : System.InvalidCastException,VMware.VimAutomation.Commands.NewVM

Does someone has an idea how I can avoid this error? Maybe someone could help me in general, how can I change the IP and Hostname of the new VM?

Any help is really appreciated! Thanks a lot!

0 Kudos
7 Replies
LucD
Leadership
Leadership

Are you running the latest PowerCLI build ?

Does

Get-PowerCLIversion

say build 208462 ?

An alternative to change the IP address is the Set-VMGuestNetworkInterface cmdlet.

To change the hostname you can use the Invoke-VMScript cmdlet.

But it depends if the computer is joined to a domain or not on which command you have to use in the script.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ykalchev
VMware Employee
VMware Employee

Hi,

Can you give us some details on the error?

$error[0].Exception | select *
$error[0].Exception.Stacktrace

Thanks,

Yasen

Yasen Kalchev, vSM Dev Team
0 Kudos
lazeru
Contributor
Contributor

Thanks for your suggestions!

Yes, I am running the latest version of PowerCLI:

PowerCLI Version

-


VMware vSphere PowerCLI 4.0 U1 build 208462

Am I right, that the Set-VMGuestNetworkInterface only works on powered on Windows VMs? I also need to configure Linux machines, that's why I tried to use the OSCustomizationSpec..

The cloned VMs are not assigned to a domain.

0 Kudos
lazeru
Contributor
Contributor

This is the detailed error output:

C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $error[0].Exception | select *

Message : Unable to cast object of type 'VMware.Vim.CustomizationSpecItem' to type 'VMware.Vim.CustomizationSpec'.

Data :

InnerException :

TargetSite : VMware.Vim.CustomizationSpec GetSpecItem(VMware.VimAutomation.Types.OSCustomizationSpec)

StackTrace : at VMware.VimAutomation.Client20.VmServiceImpl.GetSpecItem(OSCustomizationSpec spec)

at VMware.VimAutomation.Client20.VmServiceImpl.CloneVm(String name, String description, VMHost host, ResourcePool pool, VApp vApp, Folder locati

on, Datastore datastore, Nullable`1 diskStorageFormat, OSCustomizationSpec spec, Boolean runAsync, VirtualMachine vm)

at VMware.VimAutomation.Commands.NewVM.DoWork(VIAutomation client, List`1 moList)

at VMware.VimAutomation.BaseCmdlet.DoWorkEx(VIAutomation client, List`1 moList)

at VMware.VimAutomation.Util.ClientVisitor`1.Visit(T[] objs, Worker worker, ErrorCallback errorCallback)

at VMware.VimAutomation.BaseCmdlet.ProcessRecordCore()

at VMware.VimAutomation.BaseCmdlet.ProcessRecord()

at System.Management.Automation.Cmdlet.DoProcessRecord()

at System.Management.Automation.CommandProcessor.ProcessRecord()

HelpLink :

Source : VMware.VimAutomation.Client20

C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $error[0].Exception.Stacktrace

at VMware.VimAutomation.Client20.VmServiceImpl.GetSpecItem(OSCustomizationSpec spec)

at VMware.VimAutomation.Client20.VmServiceImpl.CloneVm(String name, String description, VMHost host, ResourcePool pool, VApp vApp, Folder location, Datastore datastore, Nullable`1 diskStorageFormat, OSCustomizationSpec spec, Boolean runAsync, VirtualMachine vm)

at VMware.VimAutomation.Commands.NewVM.DoWork(VIAutomation client, List`1 moList)

at VMware.VimAutomation.BaseCmdlet.DoWorkEx(VIAutomation client, List`1 moList)

at VMware.VimAutomation.Util.ClientVisitor`1.Visit(T[] objs, Worker worker, ErrorCallback errorCallback)

at VMware.VimAutomation.BaseCmdlet.ProcessRecordCore()

at VMware.VimAutomation.BaseCmdlet.ProcessRecord()

at System.Management.Automation.Cmdlet.DoProcessRecord()

at System.Management.Automation.CommandProcessor.ProcessRecord()

0 Kudos
ykalchev
VMware Employee
VMware Employee

Thanks for the details.

It turns out that it's a bug in PowerCLI 4.0 U1 and it's already fixed for the next release. The workaround is to create the new VM and then pass it to Set-VM cmdlet and apply the customization spec.

New-VM -Name $cloneName -VMHost $targetVmHost -VM $sourceVM -ResourcePool $rp -Location $targetLocation -Datastore $targetDatastore | Set-VM -OSCustomizationSpec $customizationTemplate

Regards,

Yasen

Yasen Kalchev, vSM Dev Team
0 Kudos
lazeru
Contributor
Contributor

Thanks a lot, that workaround helped me out using CustomizationSpecs!

But now I tried to modify the specification to define the hostname of the clone:

$spec = Get-OSCustomizationSpec $customizationTemplate
$spec.NamingScheme = "Fixed"
$spec.NamingPrefix = "NewHostname"
Get-VM $cloneName | set-vm -OSCustomizationSpec $spec -Confirm:$false

I am getting an error that a specified parameter was not correct:

hostname.

Set-VM : 30.06.2010 14:26:51    Set-VM    BE98CC6B-E9A7-4F85-A960-91491B051AAE    The operation for the entity vm-6743 failed with the following message: "A specified parameter was not correct.
hostname"
At line:1 char:27
+ Get-VM $cloneName | set-vm <<<<  -OSCustomizationSpec $spec -Confirm:$false
+ CategoryInfo          : InvalidArgument: (vm-6743:String) [Set-VM], InvalidArgumentException
+ FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.Commands.SetVM

0 Kudos
admin
Immortal
Immortal

Hi ,

Usually PowerCLI objects are read-only, e.g. it is not possible to change their properties directly. Obviously the customization spec object allows to edit its properties but this does not change the server object which lives on the VC server.

The correct way is to use Set-OSCustomizationSpec to change any of its properties (like any other Set-* commandlet) :

$spec = $spec | Set-OSustomizationSpec -NamingPrefix .... -NamingScheme .....

I will file a bug for this issue.

Vitali

0 Kudos