?
Solved

quick powershell question need to delete a table in the code

Posted on 2014-12-25
8
Medium Priority
?
180 Views
Last Modified: 2014-12-28
hello,

can someone please look at the code below, when the report is generated, there is a [] table entry right after the title.

you'll see it in the attached screen shot.
here is the powershell code, can someone please tell me where to delete this invalid table entry [per my screen shot attached]

Function Getservicestatus($service, $server)
{
	$st = Get-service -computername $server | where-object { $_.name -eq $service }
	if($st)
	{$servicestatus= $st.status}
	else
	{$servicestatus = "Not found"}
	
	Return $servicestatus
}

$Forest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()

[string[]]$computername = $Forest.domains | ForEach-Object {$_.DomainControllers} | ForEach-Object {$_.Name} 


$report1= @()
foreach ($server in $computername){
$temp1 = "" | select server, pingstatus
if ( Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue ) {
$temp1.pingstatus = "Pinging"
}
else {
$temp1.pingstatus = "Not pinging"
}
$temp1.server = $server
$report1+=$temp1
}

$b = $report1 | select server, pingstatus  | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>Server Availability</h2>" | Out-String


$report = @()

foreach ($server in $computername){
$temp = "" | select server, NTDS, DNS, DFSR, netlogon, w32Time
$temp.server = $server

$temp.NTDS = Getservicestatus -service "NTDS" -server $server
$temp.DNS = Getservicestatus -service "DNS" -server $server
$temp.DFSR = Getservicestatus -service "DFSR" -server $server
$temp.netlogon = Getservicestatus -service "netlogon" -server $server
$temp.w32Time = Getservicestatus -service "w32Time" -server $server
$report+=$temp
}

$b+= $REPORT | select server, NTDS, DNS, DFSR, netlogon, w32Time | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>Service Status</h2>" | Out-String


add-type -AssemblyName microsoft.visualbasic 
$strings = "microsoft.visualbasic.strings" -as [type] 


$report = @()
foreach ($server in $computername){
$temp = "" | select server, SysvolTest
$temp.server = $server
$svt = dcdiag /test:netlogons /s:$server
if($strings::instr($svt, "passed test NetLogons")){$temp.SysvolTest = "Passed"}
else
{$temp.SysvolTest = "Failed"}
$report+=$temp
}
$b+= $REPORT | select server, SysvolTest | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>NetLogon Test</h2>" | Out-String



$workfile = repadmin.exe /showrepl * /csv 
$results = ConvertFrom-Csv -InputObject $workfile | where {$_.'Number of Failures' -ge 1}
 
if ($results -ne $null ) {
    $b+= $results | select "Source DSA", "Naming Context", "Destination DSA" ,"Number of Failures", "Last Failure Time", "Last Success Time", "Last Failure Status" | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>Replication Status</h2>" | Out-String    

} else {
    $b+= "<h2>Replication Status</h2><br>There were no Replication Errors<br><br>"
}




$head = @'
<style>
body { background-color:#f5f5f5;
       font-family:Arial;
       font-size:10pt; }
td, th { border:1px solid black; 
         border-collapse:collapse; }
th { color:white;
     background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
table { margin-left:50px; }
</style>
'@
 
$s = ConvertTo-HTML -head $head -PostContent $b -Body "<h1>Automated AD DS Health Check</h1>" | Out-string


$emailFrom = "reports@mydomain.com" 
$emailTo = "user@mydomain.com"

$smtpserver= "mailserver.mydomain.com" 
$smtp=new-object Net.Mail.SmtpClient($smtpServer)

$msg = new-object Net.Mail.MailMessage
$msg.From = $emailFrom
$msg.To.Add($emailTo)
$msg.IsBodyHTML = $true
$msg.subject="Active Directory Health Check Report" 
$msg.Body = $s
$smtp.Send($msg)

Open in new window

Capture.PNG
0
Comment
Question by:siber1
[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
8 Comments
 
LVL 17

Expert Comment

by:Learnctx
ID: 40518137
That's interesting. I run the script and I do not get that empty table. Try export to a html file rather than sending the email and see what results you get? I'm just exporting the HTML to a file. Maybe its an Outlook render issue or something along those lines?
0
 
LVL 44

Expert Comment

by:Amit
ID: 40518832
Strange for me also, I am not getting any error and getting email as well. Do you have enough rights to run it. It looks like your account doesn't have rights to query AD. Add your account to domain admin and check again. Run it from DC directly.
0
 

Author Comment

by:siber1
ID: 40519234
thanks Amit, the script runs fine for me as well, but per my attached screen shot there is what appears to be a partial table right above the word "Server Availability"

I would like to remove that so that it doesn't appear in the emailed results.  thx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Qlemo
ID: 40520392
Yes, there is an empty table. For some reason ConvertTo-HTML -body wants to have a table, else it adds one. The simple solution is to provide the table and following content as Body instead of PostContent:
$s = ConvertTo-HTML -head $head -Body "<h1>Automated AD DS Health Check</h1>$b" 

Open in new window

BTW, you don't use Net.Mail.MailMessage anymore; instead, life gets easier with this cmdlet (replaces line 101 - 110):
Send-MailMessage -SmtpServer mailserver.mydomain.com -From Reports@mydomain.com -To user@mydomain.com `
  -Subject 'Active Directory Health Check Report' -Body $s -BodyAsHtml

Open in new window

0
 

Author Comment

by:siber1
ID: 40520410
hi Qlemo, thanks for the response. I added that line of code to the end of the script and the empty table from the top is now gone. however, there is now an empty table at the very bottom [below - "there were no replication errors"

should I be adding that line of code to multiple places in the script?

thanks,

S.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40520461
Didn't see that. The issue, obviously, is that the HTML tables are already built as HTML fragment code, and then sent thru ConvertTo-HTML again. Best to leave that additional step out, and built a simple HTML frame ourself.
$s = @"
<html>
$head
$b
</html>
"@

Open in new window

That should look much better now.
0
 

Author Comment

by:siber1
ID: 40520469
thanks Qlemo, where specifically would I put that updated code in the script?
0
 

Author Closing Comment

by:siber1
ID: 40520477
thank you so much Qlemo.

that worked perfectly.
much appreciated.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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