Solved

quick powershell question need to delete a table in the code

Posted on 2014-12-25
8
173 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 43

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
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…

691 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