Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Deleting a Group on a Member Server or Windows NT Workstation/Windows 2000 Professional

[C++]

The following function deletes a group on a member server or a computer running Windows NT Workstation/Windows 2000 Professional:

////////////////////////////////////////////////////////////////////////////////////////////////////
/*  DeleteADObject()   - Deletes the passed object by AdsPath 
 
    Parameters
 
        LPOLESTR pwszAdsPath        -       AdsPath of object to delete
 
    Optional Parameters:
 
       LPOLESTR pwszUser            - User Name and Password, if the parameters are NOT passed, 
       LPOLESTER pwszPassWord       - Binding will use ADsGetObject, if the parameters
                                    - Are specified, will use ADsOpenObject, passing user name and password
 
*/
HRESULT DeleteADObject(LPOLESTR pwszAdsPath, LPOLESTR  pwszUser,LPOLESTR  pwszPassWord)
{
    HRESULT             hr;
    BSTR                bsParentPath;
    IADs *              pIADsToDelete = NULL;
    IDirectoryObject *  pIDirObjectParent= NULL;
    VARIANT             vCNToDelete;
    WCHAR               pwszTemp[512];
 
    VariantInit(&vCNToDelete);
    OutputDebugString(pwszAdsPath);
    OutputDebugString(L"\r\n");
 
    // Bind to the object being deleted
 
    assert((pwszUser==NULL && pwszPassWord == NULL) || (pwszUser && pwszPassWord));
 
    // If a username and password are passed in, use ADsOpenObject()
    // otherwise use ADsGetObject()
    if (!pwszUser) // No user password passed, use ADsOpenObject 
    {
        hr = ADsGetObject(  pwszAdsPath, IID_IADs,(void **)& pIADsToDelete);
    }
    else
    {
        hr = ADsOpenObject(pwszAdsPath, pwszUser, pwszPassWord, 
                           ADS_SECURE_AUTHENTICATION,IID_IADs, (void**) & pIADsToDelete);
    }
 
    if (SUCCEEDED(hr))
    {
       // Get the parent path
        hr = pIADsToDelete->get_Parent(&bsParentPath); 
 
        // Get the CN property for the object to delete
        hr = pIADsToDelete->Get(L"cn",&vCNToDelete);
        if (SUCCEEDED(hr))
        {
            // ************************************************************
            // Now bind to the parent
            // If a username and password are passed in, use ADsOpenObject()
            // otherwise use ADsGetObject()
            if (!pwszUser) // No user password passed, use ADsOpenObject        
            {
                hr = ADsGetObject(  bsParentPath, IID_IDirectoryObject,(void **)& pIDirObjectParent);
            }
            else
            {
                hr = ADsOpenObject(bsParentPath, pwszUser, pwszPassWord, 
                                   ADS_SECURE_AUTHENTICATION,IID_IDirectoryObject, (void**) & pIDirObjectParent);
            }
            if (SUCCEEDED(hr))
            {
                // Release the object to delete
                pIADsToDelete->Release();
                pIADsToDelete =NULL;
 
                // Put the CN property into a string beginning with CN=
                swprintf(pwszTemp,L"cn=%s\n",vCNToDelete.bstrVal);
 
                // Ask the parent to delete the child
                hr =pIDirObjectParent->DeleteDSObject(pwszTemp);
                // Release the Parent Object
                pIDirObjectParent->Release();
                pIDirObjectParent = NULL;
            }
        }
        SysFreeString(bsParentPath);
    }
    // If we have a IADsObject- we need to release it
    if ( pIADsToDelete)
    {
        // Release the object to delete
        pIADsToDelete->Release();
        pIADsToDelete =NULL;
    }
    
    VariantClear(&vCNToDelete);
 return hr;
}
[Visual Basic]

The following code deletes a group on a member server or a computer running Windows NT Workstation/Windows 2000 Professional:

'Example: Deleting a local group on a member server or Windows NT Workstation/Windows 2000 Professional
 
'''''''''''''''''''''''''''''''''''''''
'Parse the arguments
'''''''''''''''''''''''''''''''''''''''
On Error Resume Next
 
Set oArgs = WScript.Arguments
If oArgs.Count < 2 Then
    sComputer = InputBox("This script deletes a group from a member server or workstation." & vbCrLf & vbCrLf &"Specify 
 
the computer name:")
    sGroup = InputBox("Specify the group name:")
Else
    sComputer = oArgs.item(0)
    sGroup = oArgs.item(1)
End If
 
If sComputer = "" Then
    WScript.Echo "No computer name was specified. You must specify a computer name."
    WScript.Quit(1)
End If
If sGroup = "" Then
    WScript.Echo "No group name was specified. You must specify a group name."
    WScript.Quit(1)
End If
 
'''''''''''''''''''''''''''''''''''''''
'Bind to the computer
'''''''''''''''''''''''''''''''''''''''
Set cont= GetObject("WinNT://" & sComputer & ",computer")
If (Err.Number <> 0) Then
    BailOnFailure Err.Number, "on GetObject method"
End If
 
'''''''''''''''''''''''''''''''''''''''
'Delete the group
'''''''''''''''''''''''''''''''''''''''
'You do not need to specify localGroup, just group is sufficient.
Set oGroup = cont.Delete("group", sGroup)
If (Err.Number <> 0) Then
    BailOnFailure Err.Number, "on IADsContainer::Delete method"
End If
 
strText = "The group " & sGroup & " was deleted on computer " & sComputer & "."
 
Call show_groups(strText, sComputer)
 

'''''''''''''''''''''''''''''''''''''''
'Display subroutines
'''''''''''''''''''''''''''''''''''''''
Sub show_groups(strText, strName)
    MsgBox strText, vbInformation, "Create group on " & strName
End Sub