1

I wrote a c# code that creates new local user

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry group = localMachine.Children.Find("administrators", "group");
DirectoryEntry user = localMachine.Children.Find(accountName, "user");
Console.WriteLine(user.Properties.ToString());

I tried to set the logon script for that user by doing:

localMachine.Properties["scriptPath"].Insert(0, "logonScript.vbs"); localMachine.CommitChanges();

same with group or user instances.but the property doesn't exist in any of theses instances (localMachine, group or user). I know that because I did:

System.Collections.ICollection col = localMachine.Properties.PropertyNames;
foreach (Object ob in col) { Console.WriteLine(ob.ToString()); }

Any idea of how to do that in other way?Cheers,

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • I'm looking at your problem and just wanted to mention that group is a reserved word for LINQ and will most likely throw a compile error at build time. – Jeff LaFay Jul 14 '10 at 18:16
  • Have you tried using the `LoginScript` property instead of adding a new one? It sounds like it has the exact same purpose as your `scriptPath` property. – Jeff LaFay Jul 14 '10 at 19:20

1 Answers1

2

The following code is well working on my Windows Seven :

DirectoryEntry deComputer = new DirectoryEntry("WinNT://MyComputer,computer");
DirectoryEntry deUser = deComputer.Children.Add("JPB", "user");
deUser.Invoke("SetPassword", new object[] { "Password" }); 
deUser.Properties["Description"].Add("user $userName");
deUser.Properties["userflags"].Add(512);
deUser.Properties["passwordExpired"].Add(1);
deUser.Properties["LoginScript"].Add("start.cmd");
deUser.CommitChanges();

The script file Start.cmd is present in the directory :

%systemroot%\system32\repl\import\Scripts

Here is the same thing in PowerShell :

$computer = "MyComputer"
$userName = "jpb"
$objComputer = [ADSI]"WinNT://$computer"
$objUser = $objComputer.Create('user', $userName)
$objUser.SetPassword("Password")
$objUser.PSBase.InvokeSet('Description', "user $userName")
$objUser.PSBase.InvokeSet('LoginScript', "start.cmd")
$objUser.PSBase.InvokeSet('userflags', 512)
$objUser.PSBase.InvokeSet('passwordExpired', 1)
$objUser.SetInfo();

I hope it helps

JP

JPBlanc
  • 70,406
  • 17
  • 130
  • 175