VMware Cloud Community
shinpad
Contributor
Contributor
Jump to solution

Script for Stretched Clusters

I'm implementing stretched clusters and would like to know if there is a way to check VMs against the host they are running on and the datastore they are in?

at site 1, all hosts are named xxxx-Axxx, VMs are named A-xxxxxx and datastores are named A-xxxxxx

at site 2, all hosts are named xxxx-Bxxx, VMs are named B-xxxxxx and datastores are named B-xxxxxx

is it possible to check that the VMs named A-xxx are runing on hosts named xxxx-Axx and from datastores named A-xxxxxx, and that that the VMs named B-xxx are runing on hosts named xxxx-Bxx and from datastores named B-xxxxxx?

thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can add a filter using the Where-Object cmdlet:

Get-VM |
Sort-Object -Property Name |
Select-Object -Property Name,VMHost,
@{Name="Datastore";Expression={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')}} |
Where-Object {($_.Name -like "*-A*" -and ($_.VMHost -notlike "*-A*" -or $_.Datastore -notlike "A-*")) -or
  ($_.Name -like "*-B*" -and ($_.VMHost -notlike "*-B*" -or $_.Datastore -notlike "B-*"))}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
4 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following PowerCLI script will return a list of all VM's, the hosts they are running on and the datastores where their vmx file is located:

Get-VM |
Sort-Object -Property Name |
Select-Object -Property Name,VMHost,
@{Name="Datastore";Expression={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')}}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

thanks for the reply.

I got as far as that, but i was hoping to be able to check that the VM named A-xxxxx is running on a host called xxxx-Axxxx and is located in a datastore called A-xxxxx and only if those rules don't match then alert me.

any idea if that can be done?

thanks

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can add a filter using the Where-Object cmdlet:

Get-VM |
Sort-Object -Property Name |
Select-Object -Property Name,VMHost,
@{Name="Datastore";Expression={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')}} |
Where-Object {($_.Name -like "*-A*" -and ($_.VMHost -notlike "*-A*" -or $_.Datastore -notlike "A-*")) -or
  ($_.Name -like "*-B*" -and ($_.VMHost -notlike "*-B*" -or $_.Datastore -notlike "B-*"))}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

that is excellent. thank you very much.

only needed one small change and ended up with this:

$body = Get-VM |
Sort-Object -Property Name |
Select-Object -Property Name,VMHost,
@{Name="Datastore";Expression={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')}} |
Where-Object {($_.Name -like "A-*" -and ($_.VMHost -notlike "*-A*" -or $_.Datastore -notlike "A-*")) -or ($_.Name -like "B-*" -and ($_.VMHost -notlike "*-B*" -or $_.Datastore -notlike "B-*"))}

thanks again.

Reply
0 Kudos