Solved

need assistance with powershell script

Posted on 2014-12-25
9
248 Views
Last Modified: 2014-12-25
hello, I'm trying to get this script on TechNet to work, can someone take a look?

the script runs but the replication report is not working, all I see in the replication section is *32

here is the link on TechNet:
https://gallery.technet.microsoft.com/AD-Daily-Health-Check-3d0932db/view/Discussions#content

here is  the script:
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}
 
 
$results = $results | where {$_.'Number of Failures' -gt 1 }
 
if ($results -ne $null ) {
    $results = $results | select "Source DSA", "Naming Context", "Destination DSA" ,"Number of Failures", "Last Failure Time", "Last Success Time", "Last Failure Status"
    $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 {
    $results = "There were no Replication Errors"
    $b+= $results | ConvertTo-HTML -Fragment  -PreContent "<h2>Replication Status</h2>" | Out-String
     
}




$head = @'
<style>
body { background-color:#dddddd;
       font-family:Tahoma;
       font-size:12pt; }
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>Active Directory Checklist</h1>" | Out-string


$emailFrom = "Itsupport01@examplexxyy.com" 
$emailTo = "SD@ssyyhe.com"

$smtpserver= "xxx.local" 
$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

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
  • 5
  • 4
9 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40517671
Do you mean this part?
$workfile = repadmin.exe /showrepl * /csv 
$results = ConvertFrom-Csv -InputObject $workfile | where {$_.'Number of Failures' -ge 1}
 
 
$results = $results | where {$_.'Number of Failures' -gt 1 }
 
if ($results -ne $null ) {
    $results = $results | select "Source DSA", "Naming Context", "Destination DSA" ,"Number of Failures", "Last Failure Time", "Last Success Time", "Last Failure Status"
    $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 {
    $results = "There were no Replication Errors"
    $b+= $results | ConvertTo-HTML -Fragment  -PreContent "<h2>Replication Status</h2>" | Out-String
     
}

Open in new window

I cannot see how this should result in "*32". Please test that part only.
0
 

Author Comment

by:siber1
ID: 40517686
Hi Qlemo, where would the output file be? I just ran that subset of code and it seemed to run but I don't see the output file to verify.

thx
0
 

Author Comment

by:siber1
ID: 40517721
hi Qlemo, are you still there? really appreciate your assistance with this, I ran the subset you requested, but it didn't produce any output file that I could share with you.  pls advise I'd really like to get this working.   thx
0
Office 365 Training for IT Pros

Learn how to provision Office 365 tenants, synchronize your on-premise Active Directory, and implement Single Sign-On.

 
LVL 70

Expert Comment

by:Qlemo
ID: 40517724
You can just type $b or $results (one of both)  to check the output.
0
 

Author Comment

by:siber1
ID: 40517735
when I type $b or $results - it does not return any results.  [empty]   thx
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40517844
So there is at most 1 replication failure. That part only starts reporting if more than one failure occurred.
This leads to the question, which part exactly should not work.  *32 is something I only met in Task Manager, for 32 bit applications on x64 systems.
0
 

Author Comment

by:siber1
ID: 40517854
should there not be a summary section for "replication status"?

I see a summary on the other sections, but for Replication summary here is what I see in the report:

*
32

thx - S.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40517958
You are right, the HTML output is as stated and wrong, but both $b and $results are not empty. $results is correct "There were no Replication Errors", but the HMTL output is wrong (number of characters instead of the string itself). This is a common mistake, and I didn't spot it earlier either.

Here is corrected and slightly reworked code (for the replication report only), as there was superfluous operations,
$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>"
}

Open in new window

0
 

Author Closing Comment

by:siber1
ID: 40517992
thank you Qlemo, that resolved the issue. brilliant as always.

S.
0

Featured Post

Office 365 Training for Admins

Learn how to provision tenants, synchronize on-premise Active Directory, and implement Single Sign-On with these master level course.  Only from Platform Scholar

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
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 …
This video discusses moving either the default database or any database to a new volume.

734 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