The problem with your discovery script is that you're setting the wildvalue to the name of the VM, which means you're intending only to track the VM itself, not the age of the snapshots for the VM. You're right that you'll need a line of output per VM per snapshot. However, it should look more like this:
$hostname = '##SYSTEM.HOSTNAME##'
Invoke-Command -ComputerName $hostname -ScriptBlock {
$vms = Get-VM
foreach ($vm in $vms){
$VMName = $vm.Name
$VMState = $vm.State
$snapshots = Get-VMSnapshot -VMName $vm.Name
foreach ($snapshot in $snapshots) {
$SnapshotName = $snapshot.Name
$SnapshotID = $snapshot.Id
Write-Host "$SnapshotID##$SnapshotName######vm.name=$VMName&vm.state=$VMState"
}
}
}
# return with a response code that indicates we ran successfully
Exit 0;
This will make one instance per snapshot. You can filter the snapshots using a discovery filter based on the auto.vm.state instance level property created by the script and you can group based on the auto.vm.name instance level property created by the script.
Once that's done, the only thing you need to know during collection about each snapshot is its ID (the same one defined in discovery) and the age:
$hostname = '##SYSTEM.HOSTNAME##'
Invoke-Command -ComputerName $hostname -ScriptBlock {
$vms = Get-VM
$currentDate = Get-Date
foreach ($vm in $vms){
$snapshots = Get-VMSnapshot -VMName $vm.Name
foreach ($snapshot in $snapshots) {
$SnapshotAge = ($currentDate - $snapshot.CreationTime).Days
$SnapshotID = $snapshot.Id
Write-Host "$SnapshotID.AgeInDays: $SnapshotAge"
}
}
}
Exit 0
The only thing remaining would be to create your datapoint. It would use "key-value pairs" and the key would be "##WILDVALUE##.AgeInDays"