Note that there are some explanatory texts on larger screens.

plurals
  1. POC# bool not set correctly
    primarykey
    data
    text
    <p>I have some C# code that's attempting to do an LDAP search using a supplied computer name to determine whether or not the computer account is disabled. Most of this code was taken from <a href="https://stackoverflow.com/questions/591681/using-c-how-do-you-check-if-a-computer-account-is-disabled-in-active-directory">this SO question</a>. The code in the example link works great and correctly displays true if I go disable an account in AD and false on computers that are active. The issue is that I can't exactly use the code in the manner it was originally presented, it must be used in the manner I have pasted it below. The problem with the below code is that it's always returning false, it doesn't seem to matter what computer name you pass to it. I also realize that the foreach loop is probably unneeded since I'm only trying to find one computer.</p> <pre><code>using System; using System.DirectoryServices; namespace DynamicNamespace { public class DynamicClass { public System.Boolean DynamicMethod(System.Boolean IsDisabled, System.String ComputerName) { //the string should be your a DC(domain controller) const string ldap = "LDAP://server-name"; //DirectoryEntry is used for manipulating objects (users, computers) DirectoryEntry entry = new DirectoryEntry(ldap); //DirectorySearcher responds to a filter method for LDAP searches //http://www.tek-tips.com/faqs.cfm?fid=5667 has a decent query guide DirectorySearcher dSearch = new DirectorySearcher(entry); //SAM Account Name was showing a $ sign at one point, using * for wildcard dSearch.Filter = String.Format("samAccountName={0}*", ComputerName); dSearch.PropertiesToLoad.Add("samAccountName"); dSearch.PropertiesToLoad.Add("userAccountControl"); SearchResultCollection results = dSearch.FindAll(); foreach (SearchResult result in results) { int userAccountControl = Convert.ToInt32(result.Properties["userAccountControl"][0]); string samAccountName = Convert.ToString(result.Properties["samAccountName"][0]); bool disabled = ((userAccountControl &amp; 2) &gt; 0); if (disabled == false) { IsDisabled = false; } else { IsDisabled = true; } } return IsDisabled; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload