VMware Cloud Community
sddunne
Contributor
Contributor
Jump to solution

How to script adding .VMX's from a Datastore (NFS) to Virtual Center

Hi Guys,

I'm hoping someone with a much better understanding of scripting than I will know of a simple way of doing this?

Basically, here is the scenario...

We use NFS Datastores running on Netapp Filers to house all of our Virtual Machines. This normally works great however we have had a few incidents recently with the filer which has meant us having to switch to our snapmirrored backups. Basically how this works is that each datastore, say for example Datastore1 normally runs on Filer1. This datastore is regulary 'snapmirrored' across to Filer2. this snapmirror is a readonly copy of the datastore. If we have a problem with Filer1, it is possible to reconfigure Filer2 to make the backup snapmirror writeable, effectively allowing us to see the vm's again.

Currently, if this happens, in order to get the Vm's back we need to remove all the vm's from Virtual Center Inventory, then add the snapmirrored datastore to all the hosts in Virtual Center, then Browse the datastore and manually add back all the vm's individually by doing the usual right click/add to inventory on each vm.

As i say we recently had an issue and had to invoke this for 10 datastores housing almost 250 vm's so as you can imagine was quite a slow and labour intensive task!!!

What i've been trying to work out is how i could script this in Power CLI? I've seen LuCD's excellent looking add .vmx script and looks very similar to what i'd like to do but can't work out how to modify it for my purposes.

What id' ideally love to get to would be a script that i can specify a Virtual Center instance, Datastore and Cluster as variables (say vcname$ = "ukvir0001" , cluster$ = "Datacentre1" and dsname$ = "datastore1") and then have the script read the datastore and add any .vmx's it finds to the specified Cluster.

I've been reading with interest the new Datastore Provider functionality in PowerCLI4 and that sounds like something that may provide an easy way of doing it i just can't work out how!

Any help would be very much appreciated as i have the daunting task of having to move almost 250 vm's back over to Filer1 now that the filer has been repaired and don't have the luxury of a several hour maintenance window to do it manually!

Many thanks,

Sean.

0 Kudos
33 Replies
beaunewcomb
Contributor
Contributor
Jump to solution

Could you provide a bit more info ?

What ESX/ESXi server version build ? How many datastores ?

Are you running the script while connected to the vCenter or a specific ESX/ESXi host ?

ESX 3.5 u4

2 NFS datastores

Connected to Virtual Center

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, could you run the following script and tell me what it returns

Before you run it enter the name of the cluster that contains the 2 ESX servers.

$cluster = <name-of-the-cluster>
$ESXname = Get-Cluster $cluster | Get-VMHost | select -First 1
$dsBrowser = Get-View (Get-View (Get-VMHost -Name $ESXname).ID).DatastoreBrowser

$dsBrowser.Datastore | %{Get-View $_} | Select Name

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
beaunewcomb
Contributor
Contributor
Jump to solution

Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At :line:6 char:21

+ $dsBrowser = Get-View <<<< (Get-View (Get-VMHost -Name $ESXname).ID).DatastoreBrowser

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks as if the DatastoreBrowser property in the HostSystem is not present.

Do you have the "administrator" role on Virtual Center and on that ESX host ?

To see the DatastoreBrowser property you need the System.View privilege.

Do you see a value for the DatastoreBrowser property when you execute this

$cluster = <name-of-the-cluster>
$ESXname = Get-Cluster $cluster | Get-VMHost | select -First 1
Get-View (Get-VMHost -Name $ESXname).ID

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
beaunewcomb
Contributor
Contributor
Jump to solution

DatastoreBrowser : HostDatastoreBrowser-datastorebrowser-44749

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And when you do

Get-View "HostDatastoreBrowser-datastorebrowser-44749"
Get-View "HostDatastoreBrowser-datastorebrowser-44749" | select -ExpandProperty Datastore

Does this return anything ?

And for the second line you should only see 2 lines of output.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
beaunewcomb
Contributor
Contributor
Jump to solution

When I run this:

