VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

registering vms to esxi host _powercli

Hi Luc,

i need to register vms which are shown by below code with corresponding datastore .can you please complete below code .

$csv_info = Import-Csv c:\servers.csv

foreach ($line in $csv_info) {

$x=get-vm $line.vmname

$datastore=get-vm $x|get-datastore

[pscUSTOMoBJECT]@{

                vmname = $x.name

                datastorename = $datastore.name

               

}

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Or you could read my first reply correctly, which was based on the original code you posted.
$datastore and $x were the full objects, not just the Name property.


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

View solution in original post

14 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not getting the question I'm afraid.

You want to register a VM, but the VM seems to be already registered (judging from the Get-VM you are doing).

To register an orphaned VM you will need the path to the .VMX file.

Something like this for example

New-Vm -DiskPath "[$($datastore.Name)] $($x.Name)/$($x.Name).vmx"


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

if yu can check the below scenerio.

one host in non responding state in vcenter .

tech support asked to generate core dump .

so we need to shut down vm and register to other hosts(host in vcenter )

and then generate core dump .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Still confused, you can vMotion a VM to another ESXi node.
Registering a VM is done on the vCenter afaik.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

host is not responding in vcenter so drs and vmotion wont work .

i was thinking of HA but that will restart vms non gracefully.

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i think below script should work .it will remove from inventory and register to new host .

$csv_info = Import-Csv c:\servers.csv

$dlist= @()

foreach ($line in $csv_info) {

$x=get-vm $line.vmname

$datastore=get-vm $x|get-datastore

$D = $datastore.name

$dlist += $D

remove-vm  $x

New-Vm -DiskPath "[$($datastore.Name)] $($x.Name)/$($x.Name).vmx" -VMHost "xyz"

[pscUSTOMoBJECT]@{

                vmname = $x.name

                datastorename = $datastore.name

               

}

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure that will work when that host is not responding.
Or are you moving away from the non-responsive host?


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

so what we will do is to shutdown all vms gracefully through rdp

then unregister it from vcenter as they are added to vcenter

we will add them back to responsive host .so moving away from non responsive host ...

if yu can validate in yur lab the script which i sent ..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should work, provided you do the Get-VM before you unregister the VM.

But you could just as well do a Move-VM after the VM is powered off (provided there is no lock on any of the VM's files).

This is hard to simulate in a lab.
A powered-off ESXi node is not the same as a non-responsive ESXi node which still might hold locks on the VM


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

jvm2016
Hot Shot
Hot Shot
Jump to solution

That should work, provided you do the Get-VM before you unregister the VM.

thats why i put remove-vm after get-vm .

i supect lock things but once vm is powered down lock will be realesed i belive .

I HATE LOCKS AND LOCKDOWN :smileycry:

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

iam checking this in lab and new-vm part is not working in below code

$csv_info = Import-Csv c:\servers.csv

$dlist= @()

foreach ($line in $csv_info) {

$x=get-vm $line.vmname

$datastore=get-vm $x|get-datastore

$D = $datastore.name

$dlist += $D

remove-vm  $x

New-Vm -DiskPath "[$($datastore.Name)] $($x.Name)/$($x.Name).vmx" -VMHost "xyz"

[pscUSTOMoBJECT]@{

                vmname = $x.name

                datastorename = $datastore.name

               

}

}

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is that a VSAN datastore?

What does the following say (while the VM is still registered)?

Get-VM | Select Name,

    @{N='VMX';E={$_.ExtensionData.Summary.Config.VmPathName}}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i figured it out

following worked .it seems i m still able to think in corona pandemic:smileylaugh:

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Or you could read my first reply correctly, which was based on the original code you posted.
$datastore and $x were the full objects, not just the Name property.


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

jvm2016
Hot Shot
Hot Shot
Jump to solution

oh i missed it thanks Luc sir.

Reply
0 Kudos