我写了一个创建新本地用户的c#代码
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());
我尝试通过执行以下操作为该用户设置登录脚本:
localMachine.Properties["scriptPath"].Insert(0, "logonScript.vbs"); localMachine.CommitChanges();
与组或用户实例相同。但该属性在任何这些实例(localMachine,group或user)中都不存在。我知道因为我做了:
System.Collections.ICollection col = localMachine.Properties.PropertyNames;
foreach (Object ob in col) { Console.WriteLine(ob.ToString()); }
如何以其他方式做到这一点?干杯,
答案 0 :(得分:2)
以下代码适用于我的Windows 7:
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();
脚本文件Start.cmd出现在目录中:
%systemroot%\system32\repl\import\Scripts
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();
我希望它有所帮助
JP