Get-SCOMAgent failed to run: Object reference not set to an instance of an object.

When you try to run SCOM 2012 PowerShell command let Get-SCOMAgent it might run into the following error.

PS D:\>Get-ScomAgent
Get-SCOMAgent:Object reference not set to an instance of an object.

I figured out that this error was related to one specific SCOM Agent.

The error did not report any details what's wrong.

I resolved the problem by removing the affected agents from SCOM.
After new approving the agent the command let Get-SCOMAgent worked fine again. 

To identify the affected Agent I used the following PowerShell command:

get-scomclass -name "Microsoft.SystemCenter.Agent" | get-scomclassinstance | sort displayname | foreach {Get-SCOMAgent $_}

This command determines all instances based on agents class. It walks through all agent instances separately and will run into the same issue. But you easily identify what agents is the problematic one.

Now you can remove the agent from SCOM Console. After some minutes it will appear under "Pending Management" again.

After approving the command let should work fine again.