none
Deactivate users who are no longer exist in AD but were added into resource pool RRS feed

  • Question

  • Hello forum members,

    does anyone know how to deactivate users who are no longer exist in AD , but were added into resource pool?

    I an trying to write some code that would update a custom value for each resource, but my code breaks whenever it encounters a resource that is not longer exists in AD. Any suggestions are appreciated.

                                                                                          

      // Modify the resources, code was taken from http://msdn.microsoft.com/en-us/library/websvcresource.resource.updateresources(v=office.12).ASPX

                    foreach (SvcResource.ResourceDataSet.ResourcesRow resourceRow in resourceDs.Resources)
                    {
                        Console.WriteLine("Check out " + resourceRow.RES_NAME);
                        if (resourceRow.IsRES_CHECKOUTBYNull())
                        {
                            resourceSvc.CheckOutResources(new Guid[] { resourceRow.RES_UID });
                            checkedOut = true;
                        }
                        else
                        {
                            if (resourceRow.RES_CHECKOUTBY == me)
                            {
                                checkedOut = true;
                            }
                            else
                            {
                                checkedOut = false;
                                Console.WriteLine("\tCan't check out this resource, skip updating this one.");
                            }
                        }
                        if (checkedOut)
                        {
                            SvcResource.ResourceDataSet updateDs = resourceSvc.ReadResource(resourceRow.RES_UID);


                                if (resourceRow.RES_TYPE <= (int)PSLibrary.Resource.Type.INACTIVATED_OFFSET)
                                {


                                    updateDs.Resources[0].RES_CODE = "A" + rand.Next(1000, 9999);
                                    Console.WriteLine("Update RES_CODE to " + updateDs.Resources[0].RES_CODE);
                                    resourceSvc.UpdateResources(updateDs, false, false);
                                    Console.WriteLine("Check in " + resourceRow.RES_NAME);
                                    resourceSvc.CheckInResources(new Guid[] { resourceRow.RES_UID }, false);

                                }


                        }

                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("".PadRight(30, '-'));
                        Console.ResetColor();
                    }


    tatiana

    Monday, August 18, 2014 6:49 PM

Answers

  • This is the logic I used:

    1) Try to inactivate the user

    2) If it fails with "AdminNTAccountNotFound" then delete

    try {
         using( OperationContextScope ocs = new OperationContextScope( resourceClient.InnerChannel ) ) {
              resourceClient.CheckOutResources( new Guid[] { resourceUID } );
         }
    
         // Perform the update
         rsDS = ( SvcResource.ResourceDataSet ) rsDS.GetChanges();
         resourceClient.UpdateResources( rsDS, false, true );
    }
    catch( Exception ex ) {
         if( ex.Message.Contains( "AdminNTAccountNotFound" ) ) {
             try {
                  resourceClient.CheckInResources( new Guid[] { resourceUID }, true );
             }
             catch {
             }
    
             //The resource does not have a valid account, deleting...
             using( OperationContextScope ocs = new OperationContextScope( resourceClient.InnerChannel ) ) {
                 resourceClient.CheckOutResources( new Guid[] { resourceUID } );
                 resourceClient.DeleteResources( new Guid[] { resourceUID }, "No longer in RBS structure and/or AD" );
             }
          }
       }


    120811049008

    • Marked as answer by tatiana obrien Thursday, August 21, 2014 3:49 PM
    Thursday, August 21, 2014 2:32 PM

All replies

  • Hey Tatiana,

    I recently tried to do the same thing. Updating from the PSI or PWA does not work, the resource is just in an error state (this is a bug in my opinion).

    I ended up deleting these resources. Not the best, but its just a suggestion.


    120811049008

    Wednesday, August 20, 2014 8:01 PM
  • thanks Paul. this is what i am doing right now - manually deactivating users once the code breaks. But we have so many users - i was hoping there was a better way to either omit those resources or deactivate/delete prior to running my code.

    tatiana

    Wednesday, August 20, 2014 10:34 PM
  • This is the logic I used:

    1) Try to inactivate the user

    2) If it fails with "AdminNTAccountNotFound" then delete

    try {
         using( OperationContextScope ocs = new OperationContextScope( resourceClient.InnerChannel ) ) {
              resourceClient.CheckOutResources( new Guid[] { resourceUID } );
         }
    
         // Perform the update
         rsDS = ( SvcResource.ResourceDataSet ) rsDS.GetChanges();
         resourceClient.UpdateResources( rsDS, false, true );
    }
    catch( Exception ex ) {
         if( ex.Message.Contains( "AdminNTAccountNotFound" ) ) {
             try {
                  resourceClient.CheckInResources( new Guid[] { resourceUID }, true );
             }
             catch {
             }
    
             //The resource does not have a valid account, deleting...
             using( OperationContextScope ocs = new OperationContextScope( resourceClient.InnerChannel ) ) {
                 resourceClient.CheckOutResources( new Guid[] { resourceUID } );
                 resourceClient.DeleteResources( new Guid[] { resourceUID }, "No longer in RBS structure and/or AD" );
             }
          }
       }


    120811049008

    • Marked as answer by tatiana obrien Thursday, August 21, 2014 3:49 PM
    Thursday, August 21, 2014 2:32 PM
  • thank you so much Paul! This is what i was looking for !!!

    tatiana

    Thursday, August 21, 2014 3:49 PM