Solved

Powershell output to HTML

Posted on 2016-10-27
1
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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