Format Conflict - dir function:

by thnk2wn at 2012-09-03 21:57:22

When I run the below code (I changed the assembly name for ease of running/reproduction) :


function Get-MyAssembly
{
[appdomain]::currentdomain.getassemblies() <br> | where-object {$_&#46;FullName -like 'System&#46;Data, *'}
| select-object -first 1
}

function Select-MyAssemblyTypes
{
$asm = Get-MyAssembly
$asm.GetTypes() | Where-Object {$.IsPublic} | Sort-Object {$.FullName }
}

dir function:

Select-MyAssemblyTypes | Where-Object {$.Name -like ‘DataTable*’} | ft


…I receive the error:
The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.
+ CategoryInfo : InvalidData: (:slight_smile: [out-lineoutput], InvalidOperationException
+ FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand


If I remove the "dir function:" or pipe that to Out-Default as below, the problem goes away. Can someone explain this behavior to me? I can live with Out-Default but I don’t understand why this issue happened and it took a while to narrow it down to "dir function:" being the problem. I am seeing some posts about potential bugs around this type of behavior so maybe it’s just an issue in PowerShell V2? One such post is: http://81stperson.blogspot.com/2011/09/powershell-gotcha-kink-in-powershell.html


function Get-MyAssembly
{
[appdomain]::currentdomain.getassemblies() <br> | where-object {$_&#46;FullName -like 'System&#46;Data, *'}
| select-object -first 1
}

function Select-MyAssemblyTypes
{
$asm = Get-MyAssembly
$asm.GetTypes() | Where-Object {$
.IsPublic} | Sort-Object {$.FullName }
}

dir function: | Out-Default

Select-MyAssemblyTypes | Where-Object {$
.Name -like ‘DataTable*’} | ft
by RichardSiddaway at 2012-09-04 03:26:08
try

dir function: | ft

Its a common problem when you output two different objects

explicitly formatting usually sorts it