Powershell script assist

John Darby
John Darby used Ask the Experts™
I have a script which has annotations and I am not sure how to work the syntax. The code I pasted below is as far as I have gotten today. I would greatly appreciate your insight to the syntax and logic of this script!!!

The gist of the script I am looking to execute is this...

1. purge the local kerberos ticket cache
2. determine the name of the local domain
3. get a kerberos TGT
4. get the local AD site name
5. get a kerberos TGT
6. get a list of domain controllers in local site
7. request a kerberos ticket from each domain controller (kerblist get ldap/dcnamehere)
8. if request is successful for each domain controller return to run script again
9. if a request against a domain controller returns an ERROR output domain controllername and timestamp to file
10. continue down list of domain controllers and once complete, start again
11. set the file write for ERROR to append
#Purge local Kerb list
kerblist purge all

# Get Domain information  
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()   

#Get a Kerberos TicketGettingTicket (TGT)
kerbist get krbtgt/$domain.name

#Get AD Site Name
function Get-ADComputerSite(%COMPUTERNAME%)
$site = nltest /server:$ComputerName /dsgetsite
if($LASTEXITCODE -eq 0){ $site[0] }

#Grab all DC hostnames in site ($dcs)
$dcObj = [adsi]"LDAP://OU=domain controllers,dc=domain,dc=local"
$dcs = $dcObj.PSBase.Children | % { $_.name }
$dcs > .\localsitedclist.txt

#LDAP query against all DCs in site
function Get-Kerblist 
$kerblist = kerblist get ldap/$dcs

for /F %%s in (localsitedclist.txt) do Get-Kerblist

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010

It looks like you're good up to 6?

Discovery of DCs within a Site is via DNS. You would format a query like this:

nslookup -q=srv "_ldap._tcp.$SiteName._sites.$($domain.Name)"

Or you can use this:

$DCs = Get-Dns "_ldap._tcp.$SiteName._sites.$($domain.Name)" SRV | Select-Object -ExpandProperty Answer

Where Get-Dns comes from my module, here http://code.msdn.microsoft.com/dnsshell. The actual servername is held in the TargetName property in each of the values in $DCs.

If you were to look at the algorithm for DC discovery you'd see that the next step is to create a query for DCs in a non-site specific scope. e.g.

Get-Dns "_ldap._tcp.$($domain.Name)" SRV

Does that help?



Thanks Chris! The get-dns cmdlet is most excellent. The 3 options above are more than worth the price of admission!  

Thanks, Brother Chris!


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial