MarkusLanderti1
VMware Employee
VMware Employee

Script to check if VMs run on correct Datastore(Cluster) / Checking VM Datastore Cluster Relationship

Dear All,


I am looking for a Script that checks if VM's are running on the correct Datastore.Architecture:

  • 1 VCSA
  • 2 Hosts, each on 1 Site
  • 2 Storage Arrays, each on 1 Site

pastedImage_0.png

So the Requirement is, that 'VM' has to reside on the Datastorecluster of it's 'Home Site'.

No automation needed so far, just a Script that:

- Lists all VM's with the Datastore Cluster on which it's VMDK's are residing in (Datastorename would be sufficient too I guess)

- Writes the List out to a CSV File

- In an ideal World: Adds a * (Star) or some mark to identify the Lines where the placing doesn't match for easy identifying.

What I got so far:

$vcenter = “VCENTER FQDN”

$clustername = “TARGET CLUSTER NAME”

if ($global:DefaultVIServer){

      Write-Host “1. Terminating existing vCenter Connections...” -ForeGroundColor Red

      Disconnect-ViServer -Server * -Confirm:$False -Force}

      Write-Host “2. Connecting to vCenter: [$vcenter]” -ForegroundColor Yellow

      $connection = Connect-VIServer $vcenter

      if($connection.isconnected -eq “true”){

      Write-Host “Connection to [$vcenter] successfully established” -ForegroundColor Green

          Sleep 2}

      else

      {Write-Host “Error while connecting to vCenter Server! Please check your credentials and target IP/FQDN” -ForegroundColor Red

      Sleep 5

      exit}

INSERT USEFUL LOOPS HERE Smiley Happy

Write-Host “3. Writing out the Data...” -ForegroundColor Green

Export-Csv C:\vm-datastore-report.csv -NoTypeInformation -UseCulture

So what I'm struggling with:

- Listing all VM's of Host1 and the DS Cluster of it's VMDK's.

- Listing all VM's of Host2 and the DS Cluster of it's VMDK's

- Marking any Line which voids that 1:1 relation with some Charakter

BTW: Do you have suggestions for a good GUI Powershell Editor? I know there are some Editors where you can put together such Commands like I need, but I don't know which ones are recommended, since I just get deeper into the Scripting Story.

Thanks in advance for your help,

Kind regards,

Markus

0 Kudos
7 Replies
LucD
Leadership
Leadership

How are Host1-DSC1 and Host2-DSC2 defined?
Will that be hard-coded in the script, or is there a file somewhere that contains that info?

At the moment the popular IDE for PowerShell is Visual Studio Code.

See Using Visual Studio Code for PowerShell Development

It's free and multi-platform.


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

0 Kudos
MarkusLanderti1
VMware Employee
VMware Employee

Hi LucD,

We can define it at the Top of the Script as a Variable.

Thanks for the Tip with the IDE Smiley Happy

0 Kudos
LucD
Leadership
Leadership

Try something like this.

The desired configuration is defined in $configData, an array of objects, each element containing the name of the ESXi node and the datastoreclustername.

$configData = @(

   @{

   VMHost = 'host1'

   DSC = 'DSC1'

   },

   @{

   VMHost = 'host2'

   DSC = 'DSC2'

   }

)


Get-VM -PipelineVariable vm |

ForEach-Object -Process {

   $correct = ''

   $dsc = Get-Datastorecluster -VM $vm

   $config = $configData | where{$_.VMHost -eq $vm.VMHost.Name}

   if($dsc.Name -ne $config.DSC){

     $correct = '*'

   }

   $vm | Select Name,@{N='VMHost';E={$vm.VMHost.Name}},

     @{N='DSC';E={$dsc.Name}},

     @{n='Correct';E={$correct}}

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
MarkusLanderti1
VMware Employee
VMware Employee

Thank you so much, Powershell God :smileylaugh:

I will test it and give Feedback.

Cheers.

0 Kudos
MarkusLanderti1
VMware Employee
VMware Employee

Dear LucD:

The filter in the script seems not to work correctly.

In the results, I get all VM's on any Datastore, not only the one's that we defined in the Hosts variables.

Can we exclude thos VM's that are not running on the 2 hosts ?

Thanks so much !!

0 Kudos
LucD
Leadership
Leadership

Can you try by changing the Get-VM line to

Get-VMHost -Name host1,host2 | Get-VM -PipelineVariable vm |


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

0 Kudos
MarkusLanderti1
VMware Employee
VMware Employee

Here the final Script

There were a few bracket issues and also some " had to be replaced with '.

Also, I had to define the Host1 + Host2 variables outside the array in the beginning, because the Get-VMHost requires it (I didn't know how to use the variable array in a correct way there)

I don't know if that's the 'fault' of the target environment, but anyway - now it works.

Big thanks again @ LucD for the help !!

# Script Variables


$vcenter = “VCENTER FQDN”

$configData = @(

   @{

   VMHost = 'Host1.FQDN'

   DSC = 'DatastoreCluster1Name'

  },

   @{

   VMHost = 'Host2.FQDN'

   DSC = 'DSC2'

  }

)

$Host1 = "Host1.fqdn"

$Host2 = "Host2.fqdn"


# / Script variables


if ($global:DefaultVIServer){

   Write-Host1. Terminating existing vCenter Connections...” -ForeGroundColor Red

   Disconnect-ViServer -Server * -Confirm:$False -Force}


   Write-Host2. Connecting to vCenter: [$vcenter]” -ForegroundColor Yellow

   $connection = Connect-VIServer $vcenter


   if($connection.isconnected -eq 'true'){

   Write-Host “Connection to [$vcenter] successfully established” -ForegroundColor Green

  Sleep 2}

   else

  {Write-Host “Error while connecting to vCenter Server! Please check your credentials and target IP/FQDN” -ForegroundColor Red

  Sleep 5

   exit}


Write-Host3. Writing out the Data...” -ForegroundColor Green


Get-VMHost -Name $Host1,$Host2 | Get-VM -PipelineVariable vm |


ForEach-Object -Process {

   $correct = ''

   $dsc = Get-Datastorecluster -VM $vm

   $config = $configData | where{$_.VMHost -eq $vm.VMHost.Name}

   if($dsc.Name -eq $config.DSC){

   $correct = 'Yes'

  }

   else {

   $correct = 'No'

  }

   $vm | Select Name,@{N='VMHost';E={$vm.VMHost.Name}},

   @{N='DSC';E={$dsc.Name}},

   @{n='Correct';E={$correct}}

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

0 Kudos