Solved

quick powershell question need to delete a table in the code

Posted on 2014-12-25
8
141 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 16

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 41

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

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 68

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

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

760 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

19 Experts available now in Live!

Get 1:1 Help Now