Solved

Powershell output to HTML

Posted on 2016-10-27
1
29 Views
Last Modified: 2016-10-27
I have the below script, it converts to HTML and usually emails (i have it commented out) but this is in a pci zone so no access to email.  Is there a way i can save it to a UNC path?

$servers = Get-ADComputer -SearchBase "OU=Servers,DC=Enterprise-1,DC=local" -Filter *
$servers += Get-ADComputer -SearchBase "OU=Domain Controllers,DC=Enterprise-1,DC=local" -Filter *

$data = @()

foreach ($i in $servers){

    $server = $i.name
	$server

    if (Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue){ 

        $wmi = gwmi Win32_OperatingSystem -ComputerName $server
        $bootTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
        $uptime = New-TimeSpan -Start $bootTime -End (Get-Date)
        $upDays = $uptime.Days
        $upHours = $uptime.Hours
        $upMin = $uptime.Minutes
        $upSecs = $uptime.Seconds

        $object = New-Object –TypeName PSObject
        $object | Add-Member –MemberType NoteProperty –Name Server –Value $server
        $object | Add-Member –MemberType NoteProperty –Name Days –Value $upDays
        $object | Add-Member –MemberType NoteProperty –Name Hours –Value $upHours
        $object | Add-Member –MemberType NoteProperty –Name Minutes –Value $upMin
        $object | Add-Member –MemberType NoteProperty –Name Seconds –Value $upSecs

        #Write-Output $object
        $data += $object

        #Write-Output "$server - Uptime: $($uptime.Days):$($uptime.Hours):$($uptime.Minutes):$($uptime.Seconds)"

    }
    else {

        $object = New-Object –TypeName PSObject
        $object | Add-Member –MemberType NoteProperty –Name Server –Value $server
        $object | Add-Member –MemberType NoteProperty –Name Days –Value "null"
        $object | Add-Member –MemberType NoteProperty –Name Hours –Value "null"
        $object | Add-Member –MemberType NoteProperty –Name Minutes –Value "null"
        $object | Add-Member –MemberType NoteProperty –Name Seconds –Value "null"

        #Write-Output $object
        $data += $object

    }
}
#$mydata = $data | sort Days -Descending | ft -auto
#$mydata = $data | sort Days -Descending | ft -auto | Out-String
$data = $data | Sort-Object Days -Descending | ConvertTo-Html
#Send-MailMessage -To "" -Subject "[Info] Server Uptimes" -From "" -Body "$data" -BodyAsHtml -SmtpServer ""

Open in new window

0
Comment
Question by:leadtheway
1 Comment
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41862123
You can use Out-File to save the file.. Example..

$data | Sort-Object Days -Descending | ConvertTo-Html | Out-File "\\ServerA\Reports\Report.htm"

Open in new window

Or after line 50 add..
$data | Out-File "\\ServerA\Reports\Report.htm"

Open in new window

PS : Make sure the share is accessible from your server where you run the script..
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now