Hey guys,
Many thanks for the good support.
I rethink the script and start from scratch with a paper and a pen.
I wanted this script to first scan all ID used and then do the comparison for each ID. The ID can be found by removing the following characters on the DRS group name: "vm-" and "-01".
This is the code:
# vCenter Server specification
$vCenter = "***"
$vCenteruser = "***"
$vCenterUserPasswd="***"
# Connect to vCenter
add-pssnapin VMware.VimAutomation.Core
Write-Output "Connecting to $vCenter…."
Connect-VIServer -Server $vCenter -user $vCenterUser -password $vCenterUserPasswd
# Import required module
Import-Module DRSRule
# Get DRSVMGROUP in an array
$DRSGroup = Get-DrsVMGroup | Select-Object Name
#
# Cluster specification
$cluster1= "***"
$cluster = Get-Cluster -Name $cluster1
#
#for each customer ID
foreach ($Group in $DRSGroup)
{
#VM Included in DRS Group
$DRSGroupVMs = Get-DrsVMGroup -Cluster $cluster -Name $Group | Sort-Object vm | Select-Object -ExpandProperty vm
#Get VM ID Only
$ID = $Group -Replace "vm-", ""
$ID = $ID -Replace "-01", ""
#VM Containing the ID
$CustomersVM = Get-vm -Location $cluster1 | where {($_.name -like "*$ID*")} | Sort-Object Name | Select-Object Name
#Compare the 2 results
Compare-Object -referenceobject $DRSGroupVMs -differenceobject $CustomersVM -passThru | Sort-Object Name | Select-Object Name
}
The big issue I have is that line 22 doesn't give me any results for some reasons: Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.
I tried to enter directly the cluster name but it's the same result.
EDIT: this has been solved by adding $DRSGroup = Get-DrsVMGroup | Select-Object Name | Select-Object -ExpandProperty Name
Now I have: Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.
I'm doing some tests.
EDIT2: I added this line:
$ID = $Group -Replace "vm-", ""
$ID = $ID -Replace "-01", ""
$ID = "*$ID*"
#VM Containing the ID
$CustomersVM = Get-vm -Location $cluster | where {($_.name -like $ID)} | Sort-Object Name | Select-Object -ExpandProperty Name
It seems to work now.
Regards,
Benoît.