Forum Discussion
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"
Thanks, this is working now!
One more question. With the monitoring script, can you query just the concerning snapshot using a filter, instead of looping through all the snapshots? Just curious because of the possible performance impact of querying all the snapshots. So the script would be something like this:
$hostname = '##SYSTEM.HOSTNAME##';
$wildvalue = '##WILDVALUE##'
$VM = '##auto.VM.Name##'
Invoke-Command -ComputerName $hostname -ScriptBlock {
$snapshot = Get-VMSnapshot -vmname $Using:VM | ? {$_.ID -eq $Using:wildvalue}
#rest of code...
}
- Stuart_Weenig3 months ago
Mastermind
You can definitely do that; it would be a SCRIPT datasource (one collection task per instance) instead of a BATCHSCRIPT datasource (one collection task for all instances). Efficiency/performance reasons is the main factor in deciding between SCRIPT and BATCHSCRIPT.
Related Content
- 8 months ago
- 5 months ago