ADSI C ++を使用してOUからすべてのコンピュータをフィルタ -- c++ フィールド と ldap-query フィールド と adsi フィールド 関連 問題

ADSI Filter All computers from an OU using c++












1
vote

問題

日本語

C ++に新しい新しいもの。 msdn記事 IAMを試してみるADSIを使用してドメイン内のOUの下のすべてのコンピュータを取得する。しかし、IAMは8254L(filter_unknown)エラーを取得します。 IAMはここで何が悪いのかわからない。フィルタを更新しようとしましたが、エラーの変更はありません。ここで何が悪いのかを提案してください。

はリストを取得するためのコードです。

<事前> <コード> // ConsoleApplication3.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <objbase.h> #include <wchar.h> #include <activeds.h> #include <sddl.h> #include <comutil.h> #include <string.h> #include <stdio.h> HRESULT FindComputers(IDirectorySearch *pContainerToSearch, // IDirectorySearch pointer to the container to search. LPOLESTR szFilter, // Filter to find specific users. // NULL returns all user objects. LPOLESTR *pszPropertiesToReturn, // Properties to return for user objects found. // NULL returns all set properties. BOOL bIsVerbose // TRUE indicates that display all properties for the found objects. // FALSE indicates that only the RDN. ); // Entry point for the application. void wmain(int argc, wchar_t *argv[]) { // Initialize COM. CoInitialize(NULL); HRESULT hr = S_OK; // Get rootDSE and the current user domain container distinguished name. IADs *pObject = NULL; IDirectorySearch *pContainerToSearch = NULL; LPOLESTR szPath = new OLECHAR[MAX_PATH]; BOOL bReturnVerbose = FALSE; DWORD dwLength = MAX_PATH * 2; LPOLESTR pszBuffer = new OLECHAR[dwLength]; VARIANT var; hr = ADsOpenObject(L"LDAP://rootDSE", NULL, NULL, ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. IID_IADs, (void**)&pObject); if (FAILED(hr)) { wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE. "); if (pObject) pObject->Release(); return; } if (SUCCEEDED(hr)) { hr = pObject->Get(_bstr_t("defaultNamingContext"), &var); if (SUCCEEDED(hr)) { wprintf(L"bstrVal: %s ", var.bstrVal); // Build path to the domain container. // wcsncpy_s(szPath, L"LDAP://", MAX_PATH); // wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath)); //hr = ADsOpenObject(szPath, hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=Infosys,DC=iaseries,Dc=local", NULL, NULL, ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. IID_IDirectorySearch, (void**)&pContainerToSearch); if (SUCCEEDED(hr)) { hr = FindComputers(pContainerToSearch, // IDirectorySearch pointer to domainDNS container. pszBuffer, NULL, // Return all properties. bReturnVerbose ); if (SUCCEEDED(hr)) { if (S_FALSE == hr) wprintf(L"Computer object cannot be found. "); } else if (E_ADS_INVALID_FILTER == hr) wprintf(L"Cannot execute query. Invalid filter was specified. "); else wprintf(L"Query failed to run. HRESULT: %x ", hr); } else { wprintf(L"Cannot execute query. Cannot bind to the container. "); } if (pContainerToSearch) pContainerToSearch->Release(); } VariantClear(&var); } if (pObject) pObject->Release(); // Uninitialize COM. CoUninitialize(); delete[] szPath; delete[] pszBuffer; getchar(); } HRESULT FindComputers(IDirectorySearch *pContainerToSearch, // IDirectorySearch pointer to the container to search. LPOLESTR szFilter, // Filter for finding specific users. // NULL returns all user objects. LPOLESTR *pszPropertiesToReturn, // Properties to return for user objects found. // NULL returns all set properties. BOOL bIsVerbose // TRUE indicates that all properties for the found objects are displayed. // FALSE indicates only the RDN. ) { if (!pContainerToSearch) return E_POINTER; DWORD dwLength = (MAX_PATH * 2)+100; // Create search filter. LPOLESTR pszSearchFilter = new OLECHAR[dwLength]; // Add the filter. //swprintf_s(pszSearchFilter, dwLength, L"((objectClass=Computer)%s)", szFilter); swprintf_s(pszSearchFilter, dwLength, L"(&(objectClass=*)(objectCategory=Computer)%s)", szFilter); // Specify subtree search. ADS_SEARCHPREF_INFO SearchPrefs; SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE; SearchPrefs.vValue.dwType = ADSTYPE_INTEGER; SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE; DWORD dwNumPrefs = 1; // COL for iterations. LPOLESTR pszColumn = NULL; ADS_SEARCH_COLUMN col; HRESULT hr = S_OK; // Interface Pointers IADs *pObj = NULL; IADs * pIADs = NULL; // Search handle. ADS_SEARCH_HANDLE hSearch = NULL; // Set search preference. hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs); if (FAILED(hr)) return hr; LPOLESTR pszBool = NULL; DWORD dwBool = 0; PSID pObjectSID = NULL; LPOLESTR szSID = NULL; LPOLESTR szDSGUID = new WCHAR[39]; LPGUID pObjectGUID = NULL; SYSTEMTIME systemtime; DATE date; VARIANT varDate; LPOLESTR *pszPropertyList = NULL; LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" }; LPOLESTR szName = new OLECHAR[MAX_PATH]; LPOLESTR szDN = new OLECHAR[MAX_PATH]; VariantInit(&varDate); int iCount = 0; DWORD x = 0L; if (!bIsVerbose) { // Return non-verbose list properties only. hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, pszNonVerboseList, sizeof(pszNonVerboseList) / sizeof(LPOLESTR), &hSearch ); } else { if (!pszPropertiesToReturn) { // Return all properties. hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, NULL, (DWORD)-1, &hSearch ); } else { // Specified subset. pszPropertyList = pszPropertiesToReturn; // Return specified properties. hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, pszPropertyList, sizeof(pszPropertyList) / sizeof(LPOLESTR), &hSearch ); } } if (SUCCEEDED(hr)) { // Call IDirectorySearch::GetNextRow() to retrieve the next data row. hr = pContainerToSearch->GetFirstRow(hSearch); if (SUCCEEDED(hr)) { while (hr != S_ADS_NOMORE_ROWS) { // Keep track of count. iCount++; if (bIsVerbose) wprintf(L"---------------------------------- "); // Loop through the array of passed column names, // print the data for each column. while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS) { hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col); if (SUCCEEDED(hr)) { // Print the data for the column and free the column. if (bIsVerbose) { // Get the data for this column. wprintf(L"%s ", col.pszAttrName); switch (col.dwADsType) { case ADSTYPE_DN_STRING: for (x = 0; x< col.dwNumValues; x++) { wprintf(L" %s ", col.pADsValues[x].DNString); } break; case ADSTYPE_CASE_EXACT_STRING: case ADSTYPE_CASE_IGNORE_STRING: case ADSTYPE_PRINTABLE_STRING: case ADSTYPE_NUMERIC_STRING: case ADSTYPE_TYPEDNAME: case ADSTYPE_FAXNUMBER: case ADSTYPE_PATH: for (x = 0; x< col.dwNumValues; x++) { wprintf(L" %s ", col.pADsValues[x].CaseIgnoreString); } break; case ADSTYPE_BOOLEAN: for (x = 0; x< col.dwNumValues; x++) { dwBool = col.pADsValues[x].Boolean; pszBool = dwBool ? L"TRUE" : L"FALSE"; wprintf(L" %s ", pszBool); } break; case ADSTYPE_INTEGER: for (x = 0; x< col.dwNumValues; x++) { wprintf(L" %d ", col.pADsValues[x].Integer); } break; case ADSTYPE_OCTET_STRING: if (_wcsicmp(col.pszAttrName, L"objectSID") == 0) { for (x = 0; x< col.dwNumValues; x++) { pObjectSID = (PSID)(col.pADsValues[x].OctetString.lpValue); // Convert SID to string. ConvertSidToStringSid(pObjectSID, &szSID); wprintf(L" %s ", szSID); LocalFree(szSID); } } else if ((_wcsicmp(col.pszAttrName, L"objectGUID") == 0)) { for (x = 0; x< col.dwNumValues; x++) { // Cast to LPGUID. pObjectGUID = (LPGUID)(col.pADsValues[x].OctetString.lpValue); // Convert GUID to string. ::StringFromGUID2(*pObjectGUID, szDSGUID, 39); // Print the GUID. wprintf(L" %s ", szDSGUID); } } else wprintf(L" Value of type Octet String. No Conversion."); break; case ADSTYPE_UTC_TIME: for (x = 0; x< col.dwNumValues; x++) { systemtime = col.pADsValues[x].UTCTime; if (SystemTimeToVariantTime(&systemtime, &date) != 0) { // Pack in variant.vt. varDate.vt = VT_DATE; varDate.date = date; VariantChangeType(&varDate, &varDate, VARIANT_NOVALUEPROP, VT_BSTR); wprintf(L" %s ", varDate.bstrVal); VariantClear(&varDate); } else wprintf(L" Could not convert UTC-Time. ", pszColumn); } break; case ADSTYPE_NT_SECURITY_DESCRIPTOR: for (x = 0; x< col.dwNumValues; x++) { wprintf(L" Security descriptor. "); } break; default: wprintf(L"Unknown type %d. ", col.dwADsType); } } else { #ifdef _MBCS // Verbose handles only the two single-valued attributes: cn and ldapdisplayname, // so this is a special case. if (0 == wcscmp(L"name", pszColumn)) { //wcscpy_s(szName, col.pADsValues->CaseIgnoreString); szName = col.pADsValues->CaseIgnoreString; } if (0 == wcscmp(L"distinguishedName", pszColumn)) { //wcscpy_s(szDN, col.pADsValues->CaseIgnoreString); szDN = col.pADsValues->CaseIgnoreString; } #endif _MBCS } pContainerToSearch->FreeColumn(&col); } FreeADsMem(pszColumn); } if (!bIsVerbose) wprintf(L"%s DN: %s ", szName, szDN); // Get the next row. hr = pContainerToSearch->GetNextRow(hSearch); } } // Close the search handle to cleanup. pContainerToSearch->CloseSearchHandle(hSearch); } if (SUCCEEDED(hr) && 0 == iCount) hr = S_FALSE; delete[] szName; delete[] szDN; delete[] szDSGUID; delete[] pszSearchFilter; return hr; }
英語