$cluster = "TRU-HA-3"

$ESXname = Get-Cluster $cluster | Get-VMHost | select -First 1

#Get-View (Get-VMHost -Name $ESXname).ID

Get-View "HostDatastoreBrowser-datastorebrowser-44749"

Get-View "HostDatastoreBrowser-datastorebrowser-44749" | select -ExpandProperty Datastore

I get this:

Datastore SupportedType MoRef Client

-


-


-


-


{Datastore-datastore-44720, ... {VMware.Vim.VmConfigFileQuer... HostDatastoreBrowser-datast... VMware.Vim.VimClient

{Datastore-datastore-44720, ... {VMware.Vim.VmConfigFileQuer... HostDatastoreBrowser-datast... VMware.Vim.VimClient

0 Kudos
beaunewcomb
Contributor
Contributor
Jump to solution

I just ran them separately. When I run the 1st, I get the above, when i run the 2nd line, I get:

Type Value

-


-


Datastore datastore-44720

Datastore datastore-44722

Datastore datastore-44724

Datastore datastore-44726

Datastore datastore-44728

Datastore datastore-44730

Datastore datastore-44732

Datastore datastore-44734

Datastore datastore-44736

Datastore datastore-44738

Datastore datastore-44800

Datastore datastore-44787

Datastore datastore-44789

Datastore datastore-44793

Datastore datastore-44791

Datastore datastore-9356

Datastore datastore-10673

Datastore datastore-9360

Datastore datastore-9362

Datastore datastore-9364

Datastore datastore-44802

Datastore datastore-9344

Datastore datastore-9346

Datastore datastore-9348

Datastore datastore-9350

Datastore datastore-9352

Datastore datastore-44750

Datastore datastore-44720

Datastore datastore-44722

Datastore datastore-44724

Datastore datastore-44726

Datastore datastore-44728

Datastore datastore-44730

Datastore datastore-44732

Datastore datastore-44734

Datastore datastore-44736

Datastore datastore-44738

Datastore datastore-44800

Datastore datastore-44787

Datastore datastore-44789

Datastore datastore-44793

Datastore datastore-44791

Datastore datastore-9356

Datastore datastore-10673

Datastore datastore-9360

Datastore datastore-9362

Datastore datastore-9364

Datastore datastore-44802

Datastore datastore-9344

Datastore datastore-9346

Datastore datastore-9348

Datastore datastore-9350

Datastore datastore-9352

Datastore datastore-44750

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And finally (no need to add the other lines)

(Get-View "HostDatastoreBrowser-datastorebrowser-44749").Datastore | %{Get-View $_} | select Name

If you now see the names of your 2 datastores then I don't understand why the script didn't work.

Since we're at the line where you got the error message.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You actually have that many datastores ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
beaunewcomb
Contributor
Contributor
Jump to solution

Very strange-

Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At :line:9 char:79

+ (Get-View "HostDatastoreBrowser-datastorebrowser-44749").Datastore | %{Get-View <<<< $_} | select Name

Yeah, we have our OS vmx's and vmdks split from the OS swaps, plus separate volumes for vswaps.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What I suspected.

There seems to be 1 "ghost" entry in the list of datastores.

Since the MoRef isn't valid the Get-View cmdlet gives this error.

Now to find out which one ...

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
beaunewcomb
Contributor
Contributor
Jump to solution

Thanks a lot for your help LucD

How does a ghost entry appear, and how do we find which one it is?

Beau

0 Kudos
IRCScott
Contributor
Contributor
Jump to solution

  • Edited the script in notepad+
  • In the top section put our servers, clusters, etc within quotes (ie.... "MyVcenter" and "MyDatastoreName"
  • Ran the script using PowerCLi
  • It paused at the Vcenter setup with a certificate warning whcih made me very nervous but it eventually continued on.
  • Suddenly VMs started appearing in vCenter as raw storage names and then they magically renamed themselves to the correct names.

BEAUTIFUL!

Absolutely a life saver!!!!

0 Kudos