0

Is it possible with the script below to create a sortable HTML table without using JavaScript? All of the examples I have found so far require the use of JavaScript. If possible I would like it to be purely done in PowerShell.

#Set variables
$image = "C:\Scriptrepository\NHSI(left).gif"
$ConvertImage = [Convert]::ToBase64String((Get-Content $image -Encoding Byte))
$ImageHTML = "<img src=data:image/gif;base64,$($ConvertImage) alt=NHSI/>"
$CurrentDate = Get-Date -Format F

#Function for alternating table colour rows
Function Global:Set-AlternatingRows {
                [CmdletBinding()]
                Param(
                [Parameter(Mandatory,ValueFromPipeline)]
                [string]$Line,

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

                [Parameter(Mandatory)]
                [string]$CSSOddClass
                )
    Begin {
                $ClassName = $CSSEvenClass
        }
    Process {
                If ($Line.Contains("<tr><td>"))
                {   $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
                If ($ClassName -eq $CSSEvenClass)
                {   $ClassName = $CSSOddClass
}
                Else
                {   $ClassName = $CSSEvenClass
            }
        }
    Return $Line
    }
}    

#Function to validate request and create variable from input
Function Global:GetADGroupMembersrecursively {
    Write-Host "Enter Distribution List / AD Group name:" -ForegroundColor Green
    $Global:ADGroupName = Read-Host
    if ([string]::IsNullOrEmpty($ADGroupName))
    { 
    CLS
    Write-Host -ForegroundColor red "Cannot be blank, please re-enter AD Group / distribution list name" | Out-Null
    GetADGroupMembersRecursively 
    }
    $Global:DLCheck = DSQuery group -Name "$ADGroupName"
    if ($DLCheck -eq $null) 
    { 
    CLS
    Write-Host -foregroundcolor red "Did not find AD Group / Distribution List, please verify this is the correct name" | Out-Null
    GetADGroupMembersRecursively
  }
}

GetADGroupMembersRecursively

$Head = @"
<style>
BODY {font-family: Arial; font-size: 8pt; color: #000000; background-color: #ffffff;}
TABLE{margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #4C607B; width: 100%;height :50px;}
TH, TD {border: 1px solid #ffffff; border-collapse: collapse;padding: 3px;}
TH {font-size: 1.2em; background-color: #003366; color: #ffffff; }
TD {color: #000000; }
 .even { background-color: #efefef; }
 .odd { background-color: #c0c0c0; }
TR { background: #b8d1f3; }"
H4 {font-family: Arial; font-size: 12pt; color: #4C607B;align=right;}
H5 {font-family: Arial; font-size: 8pt; color: #4C607B;align=right;}
</style>
"@ 

#Table creation
$Post = "<br><br> Members of $ADGroupName <br> Generated on $CurrentDate"
Get-ADGroupMember -Identity $ADGroupName -Recursive | Get-ADUser -pr GivenName,Surname,mail,sAMAccountName | Select-Object @{Name = "First Name"; Expression = {$_.GivenName}}, @{Name = "Last Name"; Expression = {$_.Surname}}, @{Name = "Email Address"; Expression = {$_.mail}}, @{Name = "User Account"; Expression = {$_.sAMAccountName}} | sort Surname | 
ConvertTo-HTML -head $head -body $ImageHTML -PostContent $Post -As Table | Set-AlternatingRows -CSSOddClass odd -CSSEvenClass even | Out-File C:\ScriptRepository\Results\$ADGroupName.htm
Invoke-Item C:\ScriptRepository\Results\$ADGroupName.htm
11
  • Sortable? Do you mean that you want someone to be able to interact with the HTML you generate and sort a table on one of its columns? Commented Apr 7, 2017 at 13:38
  • Hi Matt, yes, I would like it if they would be able to click a column title to sort the contents by that column. Commented Apr 7, 2017 at 13:50
  • 2
    I'm going to say that it cannot be done with that restriction as I am not aware of any CSS/HTML magic that would allow this. Why can't you use javascript? stackoverflow.com/questions/948578/… suggests jquery which is still javascript reliant Commented Apr 7, 2017 at 14:20
  • 1
    There isn't a way to do it in pure HTML that I know of. If you want dynamic content, then you likely need to use something more dynamic than HTML. Commented Apr 7, 2017 at 14:36
  • 1
    You could use a menu, with multiple html pages (all static), with a different sorting in each, for instance? Commented Apr 7, 2017 at 20:39

1 Answer 1

0

Is it possible with the script below to create a sortable HTML table without using JavaScript?

No. This is not possible with that limitation or at least not with current HTML. You might be able to do something with vbscript in the page but I would not be surprised if that is off limits to you as well.

The only thing I could think to do within the confines you have laid out is to roll out separate reports, within the same html file, that show the preferred sorting options and not necessarily all sorting options.

Something else you could do or do in conjunction is create a CSV file. That way your recipients could read in the data into a program or even a text editor like NotePad++ and have it do the sorting for you. I understand if that is not within the scope of your task but it is still a valid option in general.

If possible I would like it to be purely done in PowerShell.

Another option that might be "out there" is that instead of HTML output you could use something like Out-GridView. That will provide an interactive interface to your data. If the users are the ones running the script then this option would be available to them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.