Great idea... I modified the script a bit - since I saw similar issues as Stephan did... and came up with this - should be easier to read as well..
param(
[string]$DCName = "", #will be set to the current logonserver if not set
[int]$Port = 636 #636 is default for LDAPS / 389 is default for regular LDAP
)
#this script will target a single DC and show if the connection was successful
If ($DCName.Length -gt 0) {
$TargetDC = $DCName;
} Else {
#$TempDC = [System.DirectoryServices.ActiveDirectory.Domain]::getCurrentDomain().DomainControllers | Select -First 1;
#$TargetDC = $TempDC.Name
#you could also use environment variables and grab the logon server - what would be closer then the first DC - depending on the size of your network the above attempt might be an issue
$TargetDC = [string]($env:LOGONSERVER).Replace("\\","")
}
#we build our connection string...
$LDAPS = [adsi]"LDAP://$($TargetDC):$($Port)";
#let's try to connect to LDAP via the specified port..
$Connection = "";
Try {
$Connection = [adsi]($LDAPS)
} Catch {}
If ($Connection.Path.Length -gt 0) {
$ResultText = $Connection.Path + " " + $Connection.distinguishedName;
$Success = 1;
} Else {
$Success = 0;
}
$XML = "
<prtg>
<result>
<channel>" + $TargetDC + ":" + $Port + "</channel>
<value>$Success</value>
</result>
<text>$ResultText</text>
</prtg>"
Function WriteXmlToScreen ([xml]$xml) #just to make it clean XML code...
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
WriteXmlToScreen "$XML"
The TRY/CATCH seems to hick up a bit - I never so the CATCH but I didn't care to much since there was an easy way to bypass this and move forward with the script while still getting the results I was looking for...
Note that I change especially the way the DC was detected - I didn't want to go 20 thousand miles around to globe cause my first DC listed was there..
Regards
Florian Rossmark
www.it-admins.com
Add comment