Creating HTML Reports in Powershell... by Don Jones

[pre]
function Create-TableOfContents
{
[CmdletBinding()]
Param
(

Param1 help description

[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]$PathToArchive,

#Param2 help description
[parameter(Mandatory=$true,
Position=1)]
[string]$PathToTOC
)

Begin
{
}
Process
{
$Style=@"
tr.odd {background-color: pink;}
tr.even {background-color: lightblue;}
h1 {border:1px solid black;background-color:lightgreen;text-align:center;width:700px}
table {border:2px solid red;width:700px}
th,td {padding-left:10px;}
.ft {width:80px}
.fs {width:80px}
.dc {width:115px}
h4 {background-color:lightgreen;font-style:em;width:700px;}

"@

$i=0
$subpaths=(Get-ChildItem -Path $PathToArchive -Directory -Recurse | Select-Object -ExpandProperty fullname)
$subpaths | ForEach-Object -Process {
$test1=(Get-ChildItem -Path $_ -File);
$test2=(Get-ChildItem -Path $_ -Directory)
if (!$test1.Exists -and !$test2.exists){
Write-Host “There are no files or directories in $_ ,empty folder!.”
} elseif($test1.exists -and $test2.Exists){
$i++;
$glublist=New-Object -TypeName System.Collections.ArrayList;
Write-Host “files AND directories in $_ “, “File Block #$i”
foreach ($file in $test1){
if((Get-ItemProperty -Path $file.FullName).Attributes -band [io.fileattributes]::Archive -and (Get-ItemProperty -Path $file.FullName -Exclude partial))
{($glublist.add($file))
Write-Host “File Ready to Archive”;
} else {Write-Host “File Not Ready to Archive”}}
if($glublist.count -gt 0){
$DI=($test1.psparentpath -split(”::”))[1]
$params1=@{‘as’=‘table’
‘precontent’="

$DI

" 'oddrowcssclass'='odd' 'evenrowcssclass'='even' 'properties'=@{n='File Name';e={$_.basename}}, @{n='File Type';e={$_.extension};css={'ft'}}, @{n='Size (KB)';e={$_.length/1e3 -as [int]};css={'fs'}}, @{n='Date Created';e={($_.lastwritetime).toshortdatestring()};css={'dc'}}}

$placeholder=($glublist | Sort-Object -Property extension,name | Select-Object -Property basename,extension,length,lastwritetime |
ConvertTo-EnhancedHTMLFragment @params1)
New-Variable -Name frag$i -Value ($placeholder)
}

} elseif($test2.exists){
Write-Host “Directories only in $"
} else{
$i++;
$gloplist=New-Object -TypeName System.Collections.ArrayList
Write-Host "Files only in $
”, “File Block # $i”
foreach ($file in $test1){
if((Get-ItemProperty -Path $file.FullName).Attributes -band [io.fileattributes]::Archive -and (Get-ItemProperty -Path $file.FullName -Exclude partial))
{($gloplist.add($file))
Write-Host “Archive Bit Set”
} else {Write-Host “Archive Bit Cleared”}}
if($gloplist.count -gt 0){
$DI=($test1.psparentpath -split("::"))[1]
$params1=@{‘as’=‘table’
‘precontent’="

$DI

" 'oddrowcssclass'='odd' 'evenrowcssclass'='even' 'properties'=@{n='File Name';e={$_.basename}}, @{n='File Type';e={$_.extension};css={'ft'}}, @{n='Size (KB)';e={$_.length/1e3 -as [int]};css={'fs'}}, @{n='Date Created';e={($_.lastwritetime).toshortdatestring()};css={'dc'}}}

$placeholder=($gloplist | Sort-Object -Property extension,name | Select-Object -Property basename,extension,length,lastwritetime |
ConvertTo-EnhancedHTMLFragment @params1)
New-Variable -Name frag$i -Value ($placeholder)
}}
}
$params2=@{‘CssStyleSheet’=$Style
‘title’=“Table Of Contents”
‘precontent’="

Path To Archive Folder

" 'HTMLFragments'=@((Get-Variable frag* -ValueOnly))}

ConvertTo-EnhancedHTML @params2 | Out-File $pathtoTOC
}}

[/pre]

Getting this to finally work was a real humpfest. It took several months, and I had to also take a class on HTML5 and CSS from EdX university…Really excellent, BTW. I would show some of the output, but I can’t get the clipboard to do a paste.

On the book I have a question. The code starts as follows:

.EXAMPLE

.\New HTMLSystemReport -ComputerName ONE,TWO`

-Path C:\Reports\

#>

[CmdletBinding()]

param(

[Parameter(Mandatory=$true,

ValueFromPipeline=$true…

Shouldn’t there be a line just after the comment closure that says

Function verb-noun?

 

[quote quote=162929]
Shouldn’t there be a line just after the comment closure that says

Function verb-noun?[/quote]

No - you can run a script just like a function.

Regardless of that: It sounds like you’ve already read some Powershell books. Haven’t you read one or two chapters about code formatting - especially indenting code for better readability? If not you may read The PowerShell Best Practices and Style Guide.

Sorry, Olaf. I messed up and submitted after I lost the formatting. BTW, I discovered ISE Steroids which really helps both code development, curly brace matching, and a bunch of other features I haven’t had time to explore…

Peter

I suggest you to use VSCode , that is now the universal code editor. It has an awesome PowerShell extension.

VSCode: https://code.visualstudio.com/

KVPrasoon: Thanks I have VSCode version 1.32.1, but have only ever used it for my HTML class. Still learning about all the features… Something that helps identify Powershell coding mistakes would be very welcome indeed…

Great, VSCode is the leading code editor in the industry, it has support for almost all languages and PowerShell extension is far better than for any other languages.

I hope your question is answered. Hence closing this thread.