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 ,
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
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
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
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
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
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
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
Yes correct !
Then same for :
ESXi name : ng1-bs1-stv11.f1nom0a.local
Datastorename : NG1_QLF1_LBP_DATASTORE_06
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
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
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
Thanks you very much. It works