VMware Cloud Community
jojo130374
Enthusiast
Enthusiast
Jump to solution

Script to show VM wich do not respect DRS rules

Hello

I am looking for a Powercli script to obtain some informations from DRS rules:

I explain the details of the request bellow

I have 2 DRS rules :

The first rule (R1) executes several VM on specifics ESX. The name of ESX begins by SRV1xxxx.. The VM are on datastores wich the name begins by DTX1_....

The second rule (R2) executes several VM on specifics ESX. The name of ESX begins by SRV2xxxx. The VM are on datastores wich the name begins by DTY2_.....

For the 2 rules above, I need to be sure that VM wich are running on datastore DTX1, are on ESX servers named SRV1xxx, and then VM wich are running on datastore DTY2, are on ESX servers named SRV2xxx

Summary :

Rule 1 : List of VM that runs on DTX1 and SRV1xxxx

Rule 2 : List of VM that runs on DTY2 and SRV2xxxx

The script should check and show the VMs which do not follow the rules. For example, if a VM is running on DTX1 and SRV2xxx, or  a VM is running on DTX2 and SRV1xxx, I need to put the name of virtual machines into an excel file, and send the result by mail.

Thanks for your help.

Regards ,

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with this variation

Get-VM |

Select Name,

    @{N='Datastore';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1]}},

    @{N='DSid';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1] -match '\D+(?<number>\d+)\D+' | Out-Null;$matches['number']}},

    @{N='ESXi';E={$_.VMHost.Name}},

    @{N='ESXid';E={$_.VMHost.Name.Split('.')[0] -match '\D+(?<number>\d+)\D+' | Out-Null;$matches['number']}} |

where{$_.DSid -ne $_.ESXid} |

Select Name,Datastore,ESXi


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

View solution in original post

12 Replies
LucD
Leadership
Leadership
Jump to solution

Do you have VMs that might be stored on multiple datastores ?

Or do you only want to look at the datastore where the VMX file is located ?


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

0 Kudos
jojo130374
Enthusiast
Enthusiast
Jump to solution

Hello

Yes I want to look on the datastore where the VMX is located. Most of our VM are using .vmdk wich resides on the same datastore. We have few exception, but don't include this case.

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try something like this ?

It extracts the number from the hostname and the datastore, when they do not match, it displays the VM

Get-VM |

Select Name,

    @{N='Datastore';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1]}},

    @{N='DSid';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1] -match '\D+(\d+)\D+' | Out-Null;$matches['number'] }},

    @{N='ESXi';E={$_.VMHost.Name}},

    @{N='ESXid';E={$_.VMHost.Name.Split('.')[0] -match '\D+(\d+)\D+' | Out-Null;$matches['number']}} |

where{$_.DSid -ne $_.ESXid} |

Select Name,Datastore,ESXi


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

0 Kudos
jojo130374
Enthusiast
Enthusiast
Jump to solution

When I try to execute the script , the fields DSid and ESXid are empty.

The columns DSid and ESXid return no result.

Any idea ?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That would mean that the extraction of the number in the names of the ESXi node and the datastore doesn't work.

Can you give me one or more samples of both ?


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

0 Kudos
jojo130374
Enthusiast
Enthusiast
Jump to solution

I have attached the result of the command executed step by step

the last command returns no result . In the command before the last, the fields DSid and ESXid are empty

Thanks for your help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I see where the problem is, there is more than 1 number in the names.

The numbers that need to correspond between the ESXi nodename and the datastorename, are those the ones in the LVx qualifier ?

In other words:

ESXi name : lv2-bs1-stv11.f1nom0a.local

Datastorename : LV2_QLF1_LBP_DATASTORE_06

The number 2 in this case.


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

0 Kudos
jojo130374
Enthusiast
Enthusiast
Jump to solution

Yes correct !

Then same for :

ESXi name : ng1-bs1-stv11.f1nom0a.local

Datastorename : NG1_QLF1_LBP_DATASTORE_06

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with this variation

Get-VM |

Select Name,

    @{N='Datastore';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1]}},

    @{N='DSid';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1] -match '\D+(?<number>\d+)\D+' | Out-Null;$matches['number']}},

    @{N='ESXi';E={$_.VMHost.Name}},

    @{N='ESXid';E={$_.VMHost.Name.Split('.')[0] -match '\D+(?<number>\d+)\D+' | Out-Null;$matches['number']}} |

where{$_.DSid -ne $_.ESXid} |

Select Name,Datastore,ESXi


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

jojo130374
Enthusiast
Enthusiast
Jump to solution

Thanks for your help , it works !

How can I do to store this result into an excel file and send it into an email ?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want the report as an attachment to the email you can do something like this.

Another option is to send the report in the body of the email as HTML.

Note that I used "splatting" for the Send-MailMessage cmdlet.

Makes the script more readable imho.

$reportName = 'report.csv'

Get-VM |

Select Name,

    @{N='Datastore';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1]}},

    @{N='DSid';E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].Split('[')[1] -match '\D+(?<number>\d+)\D+' | Out-Null;$matches['number']}},

    @{N='ESXi';E={$_.VMHost.Name}},

    @{N='ESXid';E={$_.VMHost.Name.Split('.')[0] -match '\D+(?<number>\d+)\D+' | Out-Null;$matches['number']}} |

where{$_.DSid -ne $_.ESXid} |

Select Name,Datastore,ESXi |

Export-Csv -Path $reportName -NoTypeInformation -UseCulture

$sMail = @{

    To = 'destination@domain.com'

    From = 'me@domain.com'

    SmtpServer = 'mail.domain.com'

    Subject = 'Report'

    Body = "Report generated on $(Get-Date -Format 'ddd MMM yyyy, hh:mm')"

    Attachments = $reportName

}

Send-MailMessage @sMail


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

jojo130374
Enthusiast
Enthusiast
Jump to solution

Thanks you very much. It works

0 Kudos