Powershell Site DC enumeration within site

John Darby
John Darby used Ask the Experts™
on
I am trying to create a code snippet and had the good fortune to have Chris Dent help me with the query phrase. I am trying to parameterize this and putput to file. The goal being to have a variable $sitedcs which holds the query and the output file is overwritten with each execution of this code (i.e. not appended)

Can you give me input on the phrasing below and let me know if I can tighten it up or improve?
TIA!
JD
function Get-SiteDCs
	{
	$sitedcs = nslookup -q=srv "_ldap._tcp.$SiteName._sites.$($domain.Name)"
	if($LASTEXITCODE -eq 0){ $sitedcs[0] }
	}
	$sitedcs | ft fullname | out-file .\sitedcs.txt

Open in new window

Comment
Watch Question

Do more with

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

You'd have to parse the text return, perhaps like this:

$SiteDcs = ((nslookup -q=srv "_ldap._tcp.$SiteName._sites.$($domain.Name)") |
  Where-Object { $_ -Match 'svr hostname\s*=' }) -Replace '.*=\s*'

That'll give you a simple list or server names, it is possible to get the rest of the values, of course, but then I'll be encouraging you to use Get-Dns again ;)

Chris

Author

Commented:
That helped immensely, Chris! I tried to find the download for get-dns, but the link was not active on the site you sent me.

Author

Commented:
Great help! Thanks Chris!
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Chris DentPowerShell Developer
Top Expert 2010

Commented:

Author

Commented:
Small question on the install of the module? I tried placing the module in either of the path locations (below) and the Import-Module DnsShell isn't seeing it.

Is it simply to copy these three file to one of those locations and execute the import?
DNSShell.dll
DnsShell.Format.ps1xml
DnsShell.psd1

$Env:PSModulePath....
C:\Users\v-johnda.REDMOND\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
Chris DentPowerShell Developer
Top Expert 2010

Commented:

This one tends to be the best, it's the one I use most:

C:\Users\v-johnda.REDMOND\Documents\WindowsPowerShell\Modules

Extract the Zip archive from the link above into there, preserving the folder name, and its subfolders.

That should give you a structure like this:

Modules
     |  --  DnsShell
                  |  --  DnsShell.dll
                  |  --  DnsShell.Format.ps1xml
                  |  --  DnsShell.psd1
                  |  --  en-US
                              |  --  DnsShell.dll-help.xml

Once that's done you should be able to run this inside PowerShell:

Import-Module DnsShell

It may error because I haven't signed any of my files (I need to fix that at some point). You can have it ignore the error and continue with:

Set-ExecutionPolicy RemoteSigned

Chris

Author

Commented:
Thanks Chris. The directory is in the Env Path, but for some reason it is invisible to the cmdlet? Here is the response I get...

Import-Module : The specified module 'DnsShell' was not loaded because no valid
 module file was found in any module directory.
At line:1 char:14
+ Import-Module <<<<  DnsShell
    + CategoryInfo          : ResourceUnavailable: (DnsShell:String) [Import-M
   odule], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm
   ands.ImportModuleCommand
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Hmm I imagine that this doesn't list it either:


Get-Module -ListAvailable


Any chance you can screenshot the folder layout?

I suspect I may need to write some kind of installation script for my module :)

Chris

Author

Commented:
Hey Chris, here is the output from Get-Module -ListAvailable


PS C:\Windows\system32> Get-Module -ListAvailable

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Manifest   BitsTransfer              {}
Manifest   PSDiagnostics             {}
Manifest   TroubleshootingPack       {}
v-johnda-PSModulesDir.png
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Ahh thanks for that.

It's because it actually needs to sit in a folder called DnsShell. So you have:

Modules
    | - DnsShell
            | - Everything else you have above

I'll really have to see if I can make an installer for it, I was a bit blind to this aspect of getting it to work (I, of course, know exactly how to make it work :)).

Chris

Author

Commented:
Very nice...thanks 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