VMware Cloud Community
Pilu1978
Enthusiast
Enthusiast

How to seacrh a particular vmx file in datastores of a cluster

Hi,

I have requirement to search a particulat vmx file in all the datastores in a cluster. if found then it will check If the vmx file is registered if not then it will register the vmx file in any of the esx of the cluster. The name of the vmx file is like <servername.vmx. So if the server name is s001 then the vmx files name will be s001.vmx

Need a script which can accomplish the above scenario.

Thanks in advance.

0 Kudos
26 Replies
LucD
Leadership
Leadership

Did you already look at my Raiders of the Lost VMX post ?


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

I have already seen the script on the internet but sorry I could not understand it  properly. If you can explain then it would be greate help.

0 Kudos
LucD
Leadership
Leadership

Ok, save the function in a .ps1 file.

From the PowerCLI prompt you connect to the vCenter (Connect-VIServer).

You make the function known to the PowerShell engine by dot-sourcing the .ps1 file

. ./TheFile.ps1

That is a dot, followed by a blank and then the relative path to the .ps1 file

Now you can call the function from the PowerCLI prompt.

In your case, you wanted to search 1 or more datastores, you could do

Register-VMX -dsNames “datastore1″,”datastore2″,”datastore3″ -WhatIf

Notice the -WhatIf parameter, this will allow you to first check what the script will do, without actually performing the action.

Once you're happy with the result, you leave out the -WhatIf parameter, and let the function register the orphaned .vmx files.

Register-VMX -dsNames “datastore1″,”datastore2″,”datastore3″


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Thanks a lot for your explanation. But my requirement is slight different. The script fetch the vm name from a csv file and search for the vmx file (vmname.vmx) one at a time in all the datastore of a cluster and if it finds the vmx file for that it will  check if the vm is not registered if not then it will add the vm into the inventory.

Is it possble?

0 Kudos
LucD
Leadership
Leadership

Everything is 'poshibale' Smiley Wink

Try the following flow

$clusterName = "MyCluster" 
$dsNames
= Get-VMHost -Location (Get-Cluster -Name $clusterName) | `
   
Select -First 1 | Get-Datastore | `
    where {$_.Extensiondata.Summary.MultipleHostAccess} | %{$_.Name}     Import-Csv "C:\vmnames.csv" -UseCulture | %{     if(!(Get-VM -Name $_.vmname -ErrorAction SilentlyContinue)){         Register-VMX -dsNames $dsNames     } }

The script reads names from a CSV file (the column should be named vmname), then it checks if the VM exists (i.e. is registered).

If not, it uses my Register-VMX function to look on the shared datastores from the cluster to find the .vmx file.

If the .vmx file is found, the Register-VMX function will register the VM.


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

0 Kudos
Andavan
Contributor
Contributor

Hi LucD,

Register-VMX function is not working. I am gettting error

Senthil

0 Kudos
LucD
Leadership
Leadership

Which error ?


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

0 Kudos
Andavan
Contributor
Contributor

Error is " The term 'Register-VMX' is not recognized as the name of a cmdlet, function, script file, or operable program"

0 Kudos
LucD
Leadership
Leadership

That seems to indicate you didn't dot-source the .ps1 file that includes the Register-VMX function.


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

0 Kudos
Andavan
Contributor
Contributor

LucD,

It worked. Actual issue was source file name.

Thanks for your assistance.

0 Kudos
Andavan
Contributor
Contributor

LucD,

Again I am getting same error. yeterday i was able to run but today it is not working..

0 Kudos
LucD
Leadership
Leadership

That means again that the function is not found.

How did you include the function, by dot-sourcing or by including it in the .ps1 file ?


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

0 Kudos
Andavan
Contributor
Contributor

I am running the command  . .\regvm.ps1


I am getting same error.

0 Kudos
LucD
Leadership
Leadership

But what do you have in regvm.ps1 ?

The function and the call to the function ?


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

0 Kudos
Andavan
Contributor
Contributor

Attached my script here

0 Kudos
LucD
Leadership
Leadership

What I thought, you don't have the function in there.

The easiest way to do this, copy the function from my Raiders of the Lost VMX post, and paste it in your .ps1 file (at the beginning).


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

0 Kudos
Andavan
Contributor
Contributor

LucD,

Need to copy complete script from "Raiders of the lost VMX" post?

Thanks

0 Kudos
LucD
Leadership
Leadership

Correct, it needs to be known to the PowerShell engine before you call the function.

You can just include it in your .ps1 file before the code from above.


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

0 Kudos
Andavan
Contributor
Contributor

LucD,

I have included the function but it is trying to register all VM's located in all LUN's. it is not calling the input file and register only VM's in the input file.

attached my script.

my requirement is get the VMX file name from input file (csv file) and get register those virtual mahcines in specific cluster. help me on this..

Thanks

Senthil.

0 Kudos