Formating of HTML output

Posted on 2011-09-02
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
  • 3
LVL 70

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 = ""

# 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 -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

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

23 Experts available now in Live!

Get 1:1 Help Now