• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

Formating of HTML output

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)

$obj

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

invoke-item $outputFile

Open in new window

0
daveTechSearch
Asked:
daveTechSearch
  • 3
1 Solution
 
Chris DentPowerShell DeveloperCommented:
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).

http://www.indented.co.uk/index.php/2011/01/31/get-localgroupmember/

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.

Chris
0
 
daveTechSearchAuthor Commented:
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.
0
 
daveTechSearchAuthor Commented:
I have solved my formatting issue.  Full script attached.
# Clear error list
$error.clear()

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

# 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

0
 
daveTechSearchAuthor Commented:
Figured this out myself
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now