Solved

quick powershell question need to delete a table in the code

Posted on 2014-12-25
8
163 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
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 42

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 69

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 69

Accepted Solution

by:
Qlemo earned 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In-place Upgrading Dirsync to Azure AD Connect
how to add IIS SMTP to handle application/Scanner relays into office 365.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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