myITforum.com Community Forum myITforum.com Community Forum

Home  Forums  Blogs  Live Support chat  Search Articles  Wiki  FAQ  Email Lists  Register  Login  My Profile  Inbox  Address Book  My Subscription  My Forums 

Photo Gallery  Member List  Search  Calendars  FAQ  Ticket List  Log Out

All Forums RSS Feed Subscription:


           



Not able to display all fields

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> VB Script >> Not able to display all fields Page: [1]
Login
Message << Older Topic   Newer Topic >>
Not able to display all fields - 4/15/2008 1:57:59 PM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
 
I am trying to display all fields for a computer account.  In that scripts, I am able to display the name and localtion but not the distinguishedName and displayname.  Why???

'On Error Resume Next
Set objShell   = CreateObject("WScript.Shell")
strComputername = objShell.ExpandEnvironmentStrings("%computername%")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
   "SELECT Name, Location FROM 'LDAP://dc=xxx,dc=xxxx,dc=qc,dc=ca' " _
       & "WHERE objectClass='computer'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
   Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
   Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
   Wscript.Echo "displayName: " & objRecordSet.Fields("displayName").Value
   Wscript.Echo "distinguishedName: " & objRecordSet.Fields("distinguishedName").Value
     
   objRecordSet.MoveNext
Loop


_____________________________

François Racine
Technicien

Please rate my post :)
Post #: 1
RE: Not able to display all fields - 4/15/2008 2:20:43 PM   
mcarriere893


Posts: 3618
Score: 300
Joined: 4/12/2002
From: Manitoba, Canada
Status: offline
HINT: What's missing in this line:
SELECT Name, Location FROM 'LDAP://dc=xxx,dc=xxxx,dc=qc,dc=ca' " _

Answer: should be
SELECT Name, Location, displayname, distinguishedname FROM 'LDAP://dc=xxx,dc=xxxx,dc=qc,dc=ca' " _

< Message edited by mcarriere893 -- 4/15/2008 2:21:46 PM >


_____________________________

Mark Carriere
Microsoft MVP-SMS
www.SMSUG.ca

(in reply to fracine)
Post #: 2
RE: Not able to display all fields - 4/15/2008 3:30:30 PM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
Mark you are great!
How can I make that script querying only for a specific computer?

_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to mcarriere893)
Post #: 3
RE: Not able to display all fields - 4/15/2008 4:12:39 PM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
I changed the value you suggest but nothing is happening.

1. What is the good way to cut with _ the line:

"SELECT Name, Location, displayName, distinguishedName, objectGUID, objectSid, operatingSystem, operatingSystemServicePack sAMAccountName whenCreated FROM 'LDAP://dc=xxx,dc=xxxx,dc=qc,dc=ca' " _
      & "WHERE objectClass='computer'" 

2. As soon as I add: objectGUID, objectSid, sAMAccountName or whenCreated then the script start with no error but nothing is appearing at all.

3. What would be the good way to interrogate only a single computername?

The script:
On Error Resume Next
Set objShell   = CreateObject("WScript.Shell")
strComputername = objShell.ExpandEnvironmentStrings("%computername%")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
  "SELECT Name, Location, displayName, distinguishedName, objectGUID, objectSid, operatingSystem, operatingSystemServicePack sAMAccountName whenCreated FROM 'LDAP://dc=sct,dc=gouv,dc=qc,dc=ca' " _
      & "WHERE objectClass='computer'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
   Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
   Wscript.Echo "displayName: " & objRecordSet.Fields("displayName").Value
   Wscript.Echo "distinguishedName: " & objRecordSet.Fields("distinguishedName").Value
   Wscript.Echo "objectGUID: " & objRecordSet.Fields("objectGUID").Value
   Wscript.Echo "objectSid: " & objRecordSet.Fields("objectSid").Value
   Wscript.Echo "operatingSystem: " & objRecordSet.Fields("operatingSystem").Value
   Wscript.Echo "operatingSystemServicePack: " & objRecordSet.Fields("operatingSystemServicePack").Value
   Wscript.Echo "sAMAccountName: " & objRecordSet.Fields("sAMAccountName").Value
   Wscript.Echo "whenCreated: " & objRecordSet.Fields("whenCreated").Value
    
  objRecordSet.MoveNext
Loop

Thanks again!



_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to fracine)
Post #: 4
RE: Not able to display all fields - 4/15/2008 10:04:20 PM   
akaplan


Posts: 168
Score: 20
Joined: 4/22/2003
From: North Carolina
Status: offline
The line breaks looks like this:

"SELECT Name, Location, displayName, distinguishedName, objectGUID, objectSid, operatingSystem, " & _ operatingSystemServicePack, sAMAccountName whenCreated FROM 'LDAP://dc=xxx,dc=xxxx,dc=qc,dc=ca' " & _ "WHERE objectClass='computer' and name ='" & strComputername & "'" 

(in reply to fracine)
Post #: 5
RE: Not able to display all fields - 4/16/2008 5:53:01 AM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
Thanks.  I have very much to learn.

Any idea for my point 2 and 3?

_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to akaplan)
Post #: 6
RE: Not able to display all fields - 4/16/2008 9:02:00 AM   
mcarriere893


Posts: 3618
Score: 300
Joined: 4/12/2002
From: Manitoba, Canada
Status: offline
XXXXX = the machine to query against.

Note objSID, and ObjGUID are remmed out as I believe they come back as an array, so you would have to deal with them differently.

On Error Resume Next
Set objShell   = CreateObject("WScript.Shell")
strComputername = objShell.ExpandEnvironmentStrings("%computername%")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"SELECT Name, Location, displayName, distinguishedName, objectGUID, objectSid, " _
& "operatingSystem, operatingSystemServicePack, sAMAccountName, whenCreated " _
& "FROM 'LDAP://dc=hydro,dc=mb,dc=ca' WHERE objectClass='computer' " _
& "and name='XXXXX'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
  Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
  Wscript.Echo "displayName: " & objRecordSet.Fields("displayName").Value
  Wscript.Echo "distinguishedName: " & objRecordSet.Fields("distinguishedName").Value
' Wscript.Echo "objectGUID: " & objRecordSet.Fields("objectGUID").Value 
 ' Wscript.Echo "objectSid: " & objRecordSet.Fields("objectSid").Value
  Wscript.Echo "operatingSystem: " & objRecordSet.Fields("operatingSystem").Value
  Wscript.Echo "operatingSystemServicePack: " & objRecordSet.Fields("operatingSystemServicePack").Value
  Wscript.Echo "sAMAccountName: " & objRecordSet.Fields("sAMAccountName").Value
  Wscript.Echo "whenCreated: " & objRecordSet.Fields("whenCreated").Value
   
objRecordSet.MoveNext
Loop

_____________________________

Mark Carriere
Microsoft MVP-SMS
www.SMSUG.ca

(in reply to fracine)
Post #: 7
RE: Not able to display all fields - 4/16/2008 9:04:25 AM   
mcarriere893


Posts: 3618
Score: 300
Joined: 4/12/2002
From: Manitoba, Canada
Status: offline
Note that you were also missing a bunch of commas in your select statement , that you have to watch out for. Best to rem out the On error line.

_____________________________

Mark Carriere
Microsoft MVP-SMS
www.SMSUG.ca

(in reply to mcarriere893)
Post #: 8
RE: Not able to display all fields - 4/16/2008 9:45:08 AM   
akaplan


Posts: 168
Score: 20
Joined: 4/22/2003
From: North Carolina
Status: offline
You cannot get the SID or GUID straight out of LDAP.  Here is the revised script:
'=========
'On Error Resume Next
Set objShell   = CreateObject("WScript.Shell")
strComputername = objShell.ExpandEnvironmentStrings("%computername%")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

dim root, sADSPath
'Get the default ADsPath for the domain to search.
Set root = GetObject("LDAP://rootDSE")
sADSPath = root.Get("defaultNamingContext")

objCommand.CommandText = "SELECT Name, Location, displayName, distinguishedName," & _
"operatingSystem, operatingSystemServicePack," & _
"sAMAccountName, whenCreated FROM 'LDAP://"& sADSPath &"'" & _
" WHERE objectClass='computer'"

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 60
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
'objCommand.Properties("Size Limit") = 10   'debug, limit to 10 results
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
   Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
  Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
  Wscript.Echo "displayName: " & objRecordSet.Fields("displayName").Value
  strDN = objRecordSet.Fields("distinguishedName").Value
  Wscript.Echo "distinguishedName: " & strDN
  Wscript.Echo "objectGUID: " & GetGUID(strDN)
  Wscript.Echo "objectSid: " & GetSid(strDN)
  Wscript.Echo "operatingSystem: " & objRecordSet.Fields("operatingSystem").Value
  Wscript.Echo "operatingSystemServicePack: " & objRecordSet.Fields("operatingSystemServicePack").Value
  Wscript.Echo "sAMAccountName: " & objRecordSet.Fields("sAMAccountName").Value
  Wscript.Echo "whenCreated: " & objRecordSet.Fields("whenCreated").Value
objRecordSet.MoveNext
Loop

'most of these routines come from or are based on RL Mueller code, http://www.rlmueller.net/

Function GetSid(dn)
   Set objUser = GetObject("LDAP://" & dn )
   ' Retrieve SID and convert to hex string, then to decimal string.
   arrSid = objUser.objectSid
   strSidHex = OctetToHexStr(arrSid)
   GetSid = HexStrToDecStr(strSidHex)
End Function


Function GetGUID(dn)
   Set objUser = GetObject("LDAP://" & dn )
   arrbytGuid = objUser.objectGuid
   strHexGuid = OctetToHexStr(arrbytGuid)
   'Wscript.Echo "User Guid in hex string format: " & strHexGuid
  
   strGuid = HexGuidToGuidStr(strHexGuid)
   'Wscript.Echo "User Guid in display format: " & strGuid
   GetGUID = strGUID
End Function


Function OctetToHexStr(arrbytOctet)
   'Mueller
   ' Function to convert OctetString (byte array) to Hex string.
   Dim k
   OctetToHexStr = ""
   For k = 1 To Lenb(arrbytOctet)
       OctetToHexStr = OctetToHexStr _
           & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
   Next
End Function

Function HexStrToDecStr(strSid)
   'Mueller
   ' Function to convert hex Sid to decimal (SDDL) Sid.
   Dim arrbytSid, lngTemp, j
  
   ReDim arrbytSid(Len(strSid)/2 - 1)
       For j = 0 To UBound(arrbytSid)
       arrbytSid(j) = CInt("&H" & Mid(strSid, 2*j + 1, 2))
   Next
  
   HexStrToDecStr = "S-" & arrbytSid(0) & "-" _
       & arrbytSid(1) & "-" & arrbytSid(8)
  
   lngTemp = arrbytSid(15)
   lngTemp = lngTemp * 256 + arrbytSid(14)
   lngTemp = lngTemp * 256 + arrbytSid(13)
   lngTemp = lngTemp * 256 + arrbytSid(12)
  
   HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  
   lngTemp = arrbytSid(19)
   lngTemp = lngTemp * 256 + arrbytSid(18)
   lngTemp = lngTemp * 256 + arrbytSid(17)
   lngTemp = lngTemp * 256 + arrbytSid(16)
  
   HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  
   lngTemp = arrbytSid(23)
   lngTemp = lngTemp * 256 + arrbytSid(22)
   lngTemp = lngTemp * 256 + arrbytSid(21)
   lngTemp = lngTemp * 256 + arrbytSid(20)
  
   HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  
   lngTemp = arrbytSid(25)
   lngTemp = lngTemp * 256 + arrbytSid(24)
   HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
End Function


Function HexGuidToGuidStr(strGuid)
' Function to convert Hex Guid to display form.
Dim k

HexGuidToGuidStr = ""
For k = 1 To 4
   HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 9 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
   HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 13 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
   HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 17 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 17, 4)
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 21)
End Function

'=========
Alan
(like it? rate it)

(in reply to fracine)
Post #: 9
RE: Not able to display all fields - 4/17/2008 8:15:26 AM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
"SELECT Name, Location, displayName, distinguishedName, objectGUID, objectSid, " _ 
& "operatingSystem, operatingSystemServicePack, sAMAccountName, whenCreated " _ 
& "FROM 'LDAP://dc=hydro,dc=mb,dc=ca' WHERE objectClass='computer' " _ 
& "and name='XXXXX'"  


objCommand.CommandText = "SELECT Name, Location, displayName, distinguishedName," & _ 
"operatingSystem, operatingSystemServicePack," & _ 
"sAMAccountName, whenCreated FROM 'LDAP://"& sADSPath &"'" & _ 
" WHERE objectClass='computer'" 


I cannot test it right now but I suppose like Mark suggest I have to add:
"and name='XXXXX'" 


_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to akaplan)
Post #: 10
RE: Not able to display all fields - 4/17/2008 8:28:19 AM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
I have a problem with the and name='strComputername'", I got an error message on line 28.  How would I resolve this.  The job you did is simply incredible!

'On Error Resume Next
Set objShell   = CreateObject("WScript.Shell")
strComputername = objShell.ExpandEnvironmentStrings("%computername%")
Wscript.echo strComputername
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
dim root, sADSPath
'Get the default ADsPath for the domain to search.
Set root = GetObject("LDAP://rootDSE")
sADSPath = root.Get("defaultNamingContext")
objCommand.CommandText = "SELECT Name, Location, displayName, distinguishedName," & _
"operatingSystem, operatingSystemServicePack," & _
"sAMAccountName, whenCreated FROM 'LDAP://"& sADSPath &"'" & _
" WHERE objectClass='computer' and name='strComputername'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 60
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
'objCommand.Properties("Size Limit") = 10   'debug, limit to 10 results
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
  Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
Wscript.Echo "displayName: " & objRecordSet.Fields("displayName").Value
strDN = objRecordSet.Fields("distinguishedName").Value
Wscript.Echo "distinguishedName: " & strDN
Wscript.Echo "objectGUID: " & GetGUID(strDN)
Wscript.Echo "objectSid: " & GetSid(strDN)
Wscript.Echo "operatingSystem: " & objRecordSet.Fields("operatingSystem").Value
Wscript.Echo "operatingSystemServicePack: " & objRecordSet.Fields("operatingSystemServicePack").Value
Wscript.Echo "sAMAccountName: " & objRecordSet.Fields("sAMAccountName").Value
Wscript.Echo "whenCreated: " & objRecordSet.Fields("whenCreated").Value
objRecordSet.MoveNext
Loop
'most of these routines come from or are based on RL Mueller code, http://www.rlmueller.net/
Function GetSid(dn)
  Set objUser = GetObject("LDAP://" & dn )
  ' Retrieve SID and convert to hex string, then to decimal string.
  arrSid = objUser.objectSid
  strSidHex = OctetToHexStr(arrSid)
  GetSid = HexStrToDecStr(strSidHex)
End Function

Function GetGUID(dn)
  Set objUser = GetObject("LDAP://" & dn )
  arrbytGuid = objUser.objectGuid
  strHexGuid = OctetToHexStr(arrbytGuid)
  'Wscript.Echo "User Guid in hex string format: " & strHexGuid
 
  strGuid = HexGuidToGuidStr(strHexGuid)
  'Wscript.Echo "User Guid in display format: " & strGuid
  GetGUID = strGUID
End Function

Function OctetToHexStr(arrbytOctet)
  'Mueller
  ' Function to convert OctetString (byte array) to Hex string.
  Dim k
  OctetToHexStr = ""
  For k = 1 To Lenb(arrbytOctet)
      OctetToHexStr = OctetToHexStr _
          & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
  Next
End Function
Function HexStrToDecStr(strSid)
  'Mueller
  ' Function to convert hex Sid to decimal (SDDL) Sid.
  Dim arrbytSid, lngTemp, j
 
  ReDim arrbytSid(Len(strSid)/2 - 1)
      For j = 0 To UBound(arrbytSid)
      arrbytSid(j) = CInt("&H" & Mid(strSid, 2*j + 1, 2))
  Next
 
  HexStrToDecStr = "S-" & arrbytSid(0) & "-" _
      & arrbytSid(1) & "-" & arrbytSid(8)
 
  lngTemp = arrbytSid(15)
  lngTemp = lngTemp * 256 + arrbytSid(14)
  lngTemp = lngTemp * 256 + arrbytSid(13)
  lngTemp = lngTemp * 256 + arrbytSid(12)
 
  HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
 
  lngTemp = arrbytSid(19)
  lngTemp = lngTemp * 256 + arrbytSid(18)
  lngTemp = lngTemp * 256 + arrbytSid(17)
  lngTemp = lngTemp * 256 + arrbytSid(16)
 
  HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
 
  lngTemp = arrbytSid(23)
  lngTemp = lngTemp * 256 + arrbytSid(22)
  lngTemp = lngTemp * 256 + arrbytSid(21)
  lngTemp = lngTemp * 256 + arrbytSid(20)
 
  HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
 
  lngTemp = arrbytSid(25)
  lngTemp = lngTemp * 256 + arrbytSid(24)
  HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
