I’m sure there is a totally rational reason for this issue, but I’ll be stumped if I know it.

I have been tinkering with some reusable scripts to help me rapidly document SharePoint farm state, as recently I have been touching lots of farms in an “audit” or “what do you think, Seb?” context.

Taking a steer from Microsoft (document farms script on Technet) plus others on the intertubes, I am just munging together a bunch of output from both SharePoint-land cmdlets (Get-SP…) with some server based stuff retrieved via WMI (Get-WmiObject…), nothing fancy.

Under certain circumstances it would seem that the formatting of a prior cmdlet is affecting the operation of a subsequent cmdlet.

By way of example:

Get-SPFarm | select name, buildversion, @{expression = {$_.defaultserviceaccount.name} ; Label = “Farm Account”}, @{expression = {$_.needsupgrade} ; Label = “Needs Upgrade?”}

Followed by:

Get-SPManagedAccount;

Results in a script stop with the error:

Operation is not valid due to the current state of the object. At :line:0 char:0

As I mentioned above, I am sure that there is a reason why this behavior is seen, but it annoys in the extreme.

Stopping it?

Simply pipe the output of the first cmdlet (in my case to the host) and the operation of the second cmdlet is unimpeded.

The first cmdlet now looks like this:

Get-SPFarm | select name, buildversion, @{expression = {$_.defaultserviceaccount.name} ; Label = “Farm Account”}, @{expression = {$_.needsupgrade} ; Label = “Needs Upgrade?”} | Out-Host

Simples.

more to follow…