Iam new to c++. Using the help of MSDN article iam trying to fetch all the computers under a OU in domain using ADSI. But Iam getting 8254L (FILTER_UNKNOWN) error. Iam not sure what iam doing wrong here. I tried to update the filter but no change in the error. Please suggest what iam doing wrong here.

Below is the code am using for getting the list.

// ConsoleApplication3.cpp : Defines the entry point for the console application. //  #include "stdafx.h" #include <objbase.h> #include <wchar.h> #include <activeds.h> #include <sddl.h> #include <comutil.h> #include <string.h> #include <stdio.h>   HRESULT FindComputers(IDirectorySearch *pContainerToSearch,  //  IDirectorySearch pointer to the container to search.     LPOLESTR szFilter, //  Filter to find specific users.     //  NULL returns all user objects.     LPOLESTR *pszPropertiesToReturn, //  Properties to return for user objects found.     //  NULL returns all set properties.     BOOL bIsVerbose //  TRUE indicates that display all properties for the found objects.     //  FALSE indicates that only the RDN.     );   //  Entry point for the application. void wmain(int argc, wchar_t *argv[]) {     //  Initialize COM.     CoInitialize(NULL);     HRESULT hr = S_OK;     //  Get rootDSE and the current user domain container distinguished name.     IADs *pObject = NULL;     IDirectorySearch *pContainerToSearch = NULL;     LPOLESTR szPath = new OLECHAR[MAX_PATH];     BOOL bReturnVerbose = FALSE;     DWORD dwLength = MAX_PATH * 2;     LPOLESTR pszBuffer = new OLECHAR[dwLength];     VARIANT var;      hr = ADsOpenObject(L"LDAP://rootDSE",         NULL,         NULL,         ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.         IID_IADs,         (void**)&pObject);     if (FAILED(hr))     {         wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE. ");         if (pObject)             pObject->Release();         return;     }     if (SUCCEEDED(hr))     {         hr = pObject->Get(_bstr_t("defaultNamingContext"), &var);         if (SUCCEEDED(hr))         {             wprintf(L"bstrVal: %s ", var.bstrVal);             //  Build path to the domain container.            // wcsncpy_s(szPath, L"LDAP://", MAX_PATH);           // wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath));              //hr = ADsOpenObject(szPath,            hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=Infosys,DC=iaseries,Dc=local",                 NULL,                 NULL,                 ADS_SECURE_AUTHENTICATION, //  Use Secure Authentication.                 IID_IDirectorySearch,                 (void**)&pContainerToSearch);              if (SUCCEEDED(hr))             {                 hr = FindComputers(pContainerToSearch, //  IDirectorySearch pointer to domainDNS container.                     pszBuffer,                     NULL, //  Return all properties.                     bReturnVerbose                     );                 if (SUCCEEDED(hr))                 {                     if (S_FALSE == hr)                         wprintf(L"Computer object cannot be found. ");                 }                 else if (E_ADS_INVALID_FILTER == hr)                     wprintf(L"Cannot execute query. Invalid filter was specified. ");                 else                     wprintf(L"Query failed to run. HRESULT: %x ", hr);              }             else             {                 wprintf(L"Cannot execute query. Cannot bind to the container. ");             }             if (pContainerToSearch)                 pContainerToSearch->Release();          }         VariantClear(&var);      }      if (pObject)         pObject->Release();      //  Uninitialize COM.     CoUninitialize();     delete[] szPath;     delete[] pszBuffer;      getchar();  }   HRESULT FindComputers(IDirectorySearch *pContainerToSearch,  //  IDirectorySearch pointer to the container to search.     LPOLESTR szFilter, //  Filter for finding specific users.     //  NULL returns all user objects.     LPOLESTR *pszPropertiesToReturn, //  Properties to return for user objects found.     //  NULL returns all set properties.     BOOL bIsVerbose    //  TRUE indicates that all properties for the found objects are displayed.     //  FALSE indicates only the RDN.     ) {     if (!pContainerToSearch)         return E_POINTER;     DWORD dwLength = (MAX_PATH * 2)+100;     // Create search filter.     LPOLESTR pszSearchFilter = new OLECHAR[dwLength];      //  Add the filter.     //swprintf_s(pszSearchFilter, dwLength, L"((objectClass=Computer)%s)", szFilter);     swprintf_s(pszSearchFilter, dwLength, L"(&(objectClass=*)(objectCategory=Computer)%s)", szFilter);       //  Specify subtree search.     ADS_SEARCHPREF_INFO SearchPrefs;     SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;     SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;     SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;     DWORD dwNumPrefs = 1;      //  COL for iterations.     LPOLESTR pszColumn = NULL;     ADS_SEARCH_COLUMN col;     HRESULT hr = S_OK;      //  Interface Pointers     IADs  *pObj = NULL;     IADs  * pIADs = NULL;      //  Search handle.     ADS_SEARCH_HANDLE hSearch = NULL;      //  Set search preference.     hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs);     if (FAILED(hr))         return hr;      LPOLESTR pszBool = NULL;     DWORD dwBool = 0;     PSID pObjectSID = NULL;     LPOLESTR szSID = NULL;     LPOLESTR szDSGUID = new WCHAR[39];     LPGUID pObjectGUID = NULL;     SYSTEMTIME systemtime;     DATE date;     VARIANT varDate;     LPOLESTR *pszPropertyList = NULL;     LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" };      LPOLESTR szName = new OLECHAR[MAX_PATH];     LPOLESTR szDN = new OLECHAR[MAX_PATH];      VariantInit(&varDate);      int iCount = 0;     DWORD x = 0L;        if (!bIsVerbose)     {         //  Return non-verbose list properties only.         hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,             pszNonVerboseList,             sizeof(pszNonVerboseList) / sizeof(LPOLESTR),             &hSearch             );     }     else     {         if (!pszPropertiesToReturn)         {             //  Return all properties.             hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,                 NULL,                 (DWORD)-1,                 &hSearch                 );         }         else         {             //  Specified subset.             pszPropertyList = pszPropertiesToReturn;             //  Return specified properties.             hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,                 pszPropertyList,                 sizeof(pszPropertyList) / sizeof(LPOLESTR),                 &hSearch                 );         }     }     if (SUCCEEDED(hr))     {         //  Call IDirectorySearch::GetNextRow() to retrieve the next data row.         hr = pContainerToSearch->GetFirstRow(hSearch);         if (SUCCEEDED(hr))         {             while (hr != S_ADS_NOMORE_ROWS)             {                 //  Keep track of count.                 iCount++;                 if (bIsVerbose)                     wprintf(L"---------------------------------- ");                 //  Loop through the array of passed column names,                 //  print the data for each column.                  while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS)                 {                     hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col);                     if (SUCCEEDED(hr))                     {                         //  Print the data for the column and free the column.                         if (bIsVerbose)                         {                             //  Get the data for this column.                             wprintf(L"%s ", col.pszAttrName);                             switch (col.dwADsType)                             {                             case ADSTYPE_DN_STRING:                                 for (x = 0; x< col.dwNumValues; x++)                                 {                                     wprintf(L"  %s  ", col.pADsValues[x].DNString);                                 }                                 break;                             case ADSTYPE_CASE_EXACT_STRING:                             case ADSTYPE_CASE_IGNORE_STRING:                             case ADSTYPE_PRINTABLE_STRING:                             case ADSTYPE_NUMERIC_STRING:                             case ADSTYPE_TYPEDNAME:                             case ADSTYPE_FAXNUMBER:                             case ADSTYPE_PATH:                                 for (x = 0; x< col.dwNumValues; x++)                                 {                                     wprintf(L"  %s  ", col.pADsValues[x].CaseIgnoreString);                                 }                                 break;                             case ADSTYPE_BOOLEAN:                                 for (x = 0; x< col.dwNumValues; x++)                                 {                                     dwBool = col.pADsValues[x].Boolean;                                     pszBool = dwBool ? L"TRUE" : L"FALSE";                                     wprintf(L"  %s  ", pszBool);                                 }                                 break;                             case ADSTYPE_INTEGER:                                 for (x = 0; x< col.dwNumValues; x++)                                 {                                     wprintf(L"  %d  ", col.pADsValues[x].Integer);                                 }                                 break;                             case ADSTYPE_OCTET_STRING:                                 if (_wcsicmp(col.pszAttrName, L"objectSID") == 0)                                 {                                     for (x = 0; x< col.dwNumValues; x++)                                     {                                         pObjectSID = (PSID)(col.pADsValues[x].OctetString.lpValue);                                         //  Convert SID to string.                                         ConvertSidToStringSid(pObjectSID, &szSID);                                         wprintf(L"  %s  ", szSID);                                         LocalFree(szSID);                                     }                                 }                                 else if ((_wcsicmp(col.pszAttrName, L"objectGUID") == 0))                                 {                                     for (x = 0; x< col.dwNumValues; x++)                                     {                                         //  Cast to LPGUID.                                         pObjectGUID = (LPGUID)(col.pADsValues[x].OctetString.lpValue);                                         //  Convert GUID to string.                                         ::StringFromGUID2(*pObjectGUID, szDSGUID, 39);                                         //  Print the GUID.                                         wprintf(L"  %s  ", szDSGUID);                                     }                                 }                                 else                                     wprintf(L"  Value of type Octet String. No Conversion.");                                 break;                             case ADSTYPE_UTC_TIME:                                 for (x = 0; x< col.dwNumValues; x++)                                 {                                     systemtime = col.pADsValues[x].UTCTime;                                     if (SystemTimeToVariantTime(&systemtime,                                         &date) != 0)                                     {                                         //  Pack in variant.vt.                                         varDate.vt = VT_DATE;                                         varDate.date = date;                                         VariantChangeType(&varDate, &varDate, VARIANT_NOVALUEPROP, VT_BSTR);                                         wprintf(L"  %s  ", varDate.bstrVal);                                         VariantClear(&varDate);                                     }                                     else                                         wprintf(L"  Could not convert UTC-Time. ", pszColumn);                                 }                                 break;                             case ADSTYPE_NT_SECURITY_DESCRIPTOR:                                 for (x = 0; x< col.dwNumValues; x++)                                 {                                     wprintf(L"  Security descriptor. ");                                 }                                 break;                             default:                                 wprintf(L"Unknown type %d. ", col.dwADsType);                             }                         }                         else                         { #ifdef _MBCS                             //  Verbose handles only the two single-valued attributes: cn and ldapdisplayname,                             //  so this is a special case.                             if (0 == wcscmp(L"name", pszColumn))                             {                                 //wcscpy_s(szName, col.pADsValues->CaseIgnoreString);                                 szName = col.pADsValues->CaseIgnoreString;                             }                             if (0 == wcscmp(L"distinguishedName", pszColumn))                             {                                 //wcscpy_s(szDN, col.pADsValues->CaseIgnoreString);                                 szDN = col.pADsValues->CaseIgnoreString;                             } #endif _MBCS                         }                         pContainerToSearch->FreeColumn(&col);                     }                     FreeADsMem(pszColumn);                 }                 if (!bIsVerbose)                     wprintf(L"%s   DN: %s  ", szName, szDN);                 //  Get the next row.                 hr = pContainerToSearch->GetNextRow(hSearch);             }          }         //  Close the search handle to cleanup.         pContainerToSearch->CloseSearchHandle(hSearch);     }     if (SUCCEEDED(hr) && 0 == iCount)         hr = S_FALSE;      delete[] szName;     delete[] szDN;     delete[] szDSGUID;     delete[] pszSearchFilter;     return hr; } 
</div
        

