Created
July 26, 2016 18:03
-
-
Save drewchapin/efe9d05850f1957430997b34d0884374 to your computer and use it in GitHub Desktop.
Lists all members of a group and how many days are left until their password expires.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Author Drew Chapin <druciferre@gmail.com> | |
* @Date 2013-05-03 | |
* | |
* Lists all members of a group and how many days are left until their password expires. | |
*/ | |
#pragma comment(lib, "ActiveDS.lib") | |
#pragma comment(lib, "Oleaut32.lib") | |
#pragma comment(lib, "ADSIid.lib") | |
#include <Windows.h> | |
#include <ActiveDS.h> | |
#include <Iads.h> | |
#include <OleAuto.h> | |
#include <iostream> | |
#include <string> | |
#include <atlbase.h> | |
void DisplayUserName( IADsUser* pUser ) | |
{ // http://msdn.microsoft.com/en-us/library/windows/desktop/aa746343%28v=vs.85%29.aspx | |
DATE pwdexp = {0}; | |
HRESULT hr = pUser->get_PasswordExpirationDate(&pwdexp); | |
if( SUCCEEDED(hr) ) | |
{ | |
CComBSTR bsName; | |
pUser->get_Name(&bsName); | |
std::wcout << L"User: " << bsName.m_str << L" - password expires in "; | |
SYSTEMTIME st = {0}; | |
GetSystemTime(&st); | |
DATE now = 0; | |
SystemTimeToVariantTime(&st, &now); | |
std::wcout << static_cast<int>(pwdexp - now) << L" days" << std::endl; | |
} | |
} | |
HRESULT EnumMembers( IADsMembers* pMembers ) | |
{ // http://msdn.microsoft.com/en-us/library/windows/desktop/aa706042%28v=vs.85%29.aspx | |
IUnknown *pUnk; | |
HRESULT hr = pMembers->get__NewEnum(&pUnk); | |
if( !SUCCEEDED(hr) ) | |
return hr; | |
IEnumVARIANT* pEnum; | |
hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void**>(&pEnum)); | |
if( !SUCCEEDED(hr) ) | |
{ | |
pUnk->Release(); | |
return hr; | |
} | |
VARIANT var; | |
unsigned long lFetch = 0; | |
IDispatch* pDisp; | |
hr = pEnum->Next(1, &var, &lFetch); | |
while( SUCCEEDED(hr) ) | |
{ | |
if( 1 == lFetch ) //what the hell is fetch??? | |
{ | |
pDisp = V_DISPATCH(&var); | |
IADsUser* pUser; | |
hr = pDisp->QueryInterface(IID_IADsUser, reinterpret_cast<void**>(&pUser)); | |
if( SUCCEEDED(hr) ) | |
DisplayUserName(pUser); | |
pDisp->Release(); | |
VariantClear(&var); | |
} | |
hr = pEnum->Next(1, &var, &lFetch); | |
} | |
pDisp->Release(); | |
pEnum->Release(); | |
pUnk->Release(); | |
} | |
int main( int argc, char* argv[] ) | |
{ | |
CoInitialize(NULL); | |
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa706032%28v=vs.85%29.aspx | |
IADsGroup* pGroup; | |
HRESULT hr = ADsGetObject(L"LDAP://CN=*Berea NC Techs,OU=Distribution Groups,OU=BER,DC=pgw,DC=local", IID_IADsGroup, reinterpret_cast<void**>(&pGroup)); | |
if( SUCCEEDED(hr) ) | |
{ | |
IADsMembers* pMembers; | |
hr = pGroup->Members(&pMembers); | |
EnumMembers(pMembers); | |
pGroup->Release(); | |
} | |
CoUninitialize(); | |
std::getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment