Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


ADSI Script to check Computer name and Domain

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2155
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> ADSI Script to check Computer name and Domain
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 ADSI Script to check Computer name and Domain - 2/24/2005 1:36:56 AM   
  aek

 

Posts: 29
Score: 0
Joined: 2/24/2005
From:
Status: offline
I need to create a script. The script will be deployed by SMS.

The script should run on my machine and check:
1) Is my machine a Computer?
2) What is the name of the Computer?
3) Who is the user logged on?
4) Which domain am I joined to?
5) Does a computer account exist for my computer name in AD?

How can I do that. I am new to ADSI any help is appreciated.

Thank you
 
 
Post #: 1
 
 Re: ADSI Script to check Computer name and Domain - 2/24/2005 9:00:23 AM   
  esnmb

 

Posts: 431
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
I'm still learning ADSI, but you can use VBScript.

Set objNetwork = Wscript.CreateObject("Wscript.Network")

Wscript.Echo objNetwork.ComputerName
Wscript.Echo objNetwork.UserDomain
Wscript.Echo objNetwork.UserName

(in reply to aek)
 
 
Post #: 2
 
 Re: ADSI Script to check Computer name and Domain - 2/24/2005 2:06:14 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Option Explicit
WScript.Echo "Machine Type : " & getComputerType(".")
WScript.Echo "Computer Name : " & getComputerInfo("computer")
WScript.Echo "Logged on Uesr : " & getComputerInfo("user")
WScript.Echo "User Domain : " & getComputerInfo("domain")
WScript.Echo "Computer Account in AD: " & isComputerAccountExists(getComputerInfo("computer"))

Function getComputerType(host)
Dim wmi, network, system, item, temp
Set wmi = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" & host & "\root\cimv2")
Set system = wmi.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each item In system
temp = item.SystemType
Next
getComputerType = temp
End Function

Function getComputerInfo(info)
Dim network
Set network = CreateObject("WScript.Network")
Select Case LCase(info)
Case "computer"
getComputerInfo = network.ComputerName
Case "user"
getComputerInfo = network.UserName
Case "domain"
getComputerInfo = network.UserDomain
End Select
End Function

Function isComputerAccountExists(host)
Dim conn, cmd , rs
Set conn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conn.provider = "adsdsoobject"
conn.open "active directory provider"
cmd.activeconnection = conn
cmd.commandtext = "<LDAP://" & GetObject("LDAP://rootdse").Get("defaultnamingcontext") & ">;(&(objectcategory=computer)(objectclass=computer)(cn=" & host & "));cn;subtree"
Set rs = cmd.Execute
If rs.recordcount = 0 Then
isComputerAccountExists = False
Else
isComputerAccountExists = True
End If
End Function

(in reply to aek)
 
 
Post #: 3
 
 Re: ADSI Script to check Computer name and Domain - 2/25/2005 12:59:32 AM   
  aek

 

Posts: 29
Score: 0
Joined: 2/24/2005
From:
Status: offline
Thank you folks for the help. The script works great.

Token..a question...!

In my WinPE environment, I used your code "to test if the machine exists in a specific AD domain" but could not query other domains. What is the best way to use credentials? Are there specific requirements to query other domains.

To make it clearer...I have a build up process to stage new machines and during the build process the user selects division, domain, name that generates a full computername (by adding div,domain suffixes and likes). The computername (generated automatically above) should already exist in AD Domain ..or else the user should not be able to proceed with the build up process.

I used your code and replaced "host" with the "computername" generated based on user selections during my WinPE environment. I was able to to test whether the computername exists or not.. on only one domain...which is my default production domain. How do I test it for.. like a test domain or other domains.

When I tried to test building a new machine..but selecting a different domain..I got a "table does not exist"..kind of message. I tried adding credentials using
Conn.Properties("User ID") = "mydomain\loginname"
Conn.Properties("Password") = "password"

but that also did not work.

Should I create an account that should have read access to all the domains and include that username and password in the conn.properties? or is there a better way..?

I hope it is not too confusing.

Thanks in Advance

(in reply to aek)
 
 
Post #: 4
 
 Re: ADSI Script to check Computer name and Domain - 2/25/2005 2:00:24 AM   
  mbouchard


Posts: 1804
Score: 12
Joined: 5/15/2003
From: USA
Status: online
I too use PE and was wondering if you could post the code that you use to check for the PC in AD?

Thanks,

(in reply to aek)
 
 
Post #: 5
 
 Re: ADSI Script to check Computer name and Domain - 2/25/2005 2:07:21 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
For simplicity, replace the line below:

cmd.commandtext = "<LDAP://" & GetObject("LDAP://rootdse").Get("defaultnamingcontext") & ">;(&(objectcategory=computer)(objectclass=computer)(cn=" & host & "));cn;subtree"

with:
dim domain
domain = "dc=div1,dc=testDomain,dc=com"
cmd.commandtext = "<LDAP://" & domain & ">;(&(objectcategory=computer)(objectclass=computer)(cn=" & host & "));cn;subtree"

If you intend to do something else with it, there ways of getting data etc, but it should suffice for your intention.

(in reply to aek)
 
 
Post #: 6
 
 Re: ADSI Script to check Computer name and Domain - 2/25/2005 3:24:51 AM   
  aek

 

Posts: 29
Score: 0
Joined: 2/24/2005
From:
Status: offline
mbouchard:
We are using something similar to Token's code. The only difference is that we are also checking a value: lastlogontimestamp to check if the machine has been active on the network or not.

Token: thanks
I used the code and replaced domain with the domain that I wanted to search in, but I got the error:
Error: Table does not exist.
Code: 80040E37
Source: Provider

I am positive it is something about credentials. My AD guy said that if I am authenticated user in one domain, I dont have other rights in other domains..but I should be able to query other domains.

But that does not seem to be working for me..What do you think? Shouls I be logged on in the other domain to be able to query that domain..hmmmm....something is not right...!

(in reply to aek)
 
 
Post #: 7
 
 Re: ADSI Script to check Computer name and Domain - 2/25/2005 5:56:55 AM   
  mbouchard


Posts: 1804
Score: 12
Joined: 5/15/2003
From: USA
Status: online
Thanks, will need to try that out.

(in reply to aek)
 
 
Post #: 8
 
 Re: ADSI Script to check Computer name and Domain - 2/25/2005 8:55:49 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
It is your credentials, or rather, an permission or trust relationship issue. I just ran the code and it returns whether my computer in another domain (which it is not) without any problems.

(in reply to aek)
 
 
Post #: 9
 
 Re: ADSI Script to check Computer name and Domain - 3/8/2005 11:31:16 AM   
  robszar


Posts: 173
Score: 0
Joined: 2/27/2005
From:
Status: offline
I am running into the same situation as above, using WinPE I cannot create a computer account in AD, can I enter a userid & password while commecting to AD at end of this string:
GetObject("LDAP://cn=Computers,dc=NA,dc=fabrikam,dc=com")

also post-ghost

the machine boots up as a member of a workgroup, a script runs that finds the computer account in AD, renames the computer and joins to the domain. I am getting Error: Table does not exist. when searching AD for the computer account.

when joining the domain I can use
runas /netonly / user:
to run the script and that works but then you have to enter the password manually, which sucks but works. runas doesn't seem to work in WinPE.

(in reply to aek)
 
 
Post #: 10
 
 Re: ADSI Script to check Computer name and Domain - 3/8/2005 1:28:55 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
What is WinPE ? and what does it have anything to do with the scripts I posted ?

and at what line did the error occur in my script ?

I can use the script without any problem when doing queries across domain boundaries.

Thanks.

(in reply to aek)
 
 
Post #: 11
 
 Re: ADSI Script to check Computer name and Domain - 3/8/2005 2:09:44 PM   
  robszar


Posts: 173
Score: 0
Joined: 2/27/2005
From:
Status: offline
Windows Preinstallation Environment, it's an OS on CD used to load ghost images, so the machine bootWindows Preinstallation environment, it's an OS on CD used to load ghost images, so the machine boots up and generates a temp computer name and is not on the domain. The issue with the script is you are not logged in with domain credentials so the AD query doesnÒt work without a username and password with domain authentication. The error occurs when you try and execute the query. Is there a way to embed a user name and password? Your script is solid when ran from a computer that is a part of the domain.