回答リスト

0
 
vote
vote
ベストアンサー
 

IAMは問題を把握することができます。以下は作業サンプルのコードです。 私はコードをたくさん掃除していません。使用する予定の場合は更新してください。

 <コード> link_to7  

ありがとう、 vijay

 

Iam able to figure out the issue. Below is the code for working sample. I haven't cleaned the code much. Please update if you intend to use.

// ConsoleApplication3.cpp : Defines the entry point for the console application.  //    #include "stdafx.h"  #include <objbase.h>  #include <wchar.h>  #include <activeds.h>  #include <sddl.h>  #include <comutil.h>  #include <string.h>  #include <stdio.h>      HRESULT FindComputers(IDirectorySearch *pContainerToSearch);  //  IDirectorySearch pointer to the container to search.         //  Entry point for the application.  void wmain(int argc, wchar_t *argv[])  {   //  Initialize COM.   CoInitialize(NULL);   HRESULT hr = S_OK;   //  Get rootDSE and the current user domain container distinguished name.   IADs *pObject = NULL;   IDirectorySearch *pContainerToSearch = NULL;   LPOLESTR szPath = new OLECHAR[MAX_PATH];   BOOL bReturnVerbose = FALSE;   DWORD dwLength = MAX_PATH * 2;   VARIANT var;      hr = ADsOpenObject(L"LDAP://rootDSE",    NULL,    NULL,    ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.    IID_IADs,    (void**)&pObject);   if (FAILED(hr))   {    wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE. ");    if (pObject)     pObject->Release();    return;   }   if (SUCCEEDED(hr))   {    hr = pObject->Get(_bstr_t("defaultNamingContext"), &var);    if (SUCCEEDED(hr))    {     //wprintf(L"bstrVal: %s ", var.bstrVal);          //  Build path to the domain container.       // wcsncpy_s(szPath, L"LDAP://", MAX_PATH);      // wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath));                   //hr = ADsOpenObject(szPath,       hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=MyDept,DC=Test,Dc=com",      NULL,      NULL,      ADS_SECURE_AUTHENTICATION, //  Use Secure Authentication.      IID_IDirectorySearch,      (void**)&pContainerToSearch);       if (SUCCEEDED(hr))     {      hr = FindComputers(pContainerToSearch); //  IDirectorySearch pointer to domainDNS container.             if (SUCCEEDED(hr))      {       if (S_FALSE == hr)        wprintf(L"Computer object cannot be found. ");      }      else if (E_ADS_INVALID_FILTER == hr)       wprintf(L"Cannot execute query. Invalid filter was specified. ");      else       wprintf(L"Query failed to run. HRESULT: %x ", hr);       }     else     {      wprintf(L"Cannot execute query. Cannot bind to the container. ");     }     if (pContainerToSearch)      pContainerToSearch->Release();      }    VariantClear(&var);     }     if (pObject)    pObject->Release();     //  Uninitialize COM.   CoUninitialize();   delete[] szPath;     getchar();     }      HRESULT FindComputers(IDirectorySearch *pContainerToSearch)  //  IDirectorySearch pointer to the container to search.  {   if (!pContainerToSearch)    return E_POINTER;   DWORD dwLength = (MAX_PATH * 2);   // Create search filter.   LPOLESTR pszSearchFilter = new OLECHAR[dwLength];     //  Add the filter.   pszSearchFilter = L"((objectCategory=computer))";      //  Specify subtree search.   ADS_SEARCHPREF_INFO SearchPrefs;   SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;   SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;   SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;   DWORD dwNumPrefs = 1;     //  COL for iterations.   LPOLESTR pszColumn = NULL;   ADS_SEARCH_COLUMN col;   HRESULT hr = S_OK;     //  Interface Pointers   IADs  *pObj = NULL;   IADs  * pIADs = NULL;     //  Search handle.   ADS_SEARCH_HANDLE hSearch = NULL;     //  Set search preference.   hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs);   if (FAILED(hr))    return hr;     LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" };     LPOLESTR szName = new OLECHAR[MAX_PATH];   LPOLESTR szDN = new OLECHAR[MAX_PATH];       int iCount = 0;   DWORD x = 0L;        //  Return non-verbose list properties only.   hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,    pszNonVerboseList,    sizeof(pszNonVerboseList) / sizeof(LPOLESTR),    &hSearch    );     if (SUCCEEDED(hr))   {    //  Call IDirectorySearch::GetNextRow() to retrieve the next data row.    hr = pContainerToSearch->GetFirstRow(hSearch);    if (SUCCEEDED(hr))    {     while (hr != S_ADS_NOMORE_ROWS)     {      //  Keep track of count.      iCount++;            //  Loop through the array of passed column names,      //  print the data for each column.        while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS)      {       hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col);       if (SUCCEEDED(hr))       {        //  Verbose handles only the two single-valued attributes: cn and ldapdisplayname,        //  so this is a special case.        if (0 == wcscmp(L"name", pszColumn))        {         //wcscpy_s(szName, col.pADsValues->CaseIgnoreString);         szName = col.pADsValues->CaseIgnoreString;        }        if (0 == wcscmp(L"distinguishedName", pszColumn))        {         //wcscpy_s(szDN, col.pADsValues->CaseIgnoreString);         szDN = col.pADsValues->CaseIgnoreString;        }             pContainerToSearch->FreeColumn(&col);       }       FreeADsMem(pszColumn);      }            wprintf(L"%s   DN: %s  ", szName, szDN);        //  Get the next row.      hr = pContainerToSearch->GetNextRow(hSearch);     }      }    //  Close the search handle to cleanup.    pContainerToSearch->CloseSearchHandle(hSearch);   }   if (SUCCEEDED(hr) && 0 == iCount)    hr = S_FALSE;     delete[] szName;   delete[] szDN;   delete[] pszSearchFilter;   return hr;  }

Thanks, Vijay

</div
 
 

関連する質問

1  PowerShellテキストファイルの各項目に対してコマンドを実行します  ( Powershell run command against each item in text file ) 
私が最初に投稿したスクリプトで私の最初の問題を見つけました。これは次の問題です、そして、私はこれが簡単なものであると確信しています。 次のスクリプトの目的は、ホスト名リストテキストファイル(次のように)を参照している各コンピュータに対して指定されたADSIコマ...

0  ADSIはXPワークステーションからIISに接続しません  ( Adsi will not connect to iis from xp workstation ) 
wscript内でVBScriptを使用して、サーバーからIIS構成をリモートで読み書きします。私のデスクボックスからこれらの同じスクリプトを実行しようとしたとき、彼らは失敗します。例: <事前> <コード> Dim vdir Set vdir = GetO...

1  次回のログイン時にVBScriptでユーザーパスワードを変更します  ( Change user password at next login with a vbscript ) 
私は自宅の基本的なルータに接続されており、タイムワーナーを持っています。私はコンピュータ上でそれを実行できるようにスクリプトを書いていて、次回のログイン時に私のパスワードを変更させるでしょう。 UAL-10167がコンピュータ名で、ユーザー名は次のような次のス...

3  PowerShellを使用したActive DirectoryのActive Directoryユーザープロパティの更新  ( Updating active directory user properties in active directory using powershell ) 
PowerShell v2.0を使用して、Windows Server 2003 R2環境では、 SET-QADUSER 自分の電話番号とタイトルのように、Active Directoryのユーザープロパティを更新するには? ここでのトリックは、set-qa...

5  PowerShell - LDAPクエリを最適化する方法  ( How can i optimize my powershell ldap query ) 
私は、CSV(または他のデータセット)から読み取るスクリプトを作成し、AD環境でユーザーを作成します。 基本的に、スクリプトに渡されるデータセットが処理され、存在しない場合はユーザーが作成されます。ユーザーがすでに広告に存在する場合、スクリプトはエントリをスキ...

6  「サービスとしてログオン」を設定し、ADSIで「ログオンをローカルに許可する」  ( Setting log on as a service and allow logon locally with adsi ) 
ADSIを使用して適切に機能しているユーザー作成を自動化するためのPowerShellスクリプトを作成しようとしています。ただし、次のプロパティを設定する必要があります。これはどうして(またはADSIさえも実行できる場合)によくわかりません。 サービスとし...

1  IISに対するADSIクエリは、Vista上のIIS Managerに同意しません  ( Adsi query against iis does not agree with iis manager on vista ) 
Vistaを使う... IIS WebサイトでScriptMapsを設定するためにADSIを使用するスクリプトがあります。それはJavaScriptです、cscript.exe内で実行され、コードは次のようになります: <事前> <コード> var we...

0  C#でLINQを使用する方法Windowsローカルユーザーアカウントを問い合わせる  ( How to use linq in c sharp to query windows local user accounts ) 
ADSIを使用してC#からWindowsローカルユーザーアカウントを照会したいと思いました。 Winntプロバイダが使用されるので、ディレクトリサーチャはサポートされていません。 私は次のようにしています: <事前> <コード> DirectoryEntri...

2  IISのためのワイルドカードフィルタをプログラム的に追加する方法は?  ( How do i programatically add a wildcard filter for iis ) 
System.DirectoryServicesを使用して、IIS(バージョン5.0とIIS 6.0)のワイルドカードフィルタをプログラムで追加します。 だれでも思考のために食べ物を持っていますか?事前にありがとう! ...

0  NetBIOS名からのDNS名 - API関数  ( Dns name from netbios name api function ) 
は、 netbios ユーザーまたはグループディレクトリオブジェクトの名前を解決できる任意のAPI関数です。 次の形式で出力が必要です。 namefulifieddn - 例:cn = John Smith、OU =ユーザー、DC =エンジニアリング...




© 2022 cndgn.com All Rights Reserved. Q&Aハウス 全著作権所有