Solved

Is there a script or prog which can says if a sever is belong to a domain in AD?

Posted on 2011-02-14
10
534 Views
Last Modified: 2013-03-09
Is there a script or prog which can says if a sever is belong to a domain in AD?

For exemple, I have a list of servers in a file and would like to know in which domain they are belong in AD.

Thank you.
0
Comment
Question by:SAM2009
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Accepted Solution

by:
chandra_darbha earned 500 total points
Comment Utility
Found this code at http://blog.dotsmart.net/2009/03/11/getting-a-machine%E2%80%99s-netbios-domain-name-in-csharp/ and modified it to take machine name as input parameter. Invoke the NetUtil.GetMachineNetBiosDomain("machinename"); from your code.

Hope that helps.

class NetUtil
{
    [DllImport("netapi32.dll", CharSet = CharSet.Auto)]
    static extern int NetWkstaGetInfo(string server,
        int level,
        out IntPtr info);

    [DllImport("netapi32.dll")]
    static extern int NetApiBufferFree(IntPtr pBuf);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    class WKSTA_INFO_100
    {
        public int wki100_platform_id;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string wki100_computername;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string wki100_langroup;
        public int wki100_ver_major;
        public int wki100_ver_minor;
    }

    public static string GetMachineNetBiosDomain(string machineName)
    {
        IntPtr pBuffer = IntPtr.Zero;

        WKSTA_INFO_100 info;
        int retval = NetWkstaGetInfo(machineName, 100, out pBuffer);
        if (retval != 0)
            throw new Win32Exception(retval);

        info = (WKSTA_INFO_100)Marshal.PtrToStructure(pBuffer, typeof(WKSTA_INFO_100));
        string domainName = info.wki100_langroup;
        NetApiBufferFree(pBuffer);
        return domainName;
    }
}

Open in new window


Thanks,
Chandra
0
 
LVL 3

Expert Comment

by:chandra_darbha
Comment Utility
Sorry, haven't noticed that you wanted a VB script. This is a C# implementation. There should be an equivalent implementation in VB.

Thanks,
Chandra
0
 
LVL 12

Expert Comment

by:Amick
Comment Utility
To determine whether a given computer in a list of computers is an active member of a given domain you can invoke
for /f %i in (serverfile.txt) do net view /domain:mydomain %i

Open in new window

The response is either a list of available shares which verifies membership, or a message that the network path was not found which suggests non-membership or out-of-service.

I realize this isn't exactly the solution you wanted, but it may be useful.
0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
Comment Utility
Try with PowerShell (Quest cmdlets for 2003 AD)
http://www.quest.com/powershell/activeroles-server.aspx

Create text file with servers on C-Drive (one server name per line) and run in PowerShell

 
Get-Content c:\servers.txt | ForEach-Object {Get-QADComputer $_ | Select name,domain | Export-CSV c:\output.csv}

Open in new window


Regards,
Krzysztof
0
 
LVL 1

Author Comment

by:SAM2009
Comment Utility
If I have many domains in AD does it works also?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 39

Expert Comment

by:Krzysztof Pytko
Comment Utility
yes, but you need to change a little bit search node

 
Get-Content c:\servers.txt | ForEach-Object {Get-QADComputer -SearchRoot "dc=domain,dc=com" $_ | Select name,domain | Export-CSV c:\output_DomainName.csv}

Open in new window


where dc=domain,dc=com is FQDN of your domain and you need to specify output file name

Krzysztof
0
 
LVL 1

Author Comment

by:SAM2009
Comment Utility
Hhumm it just give me the last server on the list with default domain name. Could you try?
0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
Comment Utility
Yes, because you need to run this script for each domain separately :) So, text file with server must be from the same domain :)

Krzysztof
0
 
LVL 1

Author Comment

by:SAM2009
Comment Utility
That what I did ok I will try one more time later and let see.
0
 
LVL 1

Author Closing Comment

by:SAM2009
Comment Utility
Thank you
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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 …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now