Last post Apr 09, 2013 08:10 AM by smirnov
None
0 Points
6 Posts
Apr 08, 2013 05:31 AM|pourdad.daneshmand|LINK
Hi Friends ,
I have a paorblem with AD . I can retreieve list of all computers from AD , But I need owner of every computer in AD.
In the properties of Computers I see Managed By tab . In this tab we set user of computer. for example :
We have a Computer with V001 name and users of this computer is David Arteta.
How can I get David Arteta(Owner of V001 computer) from AD in C# or how can I access to managed by information in C# ?
Thanks.
All-Star
35149 Points
9075 Posts
Apr 08, 2013 08:01 AM|smirnov|LINK
The value must be saved in the managedBy attribute and it should look like
managedBy=CN=John,OU=Users,OU=company,DC=myDomain,DC=com
Use LDAP Browser to check it.
Apr 09, 2013 01:50 AM|pourdad.daneshmand|LINK
Your hint is good.But how can I reteive this value :
I dont now !
Apr 09, 2013 02:34 AM|smirnov|LINK
I guess it must be similar to getting any other attribute. Something like this
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://******"); DirectorySearcher search = new DirectorySearcher(searchRoot); search.Filter = ("(objectClass=computer)"); search.PropertiesToLoad.Add("managedBy"); search.PropertiesToLoad.Add("distinguishedName"); search.PropertiesToLoad.Add("cn"); SearchResultCollection groups = search.FindAll(); foreach (SearchResult sr in groups) { Response.Write(sr.Properties["managedBy"][0].ToString()); }
Apr 09, 2013 03:30 AM|pourdad.daneshmand|LINK
Hi smirnov
Thanks for your attention. This code is not work for me.I got a error : your index must be non negative . ---> ["managedBy"][0].ToString()
But I find This Code from Codekeep :
public static string[] GetComputerDetails(string Domain, string HostName) 8 { 9 try 10 { 11 // if the domain is left blank, lets use the default domain 12 if (Domain.Length == 0) 13 Domain = (string)new DirectoryEntry("LDAP://RootDSE").Properties["defaultNamingContext"][0]; 14 15 DirectoryEntry Entry = new DirectoryEntry("LDAP://" + Domain); 16 DirectorySearcher Search = new DirectorySearcher(Entry); 17 Search.Filter = string.Format("(&(objectCategory=computer)(cn={0}))", HostName); 18 SearchResult Result = Search.FindOne(); 19 if (Result == null) 20 return null; 21 else 22 { 23 ArrayList Results = new ArrayList(); 24 foreach (string Key in ((Hashtable)Result.Properties.PropertyNames.SyncRoot).Keys ) 25 { 26 for (int i = 0; i < Result.Properties[Key].Count; i++) 27 Results.Add(Key + "[" + i + "]: " + Result.Properties[Key][i].ToString()); 28 } 29 return (string[])Results.ToArray(typeof(string)); 30 } 31 } 32 catch (Exception) 33 { 34 return null; 35 } 36 }
string[] Details = GetComputerDetails("DomainName", "HostName-->meanse Computer Name");Author : Kevin ShumaBest RegardsP.Daneshmand
Apr 09, 2013 08:10 AM|smirnov|LINK
pourdad.daneshmand This code is not work for me.I got a error : your index must be non negative
That's most likely mean that object has no managedBy property
Add some check before that
if (sr.Properties.Contains("managedBy")) Response.Write(sr.Properties["managedBy"][0].ToString()); else Response.Write("No owner");
Hope this helps.
None
0 Points
6 Posts
Get Name Of Commputers Owner From Active Directory
Apr 08, 2013 05:31 AM|pourdad.daneshmand|LINK
Hi Friends ,
I have a paorblem with AD . I can retreieve list of all computers from AD , But I need owner of every computer in AD.
In the properties of Computers I see Managed By tab . In this tab we set user of computer. for example :
We have a Computer with V001 name and users of this computer is David Arteta.
How can I get David Arteta(Owner of V001 computer) from AD in C# or how can I access to managed by information in C# ?
Thanks.
All-Star
35149 Points
9075 Posts
Re: Get Name Of Commputers Owner From Active Directory
Apr 08, 2013 08:01 AM|smirnov|LINK
The value must be saved in the managedBy attribute and it should look like
managedBy=CN=John,OU=Users,OU=company,DC=myDomain,DC=com
Use LDAP Browser to check it.
None
0 Points
6 Posts
Re: Get Name Of Commputers Owner From Active Directory
Apr 09, 2013 01:50 AM|pourdad.daneshmand|LINK
Your hint is good.But how can I reteive this value :
managedBy=CN=John,OU=Users,OU=company,DC=myDomain,DC=com
I dont now !
All-Star
35149 Points
9075 Posts
Re: Get Name Of Commputers Owner From Active Directory
Apr 09, 2013 02:34 AM|smirnov|LINK
I guess it must be similar to getting any other attribute. Something like this
None
0 Points
6 Posts
Re: Get Name Of Commputers Owner From Active Directory
Apr 09, 2013 03:30 AM|pourdad.daneshmand|LINK
Hi smirnov
Thanks for your attention. This code is not work for me.I got a error : your index must be non negative . ---> ["managedBy"][0].ToString()
But I find This Code from Codekeep :
All-Star
35149 Points
9075 Posts
Re: Get Name Of Commputers Owner From Active Directory
Apr 09, 2013 08:10 AM|smirnov|LINK
That's most likely mean that object has no managedBy property
Add some check before that
Hope this helps.