Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Win32API::Net::GetDCName returns error code 87 for remote servers

by dt667 (Acolyte)
on Mar 29, 2016 at 16:40 UTC ( #1159030=perlquestion: print w/replies, xml ) Need Help??
dt667 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get the name of the domain controller for a remote machine using Win32API::Net::GetDCName. It appears that the API is silently failing and so calling Win32::GetLastError results in error code 87 (ERROR_INVALID_PARAMETER). The command works fine if I supply my local computer name or the empty string, but always returns nothing for any remote computers. Has anyone had any luck getting the DC name for a remote computer?

Use Win32API::Net; my $server = "<insert remote server name here>"; my $domain = "<insert your domain name here>"; my $dcName; Win32API::Net::GetDCName($server,$domain,$dcName); unless (Win32::GetLastError()) { print "DC = $dcName.\n"; } else { print Win32::GetLastError(); }

Replies are listed 'Best First'.
Re: Win32API::Net::GetDCName returns error code 87 for remote servers
by BrowserUk (Pope) on Mar 29, 2016 at 18:48 UTC
    GetLastError results in error code 87 (ERROR_INVALID_PARAMETER).

    The MS description of the underlying function suggest that error 87 for that api means: "The name syntax is incorrect. This error is returned if the name specified in the servername parameter contains illegal characters. "

    It goes on to caution that whilst the server name can be in either DNS (microsoft.com) or Netbios (microsoft) form; the domain name can only be in the latter form.

    I'm guessing that you are supplying the server name in a format that the call doesn't expect. Hard to confirm as you feel the need to redact the name in your posts. If you posted a fictitious name that is in the same form as the real ones you are using, someone might be able to see the problem.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      For server, I have tried: "BW2012R2", "BW2012R2.TESTLAB". My domain is "TESTLAB"

        According to this netbios names are padded to 15 (or maybe that is 16, somewhat confusing) characters with spaces.

        My suggestion is to run the command nbstat command and see what it thinks the names are.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Win32API::Net::GetDCName returns error code 87 for remote servers
by Anonymous Monk on Mar 29, 2016 at 17:24 UTC
    How about you $dcName and GetLastError at the same time?

      I am assuming you mean print out $dcName and GetLastError. With $server = "", output is:

      dc = <insert dc name here> error code = 0

      With $server set to any remote system name, output is:

      dc = error code = 87
Re: Win32API::Net::GetDCName returns error code 87 for remote servers
by SimonPratt (Friar) on Mar 31, 2016 at 12:52 UTC

    The answer seems pretty straight-forward to me:

    Also, you cannot remote this function to a non-PDC server.

    Taken from MSDN docs

Re: Win32API::Net::GetDCName returns error code 87 for remote servers
by dasgar (Priest) on Apr 01, 2016 at 01:06 UTC

    Did a Google search for a line command utility to determine the domain controller for a system and the second answer at this link suggests using nltest. Although that documentation link on nltest indicates that it applies to Windows server OSes, I have found that command on Windows 7 and Windows 8.

    Of course, if you wanted to use nltest as alternate solution, you will need to run this on the remote system. For that, you may want to check out psexec, which is part of the SysInternals set of tools. Basically psexec will let you run commands on remote Windows based systems.

      The good news is nltest will work from the command line. The bad news is that it won't work from within perl. I've tried using

      @args = ("nltest", "\/server:BW2012R2", "\/dsgetdc:Testlab"); system(@args) == 0 or die "system @args failed: $?";

      Output:

      'nltest' is not recognized as an internal or external command, operabl +e program or batch file. system nltest /server:BW2012R2 /dsgetdc:Testlab failed: 256

      I've also tried to use IPC::Run and IPC::Cmd with similar results. How can I run the nltest cmd directly from the commandline, but not from within a perl script?

        use absolute path to nltest

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1159030]
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-06-21 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (117 votes). Check out past polls.

    Notices?