Author Posts

October 28, 2013 at 2:26 am

Hello Together
i'm writing a script which send automatically an e-mail to an person which folder use to much space in a directory.
And one part of this script create a html-report but i want to convert this html-report into an csv.

And here is my question how can i convert an html-report to a csv-report

October 28, 2013 at 3:27 am

It would help to see the script you're referring to (in particular, the part the generates an HTML report).

As a general observation, it would be simpler to create both an HTML report and a CSV report from the same objects in memory, rather than having to reconstruct objects based on the HTML. Either way is doable, but reading the HTML is more work.

October 28, 2013 at 3:29 am


Param (
[string]$Path = "N:\",
[string]$ReportPath = "N:\Test",
[switch]$Recurse
)

Function AddObject {
Param (
$FileObject
)
$Size = [double]($FSO.GetFolder($FileObject.FullName).Size)
$Script:TotSize += $Size
If ($Size)
{ $Size = CalculateSize $Size
}
Else
{ $Size = "0.00 MB"
}
$Script:Report += New-Object PSObject -Property @{
'Folder Name' = $FileObject.FullName
'Created on' = $FileObject.CreationTime
'Last Updated' = $FileObject.LastWriteTime
Size = $Size
Owner = (Get-Acl $FileObject.FullName).Owner
}
}

Function CalculateSize {
Param (
[double]$Size
)
If ($Size -gt 1000000000)
{ $ReturnSize = "{0:N2} GB" -f ($Size / 1GB)
}
Else
{ $ReturnSize = "{0:N2} MB" -f ($Size / 1MB)
}
Return $ReturnSize
}

