Need Help Debugging AI-Generated PowerShell Script

0
9
Asked By TechSavvyGamer42 On

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

Answered By CleverCoder88 On

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.

TechSavvyGamer42 -

I can confirm that both $Group and $Group.group return data. The error happens during the comparison.

Answered By User12345 On

You could try having the AI troubleshoot it for you! It might catch something we can't.

Answered By ScriptWizard77 On

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!

CodeMaster99 -

Make sure to remove any colons in your output statements. Powershell can be picky about that!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.