[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Custom Formatting with PowerShell v2.0

Posted on 2010-04-08
6
Medium Priority
?
1,216 Views
Last Modified: 2012-05-09

Hi all,

I'm trying to format the output of table I created based on custom objects created like this

$objFile = New-Object psobject
$objFile | Add-Member noteproperty Name $name
$objFile | Add-Member noteproperty fSize $Size
$objFile | Add-Member noteproperty Extention $ext

So I fill up the table and try to output using this simple formatting:

$theFiles | Ft @{Expression={$_.Name};Label="Name";width=25},@{Expression={$_.fSize};Label="Size";width=25}

This always gives me the error "
out-lineoutput : 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."

When I use  format-table in combination with the field names it works as expected (    $theFiles | ft name,fsize   )


0
Comment
Question by:Bart-Vandyck
  • 4
  • 2
6 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 30114630

How are you building $theFiles? Is it just a collection of $objFile objects?

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 30114855

For instance... I can build the object like this and run it without error.

Which version of PowerShell are you using? All my testing is done under PowerShell 2 which occasionally throws a spanner in the works if trying to compare with 1.

Chris
$theFiles = 1, 2, 3 | %{
  $name = "Name$_"
  $Size = "Size$_"
  $Ext = "Ext$_"

  $objFile = New-Object psobject
  $objFile | Add-Member noteproperty Name $name
  $objFile | Add-Member noteproperty fSize $Size
  $objFile | Add-Member noteproperty Extention $ext

  $objFile
}

$theFiles | Ft @{Expression={$_.Name};Label="Name";width=25},@{Expression={$_.fSize};Label="Size";width=25}

Open in new window

0
 
LVL 4

Author Comment

by:Bart-Vandyck
ID: 30175777

Chris,

You're example works for me to;-)

Well the code looked like this

function MakeObj($name,$size,$ext)
 
{
$objFile = New-Object psobject
$objFile | Add-Member noteproperty Name $name
$objFile | Add-Member noteproperty fSize $Size
$objFile | Add-Member noteproperty Extention $ext
$objFile
 
}


$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum -ErrorAction 0)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName -ea 0 -recurse | Measure-Object -property length -sum -ea 0)
        Write-Progress -Activity "Searching for folders larger then $FolderSize MB" -CurrentOperation $i.FullName -Status "Checking:"
            if (($subfolderitems.sum/1MB) -gt $folderSize)
            {
                  makeobj $i.FullName ($subFolderItems.sum / 1MB) $i.Extension
                  $theFiles = $theFiles + (makeobj $i.FullName $subFolderItems.sum $i.Extension)
            }
           
    }
 
 $theFiles = $theFiles | Sort-Object -Property Size -Descending  
 $theFiles | ft @{Expression={$_.Name};Label="Name";width=25},@{Expression={$_.fSize};Label="Size";width=25}


So this gave me the problem. Now when I was looking it over this morning, i saw I did create the psobject twice but only added the last one

                  makeobj $i.FullName ($subFolderItems.sum / 1MB) $i.Extension
                  $theFiles = $theFiles + (makeobj $i.FullName $subFolderItems.sum $i.Extension)

The first line is/was actually a left over from the beginning of the script, but as it turns out if I delete this first line it starts working. So deleting "makeobj $i.FullName ($subFolderItems.sum / 1MB) $i.Extension" and the out-put of $theFiles starts working, which I found a bit strange. Do you have an explanation for this?

A quick other question, how to you get the syntax highlighted  code in the message?

rgds

Bart

0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
LVL 71

Expert Comment

by:Chris Dent
ID: 30188534

This one is quick and easy:

> A quick other question, how to you get the syntax highlighted  code in the message?

The Code link at the bottom of the comment box will pop up a code box. It won't insert in-line at the moment, that functionality will be added eventually.

Chris
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 total points
ID: 30191019

> Do you have an explanation for this?

I'm afraid I don't, I'm still unable to reproduce the error you receive. This could be a version disparity, which version of PowerShell are you running this in?

I'm a little confused about the output from the script. It returns folders over a certain size? Why include Extension?

Either way, it can be simplified, no need to construct your own object, after all, taking an existing object and writing it to a new object is something Select-Object was made for.

You can happily apply the Format-Table expression to any of these, you only need modify the property it accesses for Name (FullName vs Name).

Chris
# If you want sub-folders over a certain size

$FolderSize = 1
Get-ChildItem | ?{ $_.PsIsContainer } | `
  Select-Object FullName,
    @{n='Size';e={ (Get-ChildItem $_.FullName -Recurse | Measure-Object Length -Sum).Sum/1Mb }} | `
  ?{ $_.Size -gt $FolderSize } | Sort-Object Size -Descending

# If you want files over a certain size

$FolderSize = 1
Get-ChildItem -Recurse | `
  Select-Object FullName, Extension, @{n='Size';e={ ($_.Length)/1Mb }} | `
  ?{ $_.Size -gt $FolderSize } | Sort-Object Size -Descending

# If you want sub-folders over a certain size, recursive

Get-ChildItem -Recurse | ?{ $_.PsIsContainer } | `
  Select-Object FullName,
    @{n='Size';e={ (Get-ChildItem $_.FullName -Recurse | Measure-Object Length -Sum).Sum/1Mb }} | `
  ?{ $_.Size -gt $FolderSize } | Sort-Object Size -Descending

Open in new window

0
 
LVL 4

Author Closing Comment

by:Bart-Vandyck
ID: 31712269

Hi Chris,

I can understand the confusion about the output, let's just say I was having fun playing, learning PowerShell. The whole thing started as my USB stick was almost full and I wanted to look the PowerShell way for it. The goal of the script is to 1. search all folders above a certain size. 2. Display a menu 3. when you type in the number of the folder you want to see, have a grouped view of the files based on the extention.

Thanks for you're examples on Select-object, I have never used it myself, but indeed it will simplify the whole thing and you're example make the use of it verry clear.

Thanks a lot!!

Bart
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question