Function Set-AlternatingRows {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[object[]]$Lines,

[Parameter(Mandatory=$True)]
[string]$CSSEvenClass,

[Parameter(Mandatory=$True)]
[string]$CSSOddClass
)
Begin {
$ClassName = $CSSEvenClass
}
Process {
ForEach ($Line in $Lines)
{ $Line = $Line.Replace("

October 28, 2013 at 3:55 am

Perfect, you already have your $Report collection full of objects with properties 'Folder Name', 'Created on', 'Last Updated', 'Size', and 'Owner'. You can just pipe $Report to either Export-Csv or ConvertTo-Csv, depending on whether you want to save the file on disk or just generate CSV data as a string in memory.

October 28, 2013 at 5:22 am

can you give an examlpe please

October 28, 2013 at 5:55 am

$Report | Export-Csv -Path $ReportPath\FolderSizes.csv -NoTypeInformation

October 28, 2013 at 7:18 am

thank you
i have one little question...
How can i select only the parts "Owner" and "Size"

October 28, 2013 at 7:25 am

I love it when a scripting language is so expressive that the answer to your question can be taken right out of the question itself. 🙂

$Report | Select "Owner","Size" | Export-Csv -Path $ReportPath\FolderSizes.csv -NoTypeInformation

(Normally, I'd have used the full cmdlet name Select-Object, but I couldn't resist using the word "Select" right out of your question.)

October 28, 2013 at 11:08 pm

hello again a little question.....
I want to compare $Size in the script above with a variable $limit = 1, but i don't know the datatyp of $Size.....

October 29, 2013 at 4:44 am

In your script $Size starts out as a Double, and is then converted to a String (which happens to contain the trailing characters "GB" or "MB"). This is awkward for comparison, and this topic of early "fixing" of output is the topic of one of the Great Debate posts Don put up following the last Scripting Games: https://powershell.org/2013/08/27/powershell-great-debate-fixing-output/.

An alernative is to have your AddObject function just leave $Size as its raw, Double value. Then you can easily use it in a Where-Object filter of $Report later, if you'd like. Just before you send $Report to either ConvertTo-Html or Export-Csv, you can use Select-Object to convert the property to a human-readable form using your CalculateSize function. Something like this:

Function AddObject {
    Param (
        $FileObject
    )

    $Size = [double]($FSO.GetFolder($FileObject.FullName).Size)
    $Script:TotSize += $Size
    
    # Got rid of the code that converted $Size from a double to a String here

    $Script:Report += New-Object PSObject -Property @{
        'Folder Name' = $FileObject.FullName
        'Created on' = $FileObject.CreationTime
        'Last Updated' = $FileObject.LastWriteTime
        Size = $Size
        Owner = (Get-Acl $FileObject.FullName).Owner
    }
}

# Skipped to the end of the script.  Example of only exporting objects where Size is larger than 1GB, and still
# putting the formatted output from CalculateSize in the file.

$limit = 1GB

$Report |
Where-Object { $_.Size -gt $limit } |
Select-Object "Owner",@{Name='Size'; Expression = { CalculateSize $_.Size } } |
Export-Csv -Path $ReportPath\FolderSizes.csv -NoTypeInformation

October 29, 2013 at 4:54 am

thanks you
i searched in about 5 forums for an answer but You are the first person, who gave me a useful solution for my problem

THANK YOU

October 29, 2013 at 4:57 am

No problem! 🙂

October 29, 2013 at 5:29 am

excuse me but i have again a question...
now i have the variable $Report and i filtered ou the parts size, Owner and Folder Name.
If i want filt out the sizes which are under 5000000000 and the rest should again be in a report
how could i do this

thanks again.......

October 29, 2013 at 5:36 am

Please post the current version of your code, and I'll take a look.

October 29, 2013 at 5:45 am

Param (
[string]$Path = "N:\",
[string]$ReportPath = "N:\Test",
[switch]$Recurse
)

Function AddObject {
Param (
$FileObject
)

$Size = [double]($FSO.GetFolder($FileObject.FullName).Size)
$Script:TotSize += $Size

# Got rid of the code that converted $Size from a double to a String here

$Script:Report += New-Object PSObject -Property @{
'Folder Name' = $FileObject.FullName
'Created on' = $FileObject.CreationTime
'Last Updated' = $FileObject.LastWriteTime
Size = $Size
Owner = (Get-Acl $FileObject.FullName).Owner
}
}

Function CalculateSize {
Param (
[double]$Size
)
If ($Size -gt 1000000)
{ $ReturnSize = ($Size / 1GB)
}
Else
{ $ReturnSize = ($Size / 1MB)
}
Return $ReturnSize
}

Function Set-AlternatingRows {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[object[]]$Lines,

[Parameter(Mandatory=$True)]
[string]$CSSEvenClass,

[Parameter(Mandatory=$True)]
[string]$CSSOddClass
)
Begin {
$ClassName = $CSSEvenClass
}
Process {
ForEach ($Line in $Lines)
{ $Line = $Line.Replace("

October 29, 2013 at 7:15 am

Try changing the last few lines to this, see if it produces what you're looking for. I broke the pipelines out into multiple lines (a new line after each pipe character) to help with readability, but the only functional change is that I assigned a new value to $limit, and added the filtering / conversion code to both the CSV and HTML pipelines.

#Create the report and save it to a file

$limit = 5000000000

$Report |
Where-Object { $_.Size -ge $limit } |
Select-Object 'Folder Name', 'Owner', 'Created On', 'Last Updated', @{ Name = 'Size'; Expression = { CalculateSize $_.Size } } |
Sort-Object 'Folder Name' |
ConvertTo-Html -PreContent $Pre -PostContent $Post -Head $Header |
Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd |
Out-File $ReportPath\FolderSizes.html

$Report |
Where-Object { $_.Size -ge $limit } |
Select-Object 'Owner', @{ Name = 'Size'; Expression = { CalculateSize $_.Size } } |
Export-Csv -Path $ReportPath\FolderSizes.csv -NoTypeInformation

October 29, 2013 at 10:16 pm

[quote=11093]thanks you
i searched in about 5 forums for an answer but You are the first person, who gave me a useful solution for my problem
THANK YOU
[/quote]

@Dominique
We have replied at the same day you have posted with the same Answer!
Show us your code!
But you did not gave us a chance in the German PowerShell Forum! 🙁
http://social.technet.microsoft.com/Forums/de-DE/9f885114-86b6-45cd-9dac-8ab225ee1bb3/powershell-wie-man-ein-html-report-in-einen-csvreport-umwandeln-kann?forum=powershell_de

October 29, 2013 at 10:48 pm

Your forum isn't included in the 5 forums i searched..
the problem was i couldn't find my Thread anymore sry.
Next time i will aks you also in your forum.
It was a misunderstanding

November 3, 2013 at 11:11 pm

Hello Dave Wyatt
The Script you send me didn't work.
And it generate the csv and the html document but the documents are empty.

November 4, 2013 at 7:05 am

Well, try this to see if there are any objects in $Report that would pass the Where-Object filter:

$limit = 5000000000

$props = @(
    @{ Label = 'SizeType'; Expression = { $_.Size.GetType().FullName } }
    'Size'
    @{ Label = 'SizeExceedsLimit'; Expression = { $_.Size -gt $limit } }
)

$Report | Select-Object -Property $props

November 5, 2013 at 2:04 am

Hello
The Script above give me a result like this (picture is in the attachment).

November 5, 2013 at 4:32 am

There you go, then. The sizes were all under your specified limit, so the reports are supposed to be empty.

November 5, 2013 at 4:50 am

mhh now i get it
You're genius thanks
But again a question now does $Report contain only the Sizes and if there are true or false, but is it possible to add the owner and the folder names ?

November 5, 2013 at 5:27 am

The last code I posted was just for debugging purposes, you can remove it from the script any time. It did not actually change the value of $Report, though; just used $Report as a starting point.