Solved

quick powershell question need to delete a table in the code

Posted on 2014-12-25
8
151 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Find out what you should include to make the best professional email signature for your organization.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…

770 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