I used an AI to generate a PowerShell script for monitoring performance counters, but I'm facing an issue with it. I'm currently running this script in VSCode with PowerShell. I've encountered an error saying, "Cannot bind argument to parameter 'ReferenceObject' because it is null". This error seems to occur in the section where I compare the data for each counter across different Domain Controllers (DCs). The variable '$Group.group' contains data, so I'm suspecting that the 'Where-Object' section might be formatted incorrectly. I would really appreciate any help in troubleshooting this!
3 Answers
If the AI messed it up, maybe it can fix its own mess! Also, can you get any output from before the failing part? Like after the line where you're comparing counters? Check if $Group actually has a $Group.group property right before that comparison line. It might give you a clue.
You could try having the AI troubleshoot it for you! It might catch something we can't.
Here's a tweaked version of your script that skips the 'Where-Object'. Instead, use a scriptblock with 'Group-Object' to directly match against DC1:
```powershell
# Performance counters to collect
$PerformanceData = foreach ($DC in $DCs) {
Write-Host "Collecting performance data from $DC..."
[PSCustomObject]@{
CounterData = Get-Counter -ComputerName $DC -Counter $Counters -SampleInterval 5 -MaxSamples 10 |
Select-Object -ExpandProperty CounterSamples |
Select-Object Path, InstanceName, CookedValue
DomainController = "$DC"
}
}
```
This should help with your comparisons. Check it out!
Make sure to remove any colons in your output statements. Powershell can be picky about that!
I can confirm that both $Group and $Group.group return data. The error happens during the comparison.