End Function

Function HexGuidToGuidStr(strGuid)
' Function to convert Hex Guid to display form.
Dim k
HexGuidToGuidStr = ""
For k = 1 To 4
  HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 9 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
  HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 13 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
  HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 17 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 17, 4)
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 21)
End Function


_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to akaplan)
Post #: 11
RE: Not able to display all fields - 4/17/2008 8:39:18 AM   
mcarriere893


Posts: 3618
Score: 300
Joined: 4/12/2002
From: Manitoba, Canada
Status: offline
take the variable out of the quoted string like this -->

" WHERE objectClass='computer' and name='" & strComputername & "'" 

_____________________________

Mark Carriere
Microsoft MVP-SMS
www.SMSUG.ca

(in reply to fracine)
Post #: 12
RE: Not able to display all fields - 4/17/2008 9:07:33 AM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
OK it is working now!  Is it a way to find who create the computer account?

'On Error Resume Next
Set objShell   = CreateObject("WScript.Shell")
strComputername = objShell.ExpandEnvironmentStrings("%computername%")
Wscript.echo strComputername
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
dim root, sADSPath
'Get the default ADsPath for the domain to search.
Set root = GetObject("LDAP://rootDSE")
sADSPath = root.Get("defaultNamingContext")
objCommand.CommandText = "SELECT Name, Location, displayName, distinguishedName," & _
"operatingSystem, operatingSystemServicePack," & _
"sAMAccountName, whenCreated FROM 'LDAP://"& sADSPath &"'" & _
" WHERE objectClass='computer' and name='" & strComputername & "'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 60
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
'objCommand.Properties("Size Limit") = 10   'debug, limit to 10 results
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
Wscript.Echo "displayName: " & objRecordSet.Fields("displayName").Value
strDN = objRecordSet.Fields("distinguishedName").Value
Wscript.Echo "distinguishedName: " & strDN
Wscript.Echo "objectGUID: " & GetGUID(strDN)
Wscript.Echo "objectSid: " & GetSid(strDN)
Wscript.Echo "operatingSystem: " & objRecordSet.Fields("operatingSystem").Value
Wscript.Echo "operatingSystemServicePack: " & objRecordSet.Fields("operatingSystemServicePack").Value
Wscript.Echo "sAMAccountName: " & objRecordSet.Fields("sAMAccountName").Value
Wscript.Echo "whenCreated: " & objRecordSet.Fields("whenCreated").Value
objRecordSet.MoveNext
Loop
'most of these routines come from or are based on RL Mueller code, http://www.rlmueller.net/
Function GetSid(dn)
Set objUser = GetObject("LDAP://" & dn )
' Retrieve SID and convert to hex string, then to decimal string.
arrSid = objUser.objectSid
strSidHex = OctetToHexStr(arrSid)
GetSid = HexStrToDecStr(strSidHex)
End Function
Function GetGUID(dn)
Set objUser = GetObject("LDAP://" & dn )
arrbytGuid = objUser.objectGuid
strHexGuid = OctetToHexStr(arrbytGuid)
'Wscript.Echo "User Guid in hex string format: " & strHexGuid
strGuid = HexGuidToGuidStr(strHexGuid)
'Wscript.Echo "User Guid in display format: " & strGuid
GetGUID = strGUID
End Function
Function OctetToHexStr(arrbytOctet)
'Mueller
' Function to convert OctetString (byte array) to Hex string.
Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
    OctetToHexStr = OctetToHexStr _
        & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
