VMware Cloud Community
Keuka
Contributor
Contributor
Jump to solution

Script Help: VM --> Datastore Associations

Hi Everyone,

Our VM names typically end with a number (i.e. WEB1, WEB2, etc.) and our datastore names also end with a number. For application redundancy, we use datastores with names ending in even numbers for the storage needed by VMs with names ending in even numbers and datastores with names ending in odd numbers for the storage needed by VMs with names ending in odd numbers. In the physical world, the odd numbered datastores and hosts are physically separate from the even ones.

I've been trying to create a PowerCLI script to report on any mismatches (people do make mistakes) so that I can know if any VM has storage on the "wrong" kind of datastore (even VM on odd storage or vice-versa).

I'm really green with PowerShell and PowerCLI and suspect that there's a way to use the Get-View -ViewType VirtualMachine but have been unsuccessful thus far.

In my environment most VMs use more than one datastore, so the script needs to check every datastore used by each VM for a mismatch and I want to run the report against my whole environment (single VC server).

If anyone knows of something similar that's already been done or can point me in the right direction, I'd really appreciate it.

-Matt

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The attached script uses a regex to extract the number of the guestname and the datastorename.

And then it uses the modulo of the division by 2 to check if it is an odd (1) or even (0) number.

When you compare these between the guest and the datastore you will find the discrepancies.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

The attached script uses a regex to extract the number of the guestname and the datastorename.

And then it uses the modulo of the division by 2 to check if it is an odd (1) or even (0) number.

When you compare these between the guest and the datastore you will find the discrepancies.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Keuka
Contributor
Contributor
Jump to solution

You're awesome... thanks!!

0 Kudos
Keuka
Contributor
Contributor
Jump to solution

As a follow-up, how would I restrict this to a specified datacenter?

0 Kudos
Keuka
Contributor
Contributor
Jump to solution

Simple... I keep trying to make things more complex than they are.

foreach($vm in Get-Datacenter "my datacenter name here" | Get-VM -Name "*"){

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's it, good you found it Smiley Happy

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos