34

Consider this code attempting to create an Active Directory account. It's generating an exception here with a certain set of data. It's not clear right now what's causing the exception.

 var user = new UserPrincipal(someValidUserContext,
                              ".x-xyz-t-FooFooBarTest", "somePwd", true);

 user.UserPrincipalName = ".x-xyz-t-FooFooBarTest@foobarbatbaz.net";
 user.SamAccountName = ".x-xyz-t-FooFooBarTest";         
 user.DisplayName = "Some String 16 chars long";
 user.Name =  "Some String 16 chars long";
 user.Description = "Foo BarBaz 12 more characters";
 user.AccountExpirationDate = someDateInFuture;
 user.UserCannotChangePassword = true;
 user.Save();

 // exception thrown on Save(): 
 // A device attached to the system is not functioning

alt text

PrincipalOperationException was unhandled by user code: A device attached to the system is not functioning

What's causing this exception, and how can you work around it?

p.campbell
  • 98,673
  • 67
  • 256
  • 322

1 Answers1

75

The sAMAccountName attribute must be 20 characters or less.

Tawab Wakil
  • 1,737
  • 18
  • 33
Germ
  • 6,360
  • 4
  • 26
  • 26
  • 30
    Another message that Microsoft could display: You just had a an error. Yours truly. Microsoft. – Samuel Feb 05 '13 at 18:50
  • 2
    Here is a very poetic article that I've just found about the problem: http://usrlocal.com/2009/08/a-device-attached-to-the-system-is-not-functioning/ Again... a little bit of sarcasm :) – Samuel Aug 06 '14 at 17:53
  • 4
    6 years down the line and the same bloody error message. And was me thinking "surely it'll be useless to google for such a generic error message". And yes, 20 characters are still the limitation and the problem solver! – trailmax Jul 03 '16 at 23:32
  • https://msdn.microsoft.com/en-us/library/ms679635(VS.85).aspx?irgwc=1&OCID=AID681541_aff_7593_1243925&tduid=(ir_Vg0xa4zO11IyV-M0D02o4TZtUkjWAwWyl00FSc0)(7593)(1243925)(TnL5HPStwNw-p7cDo5BIWuDDcBt0yTImYA)()&irclickid=Vg0xa4zO11IyV-M0D02o4TZtUkjWAwWyl00FSc0 SAM-Account-Name attribute – Kiquenet Jun 13 '18 at 11:05