Function isComputerAccountExists(host)
Dim conn, cmd , rs
Set conn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conn.provider = "adsdsoobject"
conn.open "active directory provider"
cmd.activeconnection = conn
cmd.commandtext = "<LDAP://" & GetObject("LDAP://rootdse").Get("defaultnamingcontext") & ">;(&(objectcategory=computer)(objectclass=computer)(cn=" & host & "));cn;subtree"
Set rs = cmd.Execute<---------------This is where it bombs
If rs.recordcount = 0 Then
isComputerAccountExists = False
Else
isComputerAccountExists = True
End If
End Function

(in reply to aek)
 
 
Post #: 12
 
 Re: ADSI Script to check Computer name and Domain - 3/8/2005 2:41:01 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
ah, now I finally got it :)

Sure, just add the following lines:

conn.properties("user id") = "domain\userID"
conn.properties("password") = "password"

and replace the old cmd text string with the following:

cmd.commandtext = "<LDAP://DC_Name/dc=subdomain,dc=mydomain,dc=com>;(&(objectcategory=computer)(objectclass=computer)(cn=" & host & "));cn;subtree"

where DC_name is the actual NetBIOS name for a DC and the rest should be self explainatory.

(in reply to aek)
 
 
Post #: 13
 
 Re: ADSI Script to check Computer name and Domain - 3/9/2005 1:26:57 AM   
  robszar


Posts: 173
Score: 0
Joined: 2/27/2005
From:
Status: offline
That is perferct for the Post-Ghost process, I was missing the DC, once I put that in it worked.
1 more question,
when I boot up with the Windows PE and try and add the Computer account I can't get connected to AD, this is the string I'm using to connect:
Set objContainer = GetObject(provider & computerOU)
can I use ADO to add the computer account to AD or can I use a username and password doing it this way?
THanks!

(in reply to aek)
 
 
Post #: 14
 
 Re: ADSI Script to check Computer name and Domain - 3/9/2005 6:11:46 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
You can't use ADO to add computer accounts, you will have to bind to the container object explicitly using the following method:

Set root = GetObject("LDAP:")
Set container = root.OpenDSObject("LDAP://SERVER/OU=....DC=....", "userID", "password",200)

(in reply to aek)
 
 
Post #: 15
 
 Re: ADSI Script to check Computer name and Domain - 3/9/2005 8:36:26 AM   
  robszar


Posts: 173
Score: 0
Joined: 2/27/2005
From:
Status: offline
Thanks again Token, Works great

(in reply to aek)
 
 
Post #: 16
 
 Re: ADSI Script to check Computer name and Domain - 3/9/2005 9:17:25 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
No problem. Glad I could help.

(in reply to aek)
 
 
Post #: 17
 
 Re: ADSI Script to check Computer name and Domain - 3/10/2005 2:44:56 AM   
  aek

 

Posts: 29
Score: 0
Joined: 2/24/2005
From:
Status: offline
Token:

Adding on the issue where you added the DC name in the query to query for a machine in the WINPE environment.

My Win PE environment is not in one area. So one user could be building a machine using the same WinPE Cd in Thailand, or Japan. In that case, what would be the best way to look for the nearest DC in that domain and put that value in the LDAP query?

Should I use WMI Query and query the domainrole vlaue for each machine in that domain, and then based on the domainrole, find out which is DC and then substitute that value in the query or is there a easier way to find out name of the DC in a domain?

Thanks

(in reply to aek)
 
 
Post #: 18
 
 Re: ADSI Script to check Computer name and Domain - 3/10/2005 4:17:00 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
The only thing that comes to mind is that you can probably get the IP address of the local machine and compare it against the subnet objects and determine which site the client box belongs to. Of course, this assumes that there is at least one DC in each site.

Since the client box isn't part of the domain when it boots up, I don't see how it could determine which DC to use.

I don't understand why determing the DC computer name within a domain is going to help, could you epxlain it a little bit more ? And how many domains and sites do you have in your organization ?

(in reply to aek)
 
 
Post #: 19
 
 Re: ADSI Script to check Computer name and Domain - 3/10/2005 5:11:08 AM   
  mbouchard


Posts: 1804
Score: 12
Joined: 5/15/2003
From: USA
Status: online
quote:
Originally posted by token

What is WinPE ?


Here is some more info on WinPE.
http://www.microsoft.com/licensing/programs/sa/support/winpe.mspx
It is basically a stripped down version of XP minus the GUI, that as robszar said, boots from a CD.

(in reply to aek)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> ADSI Script to check Computer name and Domain Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts