Format-Table errors in script after working on several host

Hi All,

Im fairly new with Powershell but loving every successful second :slight_smile: My script is getting Active Listening ports from host. The script works fine without doing any formatting. Once I add Format_Table it will get through 5 or so host and then toss the below error.

$cred = get-credential

$i = Get-Content "servers.txt"

$out = Invoke-Command -ComputerName $i -ScriptBlock {foreach ($i in $i) 
     {$dd = "===="; $dd + $env:computername + $dd; $globalIP = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties(); $globalIP.GetActiveTCPListeners() | Format-Table -AutoSize}}   -credential $cred

$out | Out-File C:\Users\jdurnil-admin\Documents\Collection\Ports-Prod.txt

I get this 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.

Thank you in advance!

Each time you run Format-Table, it outputs a set of headers as well as data. Once you’ve started a table, you can’t run another Format-Table or you’ll get the error you’re getting. Without seeing where you were putting Format-Table, it’s tough to troubleshoot this for you, but generally I think you should be doing:

$out | Format-Table | Out-File

Oh yes, I missed that part. So I dont even know where to put it actually. These are the two places Ive tried it unsuccessfully

$out | Format-Table | Out-File C:\Users\jdurnil-admin\Documents\Collection\Ports-Prod.txt

$out | Out-File C:\Users\jdurnil-admin\Documents\Collection\Ports-Prod.txt | Format-Table

Well, let’s look at this:

$out | Out-File C:\Users\jdurnil-admin\Documents\Collection\Ports-Prod.txt | Format-Table

What does Out-File normally produce? Nothing. So there’s no input to Format-Table.

$out | Format-Table | Out-File C:\Users\jdurnil-admin\Documents\Collection\Ports-Prod.txt

This, I would expect to create a table inside a text file. If it isn’t, then I have to look at what’s in $out to see why.

At the risk of being a commercial, I’d strongly suggest looking at “Learn Windows PowerShell in a Month of Lunches.” You’re running against some of the most common “gotchas” in how PowerShell works, and it’s exactly what the book walks you through. A lot of people have found it useful in getting started with the shell and understanding what’s actually happening.