У меня проблема с сортировкой данных перед подачей данных в HTML. Я пробовал несколько способов сортировки psobject, однако это не сортировка на основе LastRebootTime. Я пытаюсь отсортировать исходный объект в функции: getRemoteServerInfo или пытаюсь отсортировать @writeParams. Оба не сортируются.
$computername = $env:computername
$DateStamp = (Get-Date -Format D)
$FileDateStamp = Get-Date -Format yyyyMMdd
$ScriptPath = Get-Location
$ReportFileName = "$ScriptPath\ServerPatchReport.html"
$ReportFileName2 = "$ScriptPath\ServerPatchReport-Unavailable.html"
$BGColorTbl = "#EAECEE"
$BGColorGood = "#4CBB17"
$BGColorWarn = "#FFFC33"
$BGColorCrit = "#FF0000"
$PendingRebootYes = "Yes"
$PendingRebootNo = "No"
$ReportTitle = "$ReportTitle"
$RunFrom = "This report was generated from $computername"
$Global:Servers = Get-Content servers.txt
# Create output file and nullify display output
New-Item -ItemType file $ReportFileName -Force > $null
New-Item -ItemType file $ReportFileName2 -Force > $null
<#==================================================
Write the HTML Header to the report file
==================================================#>
##HTML code
If ($Servers.count -gt 0) {
Write-ToLogFile -Info "Starting remote job against $($Servers.count) servers"
$RemoteJob = Invoke-Command -ScriptBlock ${function:getRemoteServerInfo} -computername $Servers -AsJob
$JobID = $RemoteJob.Id
Wait-Job -Id $JobID -Timeout $JobTimeout | Out-Null
$FailedServers = Get-Job -ID $JobID | select-object -expandproperty ChildJobs | Where-object {$_.State -ne "Completed"} | Select-object -expandproperty Location
$CompletedJobs = Receive-Job -ID $JobID -ErrorAction SilentlyContinue
Foreach ($Server in $FailedServers) {
writeUnavailableData $ReportFileName2 $Server
}
Foreach ($Server in $CompletedJobs) {
$Server | Sort-Object -Property LastReboot -Descending
#Write-Host $Server
$writeParams = @{
FileName = $ReportFileName
Server = $Server.PSComputerName
InstalledOn = $Server.InstalledOn
KB = $Server.KB
TaskTrigger = $Server.taskTrigger
LastReboot = $Server.LastReboot
}
#$writeParams | sort-object @{Expression={$_[2]}; Ascending=$false}
writeUpdateData @writeParams
}
Write-Host "Finishing report..." -ForegroundColor "Yellow"
writeHtmlFooter $ReportFileName
}
<#==================================================
Function to write server update information to the
HTML report file
==================================================#>
Function writeUpdateData {
param($FileName,$Server,$InstalledOn,$Domain,$KB,$OSType,$DataCenter,$taskTrigger,$LastReboot)
Add-Content $FileName "<tr>"
Add-Content $FileName "<td align='center'>$Server</td>"
Add-Content $FileName "<td align='center'>$OSType</td>"
Add-Content $FileName "<td align='center'>$Domain</td>"
Add-Content $FileName "<td align='center'>$InstalledOn</td>"
Add-Content $FileName "<td align='center'>$LastReboot</td>"
Add-Content $FileName "<td align='center'>$KB</td>"
# Color BG depending on $Warning and $Critical days set in script
If ($InstalledOn -eq "Error collecting data")
{
$DaySpanDays = "Error"
}
Else
{
$System = (Get-Date -Format "MM/dd/yyyy hh:mm:ss")
$DaySpan = New-TimeSpan -Start $InstalledOn -End $System
$DaySpanDays = $DaySpan.Days
}
If ($InstalledOn -eq "Error collecting data" -or $DaySpan.Days -gt $Critical)
{
# Red for Critical or Error retrieving data
Add-Content $FileName "<td bgcolor=$BGColorCrit align='center'>$DaySpanDays</td>"
#Add-Content $FileName "</tr>"
}
ElseIf ($DaySpan.Days -le $Warning)
{
# Green for Good
Add-Content $FileName "<td bgcolor=$BGColorGood align=center>$DaySpanDays</td>"
#Add-Content $FileName "</tr>"
}
Else
{
# Yellow for Warning
Add-Content $FileName "<td bgcolor=$BGColorWarn align=center>$DaySpanDays</td>"
#Add-Content $FileName "</tr>"
}
Add-Content $FileName "<td align='center'>$datacenter</td>"
Add-Content $FileName "<td align='center'>$taskTrigger</td>"
}
<#==================================================
Function for servers that are unavailable
==================================================#>
Function writeUnavailableData
{
param($FileName,$Server)
Add-Content $FileName "<tr>"
Add-Content $FileName "<td bgcolor=$BGColorWarn align='center'>$server</td>"
}
<#==================================================
Function to write the HTML footer
==================================================#>
Function writeHtmlFooter
{
param($FileName)
Add-Content $FileName "</table>"
Add-Content $FileName "<table width='75%' align='center'>"
Add-Content $FileName "<tr bgcolor=$BGColorTbl>"
Add-Content $FileName "</tr>"
Add-Content $FileName "</table>"
Add-Content $FileName "</body>"
Add-Content $FileName "</html>"
}
function getRemoteServerInfo {
#Create output object
$OutputObject = New-Object PSObject
#Fetch last reboot date / time
$LastReboot =gwmi win32_operatingsystem | %{ $_.ConvertToDateTime($_.LastBootUpTime) }
$OutputObject | Add-Member -MemberType NoteProperty -Name LastReboot -value $LastReboot
#Fetch patch info
$session = [activator]::CreateInstance([type]::GetTypeFromProgID('Microsoft.Update.Session'))
$searcher = $session.CreateUpdateSearcher()
$totalupdates = $searcher.GetTotalHistoryCount()
$remoteresult = $searcher.QueryHistory(0 , $totalupdates) | select-Object -First 1 | Select-Object Date, Title
$OutputObject | Add-Member -MemberType NoteProperty -Name remoteresult -value $remoteresult
$Installed = $remoteresult.date
$InstalledOn = $Installed.tolocaltime()
$OutputObject | Add-Member -MemberType NoteProperty -Name InstalledOn -value $InstalledOn
$KB = $remoteresult.Title.Split('(')[1].split(')')[0]
$OutputObject | Add-Member -MemberType NoteProperty -Name KB -value $KB
$OutputObject | Add-Member -MemberType NoteProperty -Name tasktrigger -value $taskTrigger
$OutputObject
}
}