VMware Cloud Community
Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

Find Unregistered VMs and Register

HI all

I'm trying to register back all unregistered VMs with a specific name convention 

and back to each host the VMX was before, also the name of the VMX suggests the name of the Hosts

I'm using LucD's script to find all the unregistered.

This:

$Datastores = 'DS1','DS2'

foreach($Datastore in Get-Datastore $Datastores) {

    # Collect .vmx paths of registered VMs on the datastore

    $registered = @{}

    Get-VM -Datastore $Datastore | %{$registered.Add($_.ExtensionData.Summary.Config.VmPathName.Split('/')[-1],$true)}

    # Set up Search for .VMX Files in Datastore

    New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

    $unregistered = @(Get-ChildItem -Path TgtDS: -Filter *.vmx -Recurse |

        where {$_.FolderPath -notmatch ".snapshot" -and !$registered.ContainsKey($_.Name)})

    Remove-PSDrive -Name TgtDS

}

$unregistered | Select Name,FolderPath

 

after this script finds all the unregistered, I'm trying to figure out how to register them back 

the name convention I mentioned are as follow:
Host names are like so: (for example)
10.10.10.1 , 10.10.10.2 , 10.10.10.3 ... etc

VMs names are like so: (for example)

VM-10.10.10.1 , VM-10.10.10.2 , VM-10.10.10.3 ... etc

my question is how can I find the VMX by this names, and to register back each VM to its host

for example: VM-10.10.10.1 - register back to host 10.10.10.1 and so on

 

Thanks for any help!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you already try like this?

$unregistered | 
ForEach-Object -Process {
    New-VM -VMFilePath $_.FolderPath -VMHost ($_.Name.Split('-')[1]) -Confirm:$false
}


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

View solution in original post

12 Replies
LucD
Leadership
Leadership
Jump to solution

Did you already try like this?

$unregistered | 
ForEach-Object -Process {
    New-VM -VMFilePath $_.FolderPath -VMHost ($_.Name.Split('-')[1]) -Confirm:$false
}


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

Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

I do now

but for some reason, now I can't find any unregistered VMs at all.

and there are many of them in this env...

I'm trying to figure out what is wrong with either the script or the VC that it does not finding any unregistered VMs

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is nothing in the $unregistered variable?

Can you test with a new VM, unregister it and then run the script again?


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

Reply
0 Kudos
Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

I tried at first in same ENV to install new VM and then unregister it, run the script again same result didnt work.

then I tried the finding script on other env with unregistered VMs, and it did work.

then I added 

$unregistered |
ForEach-Object -Process {
New-VM -VMFilePath $_.FolderPath -VMHost ($_.Name.Split('-')[1]) -Confirm:$false
}

as suggested, and got an error:

New-VM Please specify at least one of the following parameters: "ResourcePool" or "VMHost".
At line:33 char:5
+ New-VM -VMFilePath $_.FolderPath -VMHost ($_.Name.Split('-')[1]) ...

am I doing something wrong with the path and VMHost?

because it does finds the VMFilePath when I'm just finding the unregistered VMs

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It's on the VMHost parameter that there seems to be an error.
Is the name of the VM perhaps not something like VM-10.1.1.1?


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

Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

Yes it is something like VM-10.1.1.1

also I've tried different vmfilepath like this:

New-VM -VMFilePath $_.DatastoreFullPath -VMHost ($_.Name.Split('-')[1]) -Confirm:$false

and now it did work.

but I got few more questions please.

how could I split the name few times?

for example if the ENV got VMs with not very convenient names like - 'Z-VM-10.1.1.1-01234'

and 'Z-VM-10.1.1.1'

how complicated will that be to split this kind of name, to get the IP of '10.1.1.1' and register this VM to host 10.1.1.1 ? 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There have to be some naming conventions to be able to programmatically extract the VMHost.
If the names are all like Z-VM-10.1.1.1, the split would be

-VMHost ($_.Name.Split('-')[2])

 


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

Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

and yet another question\error

now I'm getting:

New-VM Could not find VMHost with name '10.1.1.1.vmx'.
At line:28 char:5
+ New-VM -VMFilePath $_.FolderPath -VMHost ($_.Name.Split('-')[1]) ...

I'm guessing for the VMHost there is a need to split the '.vmx' part as well? so it will get only the numbers from the name?

if so, how would it be done?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, try with this

-VMHost ($_.Name.Split('-')[1].Split('.')[0]) 


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

Reply
0 Kudos
Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

I'm kind of struggling with this one

with that its either gets the first digit '10' with 

($_.Name.Split('-')[1].Split('.')[0]) 

 

or if I try to play with it and do 

($_.Name.Split('-')[1].Split('.')[3]) 

its getting the last digit '1' from '10.1.1.1'

it never gets the full 'IP' number and only parts of the array.

and idea?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Oops, I forgot your using the IP address in there.
As an alternative you could do

($_.Name.Split('-')[1].TrimEnd('.vmx'))


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

Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

Thank you so much LucD !!
you are a life saver ! 

you so fast and accurate in your responses.

Reply
0 Kudos