Albert Widjaja
asked on
Need some help in fixing PowerShell script to list servicename in HTML format & uptime in meaningful format ?
Hi All,
I wonder if anyone here can assist me in fixing the PowerShell script below:
Problem:
1. I cannot display the Uptime in the meaningful format like Days, Hours...
2. I cannot display the service name in the proper HTML table for easy reading.
Thanks in advance.
I wonder if anyone here can assist me in fixing the PowerShell script below:
#################################################################################################################################################################################
# Show the list of server in the specific OU that has problem with stopped service that is set to be running as Automatic when the CPU or the Memory load of the server is > 80%
#################################################################################################################################################################################
#
$ResultFile = "C:\TEMP\TestResult.htm"
$StartOU = "OU=Production 1,OU=Computers,DC=MyDomain,DC=com"
$ServerList = @()
$ServerList = Get-ADComputer -Properties OperatingSystem, OperatingSystemVersion, lastLogonTimestamp, lastLogon -Filter {Enabled -eq $True} -SearchBase $StartOU |
Where-Object {Test-Connection $_.Name -Count 1 -Quiet} |
Select-Object -Property Name
ForEach($Computer in $ServerList) {
$hostdns = [System.Net.DNS]::GetHostEntry($Computer)
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
$LastBoot = [System.Management.ManagementDateTimeconverter]::ToDateTime("$((Get-WmiObject Win32_OperatingSystem -ComputerName $Computer).LastBootUpTime)")
$Uptime = (Get-Date) - $LastBoot
$propHash = [ordered]@{
ComputerName = $Computer
BootTime = $BootTime
Uptime = $Uptime
}
$objComputerUptime = New-Object PSOBject -Property $propHash
$AVGProc = Get-WmiObject -ComputerName $computername win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
$OS = Get-WmiObject -Class -Class Win32_OperatingSystem -ComputerName $computername | Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
$vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" | Select-object @{Name = "C PercentFree"; Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) } }
$result += [PSCustomObject] @{
ServerName = "$computername"
CPULoad = "$($AVGProc.Average)%"
MemLoad = "$($OS.MemoryUsage)%"
CDrive = "$($vol.'C PercentFree')%"
}
$Service = Get-WmiObject Win32_Service -ComputerName $computername | Where {($_.startmode -like "*auto*") -and ($_.state -notlike "*running*")} |`
foreach { new-object psobject -Property @{
DisplayName = $_.DisplayName
Name = $_.Name
StartMode = $_.StartMode
State = $_.State
}
}
$Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> Server Health Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=gray align=center>
<TD><B>Server Name</B></TD>
<TD><B>Uptime</B></TD>
<TD><B>C Drive Utilizatoin</B></TD>
<TD><B>Service Displayname</B></TD>
<TD><B>Service Name</B></TD>
<TD><B>StartMode</B></TD>
<TD><B>State</B></TD>
</TR>"
Foreach($Entry in $Result) {
if((($Entry.CpuLoad) -or ($Entry.memload)) -ge "80") {
$Outputreport += "<TR bgcolor=red>"
}
else {
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD><TD>$($objComputerUptime.Uptime)</TD><TD align=center>$($Entry.Cdrive)</TD>
<TD>$($Service.DisplayName)</TD><TD>$($Service.Name)</TD><TD>$($Service.StartMode)</TD><TD>$($Service.State)</TD>
</TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file $ResultFile -NoClobber
Invoke-Expression $ResultFile
Problem:
1. I cannot display the Uptime in the meaningful format like Days, Hours...
2. I cannot display the service name in the proper HTML table for easy reading.
Thanks in advance.
If you are good at html you can loop through and display them in particualr td and tr
foreach($s in $Service)
{
$s.DisplayName
$s.Name
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks all for participating in this question.
OBDA has fixed it fully working :-)
OBDA has fixed it fully working :-)
Are you going to create one large HTML Table of all your servers? I ran this against my computer only and it is very difficult to read.
Does your output file look like mine?
TestResult.htm