Next
End Function
Function HexStrToDecStr(strSid)
'Mueller
' Function to convert hex Sid to decimal (SDDL) Sid.
Dim arrbytSid, lngTemp, j
ReDim arrbytSid(Len(strSid)/2 - 1)
    For j = 0 To UBound(arrbytSid)
    arrbytSid(j) = CInt("&H" & Mid(strSid, 2*j + 1, 2))
Next
HexStrToDecStr = "S-" & arrbytSid(0) & "-" _
    & arrbytSid(1) & "-" & arrbytSid(8)
lngTemp = arrbytSid(15)
lngTemp = lngTemp * 256 + arrbytSid(14)
lngTemp = lngTemp * 256 + arrbytSid(13)
lngTemp = lngTemp * 256 + arrbytSid(12)
HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
lngTemp = arrbytSid(19)
lngTemp = lngTemp * 256 + arrbytSid(18)
lngTemp = lngTemp * 256 + arrbytSid(17)
lngTemp = lngTemp * 256 + arrbytSid(16)
HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
lngTemp = arrbytSid(23)
lngTemp = lngTemp * 256 + arrbytSid(22)
lngTemp = lngTemp * 256 + arrbytSid(21)
lngTemp = lngTemp * 256 + arrbytSid(20)
HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
lngTemp = arrbytSid(25)
lngTemp = lngTemp * 256 + arrbytSid(24)
HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
End Function
Function HexGuidToGuidStr(strGuid)
' Function to convert Hex Guid to display form.
Dim k
HexGuidToGuidStr = ""
For k = 1 To 4
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 9 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 13 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 17 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 17, 4)
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 21)
End Function


< Message edited by fracine -- 4/17/2008 9:27:57 AM >


_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to mcarriere893)
Post #: 13
RE: Not able to display all fields - 4/17/2008 12:35:41 PM   
akaplan


Posts: 168
Score: 20
Joined: 4/22/2003
From: North Carolina
Status: offline
Function objOwner(dn)
dim objNtSecurityDescriptor
Set objUser = GetObject("LDAP://" & dn )
Set objNtSecurityDescriptor = objUser.Get("ntSecurityDescriptor")
objOwner = objNtSecurityDescriptor.Owner

End Function



(in reply to fracine)
Post #: 14
RE: Not able to display all fields - 4/17/2008 12:53:04 PM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
Great!
Will it be the owner or the creator?  Are they the same?
What are the values available to be query?
I am looking with AdExplorer but not finding the owner value.  Am I looking to the good location?

< Message edited by fracine -- 4/17/2008 12:58:35 PM >


_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to akaplan)
Post #: 15
RE: Not able to display all fields - 4/18/2008 8:18:38 AM   
akaplan


Posts: 168
Score: 20
Joined: 4/22/2003
From: North Carolina
Status: offline
The creator is the initial owner, and remains so unless ownership is changed.  I have never seen anyone change the ownership of AD objects .

Alan

(in reply to fracine)
Post #: 16
RE: Not able to display all fields - 4/18/2008 9:19:03 AM   
fracine


Posts: 1609
Score: 23
Joined: 11/17/2001
From: Québec, Canada
Status: offline
I just make it work on my network (bigger test) and noticed it is taking a little time to get the result.

1. It is a way to make it display the information for a specific computer faster?
2. Is the object owner, the creator of the computer account or if there is another object for the creator?


OK, I did not notice someone already answered to my second question.

3. If I run it during all computer setup and store the information locally for diagnostic purpose, how would react a network?

< Message edited by fracine -- 4/18/2008 9:20:46 AM >


_____________________________

François Racine
Technicien

Please rate my post :)

(in reply to fracine)
Post #: 17
RE: Not able to display all fields - 4/18/2008 9:57:37 AM   
akaplan


Posts: 168
Score: 20
Joined: 4/22/2003
From: North Carolina
Status: offline
If you are seaching for only one system, try commenting out page size and timout.  The defaults will be fine.  Also, search on objectCategory instead of objectClass, since objectClass is not an indexed property.  The impact on you DCs will be minimal.

Alan

(in reply to fracine)
Post #: 18
Page:   [1]
All Forums >> [Scripting Technologies] >> VB Script >> Not able to display all fields Page: [1]
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




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.309