Solved

Powershell output to HTML

Posted on 2016-10-27
1
53 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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