下面的腳本可以在不使用JavaScript的情況下創建可排序的HTML表格嗎?到目前爲止我發現的所有例子都需要使用JavaScript。如果可能的話,我希望它在PowerShell中完成。Powershell可排序的HTML表格
#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
可排序?你的意思是你希望有人能夠與你生成的HTML進行交互,並在其中一列上對錶格進行排序? – Matt
嗨馬特,是的,我希望它能夠點擊列標題來對該列中的內容進行排序。 –
好的。你的意思是沒有Java或Java * Script *嗎?答案也必須是HTML。我的意思是你可以使用'Out-GridView',它可以爲你做這些事情。模擬,如果這將被不使用PowerShell的人閱讀。你不能只是做一些像[這從w3schools](https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sort_table_desc)....是的,我知道它的JavaScript?我不知道你有什麼排除。 – Matt