Formating of HTML output

Posted on 2011-09-02
Medium Priority
Last Modified: 2012-05-12
Objective: Query local adminstrators group for group membership on remote systems

Getting what I want so far, one step at a time, but need to get the results for 'Members' each on their own line.  Right now it is just getting strung to gether.

My goal is to keep the output in HTML format, eventually need to figure out how to add an additional column to:
1. Query each group member and determine if it is a local user, domain user, or domain group.
2. If the member is a domain group, expand the group.

My main focus right now is to properly format the 'members', one per line.

This will be supplied to our security team once it is complete....
$serverList = (read-host "Enter servers to query (separate with comma)").split(',') | %{$_.trim()}
$cellFormat = "<style>"
$cellFormat = $cellFormat + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$cellFormat = $cellFormat + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$cellFormat = $cellFormat + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$cellFormat = $cellFormat + "</style>"
$outputFile = read-host "Please enter path/name for output file (include .htm extention)"

@(foreach($server in $serverList)

$group = [ADSI]("WinNT://$server/Administrators,group")   
$GMembers = $group.psbase.invoke("Members") 
$obj = new-object PSObject

$obj | add-member NoteProperty -Name "ServerName" -Value $server
$obj | add-member NoteProperty -Name "Members" -Value ($GMembers | ForEach-Object {($_.GetType().InvokeMember("Name",'GetProperty', $null, $_, $null)).split(',')}| out-string)


}) | convertTo-HTML -head $cellFormat | set-content $outputFile

invoke-item $outputFile

Open in new window

Question by:daveTechSearch
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
  • 3
LVL 71

Expert Comment

by:Chris Dent
ID: 36505925
I have a snippet at work that I can post tomorrow which will expand groups if you wish. Please remind me if I haven't posted it, rather busy recently. In the meantime, this example includes all the possible properties you can return using this interface, including the object class (allowing you to determine whether or not it's a user or a group).


It's a little more complex than your example above (in presentation), but you could pick on my list of field names, and select them as you've done with Name above.

You can simplify your loop a little too. Instead of:
@(foreach($server in $serverList)

Open in new window

Make it:
$serverList | ForEach-Object {
  $group = [ADSI]("WinNT://$_/Administrators,group")
} | ConvertTo-Html ...

Open in new window

Manipulating the format as you've done with CSS is good.


Author Comment

ID: 36518148
i'd like to see what your snippet on expanding groups.  for outputting each group on a single line I decided to replace 'winnt' with a <BR> tag.

Accepted Solution

daveTechSearch earned 0 total points
ID: 36893981
I have solved my formatting issue.  Full script attached.
# Clear error list

# Specify output path
$output = "c:\psOutput"

# Query user for SMTP address
$addy = read-host "Enter your email address"
# Create variable for email server
$mailServer = "mail.server.here"

# Prompt user for server list to query
$serverList = (read-host "Enter servers to query (separate with comma)").split(',') | %{$_.trim()}
# Create format for later HTML output file
$cellFormat = "<style>"
$cellFormat = $cellFormat + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$cellFormat = $cellFormat + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$cellFormat = $cellFormat + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$cellFormat = $cellFormat + "</style>"

# Begin loop to process/query servers specified by the user
Write-host "Querying Servers"
@(foreach($server in $serverList)
Write-host "Querying $server"
# Query server for members of Local Administrators group
$group = [ADSI]("WinNT://$server/Administrators,group")   
$GMembers = $group.psbase.invoke("Members") 
# Write out group memebership to log file, formatted for later use
$GMembers | ForEach-Object {($_.GetType().InvokeMember("AdsPath",'GetProperty', $null, $_, $null)).split(',')} | out-file $output\gmembersTMP.log
# Read in formatted list and create variable
$Members = (get-content $output\gmembersTMP.log |where {$_ -ne " "})

# Create a new PowerShell object to be used in creating final output
$obj = new-object PSObject
# Add items to PowerShell object
$obj | add-member NoteProperty -Name "ServerName" -Value ($server)
$obj | add-member NoteProperty -Name "Members" -Value ("$members" | out-string)
# Write out object information 

# End loop and export data to CSV
}) | export-csv -noType $output\adminQuery.csv
write-output "Finished querying servers"
Write-output "Manipulating data for final output"

# Read in CSV results and create new variable
$queryResults = import-csv $outPut\adminQuery.csv

# Loop through CSV and format data for final output
@(foreach($query in $queryResults)
        $query | select @{name="ServerName";expression={$query.ServerName |out-string}},@{name="Administrator GroupMembers";expression={$query.Members}}

# End loop
    }) | 
    # Convert data to HTML format, apply specific formatting and write out HTM file
    convertTo-HTML -head $cellFormat | set-content $output\adminQuery.htm

#Replace 'WinNT://' with a '<BR>' tag to properly format HTML for group member listings
(get-content c:\temp\adminQuery.htm) -replace 'WinNT://','<BR>' | set-content $outPut\adminQuery.htm

#Send email attachment to user (provided a valid SMTP address was provided)
send-mailmessage -subject "Admin query results" -to $addy -from noReply@owfg.com -smtpServer $mailServer -attachments $output\adminQuery.htm

$error | out-file $output\AdministratorQuery.err.log

Open in new window


Author Closing Comment

ID: 36915534
Figured this out myself

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

777 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